Skip to content

Commit

Permalink
WIP - rework with contextvar
Browse files Browse the repository at this point in the history
Signed-off-by: Shaanjot Gill <gill.shaanjots@gmail.com>
  • Loading branch information
shaangill025 committed Sep 7, 2023
1 parent 9a6e9b7 commit 8e9b0f2
Show file tree
Hide file tree
Showing 124 changed files with 479 additions and 1,396 deletions.
2 changes: 2 additions & 0 deletions Logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Other log levels fall back to `WARNING`.

* `--log-level` - The log level to log on std out.
* `--log-file` - Path to a file to log to.

## WIP - add config file details
* `--log-handler-config` - Specifies `when`, `interval`, `backupCount` for the `TimedRotatingFileMultiProcessHandler`. These 3 attributes are passed as a `;` seperated string. For example, `when` of d (days), `interval` of 7 and `backupCount` of 1 will be passed as `D;7;1`. Note: `backupCount` of 0 will mean all backup log files will be retained and not deleted at all. More details about these attributes can be found [here](https://docs.python.org/3/library/logging.handlers.html#timedrotatingfilehandler). `TimedRotatingFileMultiProcessHandler` supports the ability to cleanup logs by time and mantain backup logs and a custom JSON formatter for logs.
* `--log-fmt-pattern` - Specifies logging.Formatter pattern to override default patterns.
* `--log-json-fmt` - Specifies whether to use JSON logging formatter or text logging formatter. Defaults to `False`.
Expand Down
63 changes: 0 additions & 63 deletions aries_cloudagent/config/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1013,46 +1013,6 @@ def add_arguments(self, parser: ArgumentParser):
"('debug', 'info', 'warning', 'error', 'critical')"
),
)
parser.add_argument(
"--log-handler-config",
dest="log_handler_config",
type=str,
metavar="<log-handler-config>",
default=None,
env_var="ACAPY_LOG_HANDLER_CONFIG",
help=(
"Specifies when, interval, backupCount for the "
"TimedRotatingFileHandler. These attributes are "
"passed as a ; seperated string. For example, "
"when of D (days), interval of 7 and backupCount "
"of 1 will be passed as 'D;7;1'. Note: "
"backupCount of 0 will mean all backup log files "
"will be retained and not deleted at all."
),
)
parser.add_argument(
"--log-fmt-pattern",
dest="log_fmt_pattern",
type=str,
metavar="<log-fmt-pattern>",
default=None,
env_var="ACAPY_LOG_FMT_PATTERN",
help=(
"Specifies logging formatter pattern as string. Examples are included "
"in 'Logging.md'. For information regarding different attributes "
"supported in the pattern, please look at "
"https://docs.python.org/3/library/logging.html#logrecord-attributes."
),
)
parser.add_argument(
"--log-json-fmt",
action="store_true",
env_var="ACAPY_LOG_JSON_FMT",
help=(
"Specifies whether to use JSON logging formatter or "
"text logging formatter."
),
)

def get_settings(self, args: Namespace) -> dict:
"""Extract logging settings."""
Expand All @@ -1063,29 +1023,6 @@ def get_settings(self, args: Namespace) -> dict:
settings["log.file"] = args.log_file
if args.log_level:
settings["log.level"] = args.log_level
if args.log_handler_config:
try:
handler_config_attribs = (args.log_handler_config).split(";")
settings["log.handler_when"] = handler_config_attribs[0]
settings["log.handler_interval"] = int(handler_config_attribs[1])
settings["log.handler_bakcount"] = int(handler_config_attribs[2])
except IndexError:
raise ArgsParseError(
"With --log-handler-config, the provided argument must be "
"in 'when;interval;backupCount' format. Each of the 3 "
"attributes for TimedRotatingFileHandler must be specified."
)
except ValueError:
raise ArgsParseError(
"With --log-handler-config, 'interval' and 'backupCount' "
"should be a number [int]"
)
if args.log_fmt_pattern:
settings["log.fmt_pattern"] = args.log_fmt_pattern
if args.log_json_fmt:
settings["log.json_fmt"] = True
else:
settings["log.json_fmt"] = False
return settings


Expand Down
27 changes: 27 additions & 0 deletions aries_cloudagent/config/default_per_tenant_logging_config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[loggers]
keys=root

[handlers]
keys=stream_handler, timed_file_handler

[formatters]
keys=formatter

[logger_root]
level=ERROR
handlers=stream_handler, timed_file_handler

[handler_stream_handler]
class=StreamHandler
level=DEBUG
formatter=formatter
args=(sys.stderr,)

[handler_timed_file_handler]
class=logging.handlers.TimedRotatingFileMultiProcessHandler
level=DEBUG
formatter=formatter
args=('acapy.log', 'd', 7, 1,)

[formatter_formatter]
format=%(asctime)s %(wallet_id)s %(levelname)s %(pathname)s:%(lineno)d %(message)s
23 changes: 23 additions & 0 deletions aries_cloudagent/config/default_per_tenant_logging_config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
version: 1
formatters:
default:
format: '%(asctime)s %(wallet_id)s %(levelname)s %(pathname)s:%(lineno)d %(message)s'
handlers:
console:
class: logging.StreamHandler
level: DEBUG
formatter: default
stream: ext://sys.stderr
rotating_file:
class: logging.handlers.TimedRotatingFileMultiProcessHandler
level: DEBUG
filename: 'acapy.log'
when: 'd'
interval: 7
backupCount: 1
formatter: default
root:
level: INFO
handlers:
- console
- rotating_file
17 changes: 3 additions & 14 deletions aries_cloudagent/config/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from prompt_toolkit.eventloop.defaults import use_asyncio_event_loop
from prompt_toolkit.formatted_text import HTML

from ..config.logging import get_adapted_logger_inst
from ..config.settings import Settings
from ..core.profile import Profile
from ..ledger.base import BaseLedger
Expand Down Expand Up @@ -129,14 +128,9 @@ async def ledger_config(
"""Perform Indy ledger configuration."""

session = await profile.session()
_logger = get_adapted_logger_inst(
logger=LOGGER,
log_file=profile.settings.get("log.file"),
wallet_id=profile.settings.get("wallet.id"),
)
ledger = session.inject_or(BaseLedger)
if not ledger:
_logger.info("Ledger instance not provided")
LOGGER.info("Ledger instance not provided")
return False

async with ledger:
Expand Down Expand Up @@ -257,11 +251,6 @@ async def accept_taa(

mechanisms = taa_info["aml_record"]["aml"]
mechanism = None
_logger = get_adapted_logger_inst(
logger=LOGGER,
log_file=profile.settings.get("log.file"),
wallet_id=profile.settings.get("wallet.id"),
)
taa_acceptance_mechanism = profile.settings.get("ledger.taa_acceptance_mechanism")
taa_acceptance_version = profile.settings.get("ledger.taa_acceptance_version")

Expand All @@ -287,13 +276,13 @@ async def accept_taa(
elif sys.stdout.isatty():
mechanism = await select_aml_tty(taa_info, provision)
else:
_logger.warning(
LOGGER.warning(
"Cannot accept TAA without interactive terminal or taa accept config"
)

if not mechanism:
return False

_logger.debug(f"Accepting the TAA using mechanism '{mechanism}'")
LOGGER.debug(f"Accepting the TAA using mechanism '{mechanism}'")
await ledger.accept_txn_author_agreement(taa_info["taa_record"], mechanism)
return True
Loading

0 comments on commit 8e9b0f2

Please sign in to comment.