|
1 | 1 | import argparse |
2 | | -import logging |
3 | 2 | import typing |
4 | 3 |
|
5 | | -DEFAULT_LOGGING_LEVEL: str = logging.getLevelName(logging.INFO) |
6 | | -DEFAULT_LOGGING_FORMAT: str = '%(asctime)s [%(levelname)-8s] - %(filename)s:%(lineno)s -- %(message)s' |
| 4 | +import edq.core.log |
7 | 5 |
|
8 | | -LEVELS: list[str] = [ |
9 | | - 'TRACE', |
10 | | - logging.getLevelName(logging.DEBUG), |
11 | | - logging.getLevelName(logging.INFO), |
12 | | - logging.getLevelName(logging.WARNING), |
13 | | - logging.getLevelName(logging.ERROR), |
14 | | - logging.getLevelName(logging.CRITICAL), |
| 6 | +WARN_LOGGERS: typing.List[str] = [ |
| 7 | + "PIL", |
15 | 8 | ] |
16 | | - |
17 | | -def init(level: str = DEFAULT_LOGGING_LEVEL, log_format: str = DEFAULT_LOGGING_FORMAT, **kwargs: typing.Any) -> None: |
18 | | - """ |
19 | | - Initialize or re-initialize the logging infrastructure. |
20 | | - """ |
21 | | - |
22 | | - # Add trace. |
23 | | - _add_logging_level('TRACE', logging.DEBUG - 5) |
24 | | - |
25 | | - logging.basicConfig(level = level, format = log_format, force = True) |
26 | | - |
27 | | - # Ignore logging from third-party libraries. |
28 | | - logging.getLogger("PIL").setLevel(logging.WARNING) |
| 9 | +""" Loggers (usually third-party) to move up to warning on init. """ |
29 | 10 |
|
30 | 11 | def set_cli_args(parser: argparse.ArgumentParser) -> argparse.ArgumentParser: |
31 | 12 | """ |
32 | 13 | Set common CLI arguments. |
33 | 14 | This is a sibling to init_from_args(), as the arguments set here can be interpreted there. |
34 | 15 | """ |
35 | 16 |
|
36 | | - parser.add_argument('--log-level', dest = 'log_level', |
37 | | - action = 'store', type = str, default = logging.getLevelName(logging.INFO), |
38 | | - choices = LEVELS, |
39 | | - help = 'Set the logging level (default: %(default)s).') |
40 | | - |
41 | | - parser.add_argument('--quiet', dest = 'quiet', |
42 | | - action = 'store_true', default = False, |
43 | | - help = 'Set the logging level to warning (overrides --log-level) (default: %(default)s).') |
44 | | - |
45 | | - parser.add_argument('--debug', dest = 'debug', |
46 | | - action = 'store_true', default = False, |
47 | | - help = 'Set the logging level to debug (overrides --log-level and --quiet) (default: %(default)s).') |
48 | | - |
| 17 | + edq.core.log.set_cli_args(parser, {}) |
49 | 18 | return parser |
50 | 19 |
|
51 | | -def init_from_args(args: argparse.Namespace) -> argparse.Namespace: |
| 20 | +def init_from_args(parser: argparse.ArgumentParser, args: argparse.Namespace) -> argparse.Namespace: |
52 | 21 | """ |
53 | 22 | Take in args from a parser that was passed to set_cli_args(), |
54 | 23 | and call init() with the appropriate arguments. |
55 | 24 | """ |
56 | 25 |
|
57 | | - level = args.log_level |
58 | | - |
59 | | - if (args.quiet): |
60 | | - level = logging.getLevelName(logging.WARNING) |
61 | | - |
62 | | - if (args.debug): |
63 | | - level = logging.getLevelName(logging.DEBUG) |
64 | | - |
65 | | - init(level) |
66 | | - |
| 26 | + edq.core.log.init_from_args(parser, args, {}) |
67 | 27 | return args |
68 | 28 |
|
69 | | -def _add_logging_level(level_name: str, level_number: int, method_name: str | None = None) -> None: |
70 | | - """ |
71 | | - Add a new logging level. |
72 | | -
|
73 | | - See https://stackoverflow.com/questions/2183233/how-to-add-a-custom-loglevel-to-pythons-logging-facility/35804945#35804945 . |
74 | | - """ |
75 | | - |
76 | | - if (method_name is None): |
77 | | - method_name = level_name.lower() |
78 | | - |
79 | | - # Level has already been defined. |
80 | | - if hasattr(logging, level_name): |
81 | | - return |
82 | | - |
83 | | - def log_for_level(self: typing.Any, message: str, *args: typing.Any, **kwargs: typing.Any) -> None: |
84 | | - if self.isEnabledFor(level_number): |
85 | | - self._log(level_number, message, args, **kwargs) |
86 | | - |
87 | | - def log_to_root(message: str, *args: typing.Any, **kwargs: typing.Any) -> None: |
88 | | - logging.log(level_number, message, *args, **kwargs) |
89 | | - |
90 | | - logging.addLevelName(level_number, level_name) |
91 | | - setattr(logging, level_name, level_number) |
92 | | - setattr(logging.getLoggerClass(), method_name, log_for_level) |
93 | | - setattr(logging, method_name, log_to_root) |
94 | | - |
95 | 29 | # Load the default logging when this module is loaded. |
96 | | -init() |
| 30 | +edq.core.log.init(warn_loggers = WARN_LOGGERS) |
0 commit comments