loggery 0.3.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- loggery-0.3.1/LICENSE +65 -0
- loggery-0.3.1/PKG-INFO +135 -0
- loggery-0.3.1/README.md +118 -0
- loggery-0.3.1/pyproject.toml +46 -0
- loggery-0.3.1/src/loggery/__init__.py +187 -0
- loggery-0.3.1/src/loggery/py.typed +0 -0
loggery-0.3.1/LICENSE
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
|
2
|
+
|
|
3
|
+
Version 3, 29 June 2007
|
|
4
|
+
|
|
5
|
+
Copyright © 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
|
6
|
+
|
|
7
|
+
Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
|
|
8
|
+
|
|
9
|
+
This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below.
|
|
10
|
+
0. Additional Definitions.
|
|
11
|
+
|
|
12
|
+
As used herein, “this License” refers to version 3 of the GNU Lesser General Public License, and the “GNU GPL” refers to version 3 of the GNU General Public License.
|
|
13
|
+
|
|
14
|
+
“The Library” refers to a covered work governed by this License, other than an Application or a Combined Work as defined below.
|
|
15
|
+
|
|
16
|
+
An “Application” is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library.
|
|
17
|
+
|
|
18
|
+
A “Combined Work” is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the “Linked Version”.
|
|
19
|
+
|
|
20
|
+
The “Minimal Corresponding Source” for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version.
|
|
21
|
+
|
|
22
|
+
The “Corresponding Application Code” for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work.
|
|
23
|
+
1. Exception to Section 3 of the GNU GPL.
|
|
24
|
+
|
|
25
|
+
You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL.
|
|
26
|
+
2. Conveying Modified Versions.
|
|
27
|
+
|
|
28
|
+
If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version:
|
|
29
|
+
|
|
30
|
+
a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or
|
|
31
|
+
b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy.
|
|
32
|
+
|
|
33
|
+
3. Object Code Incorporating Material from Library Header Files.
|
|
34
|
+
|
|
35
|
+
The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following:
|
|
36
|
+
|
|
37
|
+
a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License.
|
|
38
|
+
b) Accompany the object code with a copy of the GNU GPL and this license document.
|
|
39
|
+
|
|
40
|
+
4. Combined Works.
|
|
41
|
+
|
|
42
|
+
You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following:
|
|
43
|
+
|
|
44
|
+
a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License.
|
|
45
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license document.
|
|
46
|
+
c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document.
|
|
47
|
+
d) Do one of the following:
|
|
48
|
+
0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.
|
|
49
|
+
1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version.
|
|
50
|
+
e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.)
|
|
51
|
+
|
|
52
|
+
5. Combined Libraries.
|
|
53
|
+
|
|
54
|
+
You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following:
|
|
55
|
+
|
|
56
|
+
a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License.
|
|
57
|
+
b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work.
|
|
58
|
+
|
|
59
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
|
60
|
+
|
|
61
|
+
The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
|
|
62
|
+
|
|
63
|
+
Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation.
|
|
64
|
+
|
|
65
|
+
If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.
|
loggery-0.3.1/PKG-INFO
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: loggery
|
|
3
|
+
Version: 0.3.1
|
|
4
|
+
Summary: Configure logging with reduced noise from spammy modules with easy argparse support
|
|
5
|
+
License: LGPL-3.0-or-later
|
|
6
|
+
Author: Erskin Cherry
|
|
7
|
+
Author-email: erskin@eldritch.org
|
|
8
|
+
Requires-Python: >=3.13
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)
|
|
13
|
+
Project-URL: Homepage, https://gitlab.com/frobnic8/loggery
|
|
14
|
+
Project-URL: Repository, https://gitlab.com/frobnic8/loggery
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
logsetup
|
|
18
|
+
========
|
|
19
|
+
*Easy and standardized logging for Python*
|
|
20
|
+
|
|
21
|
+
Log Format
|
|
22
|
+
----------
|
|
23
|
+
This package configures logging with the following format:
|
|
24
|
+
|
|
25
|
+
"{asctime} [{levelname}] {name} - {message}"
|
|
26
|
+
|
|
27
|
+
* asctime - Log message timestamp consisting of date and time (with ms) separated by a space
|
|
28
|
+
* levelname - Log level of the message
|
|
29
|
+
* name - Logger name
|
|
30
|
+
* message - Body of the log message
|
|
31
|
+
|
|
32
|
+
This produces log messages such as this:
|
|
33
|
+
|
|
34
|
+
2022-04-29 10:38:52,315 [INFO] pyspam-app - Slicing 27 cans of spam into 6 slices each for a total of 162 slices.
|
|
35
|
+
|
|
36
|
+
Basic Usage
|
|
37
|
+
-----------
|
|
38
|
+
For command line tools, after creating your parser with `argparse`, pass it to `add_logging_args()`. Then, after parsing
|
|
39
|
+
arguments but before any logging, call `configure_logging_from_args(args.verbose, args.quiet, args.log_stdout)`:
|
|
40
|
+
|
|
41
|
+
import argparse
|
|
42
|
+
import logsetup
|
|
43
|
+
|
|
44
|
+
parser = argparse.ArgumentParser()
|
|
45
|
+
|
|
46
|
+
... build your parser ...
|
|
47
|
+
|
|
48
|
+
logsetup.add_logging_args(parser)
|
|
49
|
+
args = parser.parse_args()
|
|
50
|
+
logsetup.configure_logging_from_args(args.verbose, args.quiet, args.log_stdout)
|
|
51
|
+
|
|
52
|
+
This will set logging levels as requested by the command line options, with known external modules that would otherwise
|
|
53
|
+
flood the logs to be set slightly less verbose. The specific list is in `logsetup.SPAMMY_LOGGERS` and can be
|
|
54
|
+
edited before calling any configure logging function.
|
|
55
|
+
|
|
56
|
+
The default log levels in `logsetup.DEFAULT_LOG_LEVEL` and `logsetup.DEFAULT_SPAMMY_LOG_LEVEL` can also be modified, if
|
|
57
|
+
desired. These do not support custom logging levels, however.
|
|
58
|
+
|
|
59
|
+
You may also pass a `logger_name` as a fourth argument to `configure_logging_from_args()` if you don't want to use the root
|
|
60
|
+
logger. However, if your code is the main execution thread, you should generally configure the root logger so that all output
|
|
61
|
+
uses the same log format.
|
|
62
|
+
|
|
63
|
+
Finally, you can also call `configure_logging()` with specific log levels directly if needed.
|
|
64
|
+
|
|
65
|
+
Once configured, each module or scripts should generate log messages with the default logger for their name:
|
|
66
|
+
|
|
67
|
+
import logging
|
|
68
|
+
LOG = logging.getLogger(__name__)
|
|
69
|
+
|
|
70
|
+
Then just write to the log using that logger:
|
|
71
|
+
|
|
72
|
+
LOG.debug('This is a debug level log message.')
|
|
73
|
+
LOG.info('This is an info level log message.')
|
|
74
|
+
LOG.warning('This is a warning level log message.')
|
|
75
|
+
LOG.error('This is an error level log message.')
|
|
76
|
+
LOG.critical('This is a critical level log message.')
|
|
77
|
+
|
|
78
|
+
Design Overview
|
|
79
|
+
---------------
|
|
80
|
+
The core assumptions are that logging should be easy to setup with reasonable defaults, configurable with command line options
|
|
81
|
+
at runtime, and allow fine control over the volume of information output.
|
|
82
|
+
|
|
83
|
+
To support this last point, there are modules that are commonly used and provide useful log information, but are also verbose
|
|
84
|
+
when set to more detailed log levels despite usually not being the log messages you are interested in. The requests module is a
|
|
85
|
+
great example of this. Debug level logs are verbose and produce tons of information, but the code itself is rarely the source
|
|
86
|
+
of issues. Setting a general log level of debug means seeing both the debug messages of the problematic code and modules like
|
|
87
|
+
requests, which just adds more messages to wade through that aren't helpful.
|
|
88
|
+
|
|
89
|
+
Because of this, logsetup has a list of loggers which are known to be both reliable and noisy. When requesting more verbose
|
|
90
|
+
log information, these "spammy" loggers trail behind the general log level by two steps. This causes more relevant logs to be
|
|
91
|
+
displayed without the spammy logger information unless even more verbosity is requested.
|
|
92
|
+
|
|
93
|
+
In practice, with the default settings, it results in the following level of log output per verbose flag given:
|
|
94
|
+
|
|
95
|
+
| Verbosity | General Log Level | Spammy Logger Level |
|
|
96
|
+
|-----------|:-----------------:|:-------------------:|
|
|
97
|
+
| (none) | WARNING | WARNING |
|
|
98
|
+
| `-v` | INFO | WARNING |
|
|
99
|
+
| `-vv` | DEBUG | WARNING |
|
|
100
|
+
| `-vvv` | DEBUG | INFO |
|
|
101
|
+
| `-vvvv` | DEBUG | DEBUG |
|
|
102
|
+
|
|
103
|
+
Configuration
|
|
104
|
+
-------------
|
|
105
|
+
The default log levels in `logsetup.DEFAULT_LOG_LEVEL` and `logsetup.DEFAULT_SPAMMY_LOG_LEVEL` are set to
|
|
106
|
+
WARNING and can be modified, if desired. Custom logging levels, however, are not supported.
|
|
107
|
+
|
|
108
|
+
The set of loggers considered spammy is an iterable in `logsetup.SPAMMY_LOGGERS` and can also be modified or replaced.
|
|
109
|
+
|
|
110
|
+
Finally, while not recommended, the log format can be modified by changing the value in `logsetup._LOG_FORMAT`. This
|
|
111
|
+
uses the curly brace style formatting. Note this specific attribute is internal may undergo breaking changes in the future.
|
|
112
|
+
The default format is: `"{asctime} [{levelname}] {name} - {message}"`
|
|
113
|
+
|
|
114
|
+
All configuration changes should be made before calling any configuration functions.
|
|
115
|
+
|
|
116
|
+
Function Overview
|
|
117
|
+
-----------------
|
|
118
|
+
For full details please see the source code and function documentation strings.
|
|
119
|
+
|
|
120
|
+
### `add_logging_args(parser: argparse.ArgumentParser)`
|
|
121
|
+
|
|
122
|
+
Adds the `--verbose`, `--quiet`, and `--log-stdout` options to the given argument parser. These arguments are used by
|
|
123
|
+
`configure_logging_from_args()`.
|
|
124
|
+
|
|
125
|
+
### `configure_logging_from_args(verbose: int = 0, quiet: int = 0, log_stdout: bool = False, logger_name: str = None)`
|
|
126
|
+
|
|
127
|
+
Configures the specified logger, typically the root logger, from the given number of verbose and quiet flags, and, when
|
|
128
|
+
increaing verbosity, sets the log level for known spammy loggers to trail behind by two log levels.
|
|
129
|
+
|
|
130
|
+
### `configure_logging(log_level: int = None, spammy_log_level: int = None, log_stdout: bool = False, logger_name: str = None)`
|
|
131
|
+
|
|
132
|
+
For the given logger, typically the root logger, overwrites the existing configuration to use the specified level and
|
|
133
|
+
standard format. Also sets the spammy loggers to their specified level. This function is used by
|
|
134
|
+
`configure_logging_from_args()` and can alternately be called directly if needed.
|
|
135
|
+
|
loggery-0.3.1/README.md
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
logsetup
|
|
2
|
+
========
|
|
3
|
+
*Easy and standardized logging for Python*
|
|
4
|
+
|
|
5
|
+
Log Format
|
|
6
|
+
----------
|
|
7
|
+
This package configures logging with the following format:
|
|
8
|
+
|
|
9
|
+
"{asctime} [{levelname}] {name} - {message}"
|
|
10
|
+
|
|
11
|
+
* asctime - Log message timestamp consisting of date and time (with ms) separated by a space
|
|
12
|
+
* levelname - Log level of the message
|
|
13
|
+
* name - Logger name
|
|
14
|
+
* message - Body of the log message
|
|
15
|
+
|
|
16
|
+
This produces log messages such as this:
|
|
17
|
+
|
|
18
|
+
2022-04-29 10:38:52,315 [INFO] pyspam-app - Slicing 27 cans of spam into 6 slices each for a total of 162 slices.
|
|
19
|
+
|
|
20
|
+
Basic Usage
|
|
21
|
+
-----------
|
|
22
|
+
For command line tools, after creating your parser with `argparse`, pass it to `add_logging_args()`. Then, after parsing
|
|
23
|
+
arguments but before any logging, call `configure_logging_from_args(args.verbose, args.quiet, args.log_stdout)`:
|
|
24
|
+
|
|
25
|
+
import argparse
|
|
26
|
+
import logsetup
|
|
27
|
+
|
|
28
|
+
parser = argparse.ArgumentParser()
|
|
29
|
+
|
|
30
|
+
... build your parser ...
|
|
31
|
+
|
|
32
|
+
logsetup.add_logging_args(parser)
|
|
33
|
+
args = parser.parse_args()
|
|
34
|
+
logsetup.configure_logging_from_args(args.verbose, args.quiet, args.log_stdout)
|
|
35
|
+
|
|
36
|
+
This will set logging levels as requested by the command line options, with known external modules that would otherwise
|
|
37
|
+
flood the logs to be set slightly less verbose. The specific list is in `logsetup.SPAMMY_LOGGERS` and can be
|
|
38
|
+
edited before calling any configure logging function.
|
|
39
|
+
|
|
40
|
+
The default log levels in `logsetup.DEFAULT_LOG_LEVEL` and `logsetup.DEFAULT_SPAMMY_LOG_LEVEL` can also be modified, if
|
|
41
|
+
desired. These do not support custom logging levels, however.
|
|
42
|
+
|
|
43
|
+
You may also pass a `logger_name` as a fourth argument to `configure_logging_from_args()` if you don't want to use the root
|
|
44
|
+
logger. However, if your code is the main execution thread, you should generally configure the root logger so that all output
|
|
45
|
+
uses the same log format.
|
|
46
|
+
|
|
47
|
+
Finally, you can also call `configure_logging()` with specific log levels directly if needed.
|
|
48
|
+
|
|
49
|
+
Once configured, each module or scripts should generate log messages with the default logger for their name:
|
|
50
|
+
|
|
51
|
+
import logging
|
|
52
|
+
LOG = logging.getLogger(__name__)
|
|
53
|
+
|
|
54
|
+
Then just write to the log using that logger:
|
|
55
|
+
|
|
56
|
+
LOG.debug('This is a debug level log message.')
|
|
57
|
+
LOG.info('This is an info level log message.')
|
|
58
|
+
LOG.warning('This is a warning level log message.')
|
|
59
|
+
LOG.error('This is an error level log message.')
|
|
60
|
+
LOG.critical('This is a critical level log message.')
|
|
61
|
+
|
|
62
|
+
Design Overview
|
|
63
|
+
---------------
|
|
64
|
+
The core assumptions are that logging should be easy to setup with reasonable defaults, configurable with command line options
|
|
65
|
+
at runtime, and allow fine control over the volume of information output.
|
|
66
|
+
|
|
67
|
+
To support this last point, there are modules that are commonly used and provide useful log information, but are also verbose
|
|
68
|
+
when set to more detailed log levels despite usually not being the log messages you are interested in. The requests module is a
|
|
69
|
+
great example of this. Debug level logs are verbose and produce tons of information, but the code itself is rarely the source
|
|
70
|
+
of issues. Setting a general log level of debug means seeing both the debug messages of the problematic code and modules like
|
|
71
|
+
requests, which just adds more messages to wade through that aren't helpful.
|
|
72
|
+
|
|
73
|
+
Because of this, logsetup has a list of loggers which are known to be both reliable and noisy. When requesting more verbose
|
|
74
|
+
log information, these "spammy" loggers trail behind the general log level by two steps. This causes more relevant logs to be
|
|
75
|
+
displayed without the spammy logger information unless even more verbosity is requested.
|
|
76
|
+
|
|
77
|
+
In practice, with the default settings, it results in the following level of log output per verbose flag given:
|
|
78
|
+
|
|
79
|
+
| Verbosity | General Log Level | Spammy Logger Level |
|
|
80
|
+
|-----------|:-----------------:|:-------------------:|
|
|
81
|
+
| (none) | WARNING | WARNING |
|
|
82
|
+
| `-v` | INFO | WARNING |
|
|
83
|
+
| `-vv` | DEBUG | WARNING |
|
|
84
|
+
| `-vvv` | DEBUG | INFO |
|
|
85
|
+
| `-vvvv` | DEBUG | DEBUG |
|
|
86
|
+
|
|
87
|
+
Configuration
|
|
88
|
+
-------------
|
|
89
|
+
The default log levels in `logsetup.DEFAULT_LOG_LEVEL` and `logsetup.DEFAULT_SPAMMY_LOG_LEVEL` are set to
|
|
90
|
+
WARNING and can be modified, if desired. Custom logging levels, however, are not supported.
|
|
91
|
+
|
|
92
|
+
The set of loggers considered spammy is an iterable in `logsetup.SPAMMY_LOGGERS` and can also be modified or replaced.
|
|
93
|
+
|
|
94
|
+
Finally, while not recommended, the log format can be modified by changing the value in `logsetup._LOG_FORMAT`. This
|
|
95
|
+
uses the curly brace style formatting. Note this specific attribute is internal may undergo breaking changes in the future.
|
|
96
|
+
The default format is: `"{asctime} [{levelname}] {name} - {message}"`
|
|
97
|
+
|
|
98
|
+
All configuration changes should be made before calling any configuration functions.
|
|
99
|
+
|
|
100
|
+
Function Overview
|
|
101
|
+
-----------------
|
|
102
|
+
For full details please see the source code and function documentation strings.
|
|
103
|
+
|
|
104
|
+
### `add_logging_args(parser: argparse.ArgumentParser)`
|
|
105
|
+
|
|
106
|
+
Adds the `--verbose`, `--quiet`, and `--log-stdout` options to the given argument parser. These arguments are used by
|
|
107
|
+
`configure_logging_from_args()`.
|
|
108
|
+
|
|
109
|
+
### `configure_logging_from_args(verbose: int = 0, quiet: int = 0, log_stdout: bool = False, logger_name: str = None)`
|
|
110
|
+
|
|
111
|
+
Configures the specified logger, typically the root logger, from the given number of verbose and quiet flags, and, when
|
|
112
|
+
increaing verbosity, sets the log level for known spammy loggers to trail behind by two log levels.
|
|
113
|
+
|
|
114
|
+
### `configure_logging(log_level: int = None, spammy_log_level: int = None, log_stdout: bool = False, logger_name: str = None)`
|
|
115
|
+
|
|
116
|
+
For the given logger, typically the root logger, overwrites the existing configuration to use the specified level and
|
|
117
|
+
standard format. Also sets the spammy loggers to their specified level. This function is used by
|
|
118
|
+
`configure_logging_from_args()` and can alternately be called directly if needed.
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "loggery"
|
|
3
|
+
version = "0.3.1"
|
|
4
|
+
description = "Configure logging with reduced noise from spammy modules with easy argparse support"
|
|
5
|
+
authors = [
|
|
6
|
+
{name = "Erskin Cherry", email = "erskin@eldritch.org"}
|
|
7
|
+
]
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
requires-python = ">=3.13"
|
|
10
|
+
dependencies = [
|
|
11
|
+
]
|
|
12
|
+
classifiers = [
|
|
13
|
+
"Programming Language :: Python :: 3",
|
|
14
|
+
"Operating System :: OS Independent",
|
|
15
|
+
"Intended Audience :: Developers",
|
|
16
|
+
"License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)",
|
|
17
|
+
]
|
|
18
|
+
license = "LGPL-3.0-or-later"
|
|
19
|
+
license-files = ["LICENSE"]
|
|
20
|
+
[project.urls]
|
|
21
|
+
Homepage = "https://gitlab.com/frobnic8/loggery"
|
|
22
|
+
Repository = "https://gitlab.com/frobnic8/loggery"
|
|
23
|
+
|
|
24
|
+
[tool.poetry]
|
|
25
|
+
packages = [{include = "loggery", from = "src"}]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
[tool.poetry.group.dev.dependencies]
|
|
29
|
+
pytest-mock = "^3.14.0"
|
|
30
|
+
|
|
31
|
+
[build-system]
|
|
32
|
+
requires = ["poetry-core>=2.0.0,<3.0.0"]
|
|
33
|
+
build-backend = "poetry.core.masonry.api"
|
|
34
|
+
|
|
35
|
+
[tool.pylint]
|
|
36
|
+
max-line-length = 100
|
|
37
|
+
|
|
38
|
+
[tool.pytest.ini_options]
|
|
39
|
+
pythonpath = [
|
|
40
|
+
"src"
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
[tool.mypy]
|
|
45
|
+
mypy_path = "src/:tests/"
|
|
46
|
+
files = ["src/", "tests/"]
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
"""loggery - Easy and standardized logging for Python
|
|
2
|
+
|
|
3
|
+
For command line tools, after creating your parser with `argparse`, pass it to `add_logging_args()`.
|
|
4
|
+
Then, after parsing arguments but before any logging, call
|
|
5
|
+
`configure_logging_from_args(args.verbose, args.quiet, args.log_stdout)`:
|
|
6
|
+
|
|
7
|
+
import argparse
|
|
8
|
+
import loggery
|
|
9
|
+
|
|
10
|
+
parser = argparse.ArgumentParser()
|
|
11
|
+
|
|
12
|
+
... build your parser ...
|
|
13
|
+
|
|
14
|
+
loggery.add_logging_args(parser)
|
|
15
|
+
args = parser.parse_args()
|
|
16
|
+
loggery.configure_logging_from_args(args.verbose, args.quiet, args.log_stdout)
|
|
17
|
+
|
|
18
|
+
This will set logging levels as requested by the command line options, with known external modules
|
|
19
|
+
that would otherwise flood the logs to be set slightly less verbose. The specific list is in
|
|
20
|
+
`loggery.SPAMMY_LOGGERS` and can be edited before calling any configure logging function.
|
|
21
|
+
|
|
22
|
+
The default log levels in `loggery.DEFAULT_LOG_LEVEL` and `loggery.DEFAULT_SPAMMY_LOG_LEVEL` can
|
|
23
|
+
also be modified, if desired. These do not support custom logging levels, however.
|
|
24
|
+
|
|
25
|
+
You may also pass a `logger_name` as a fourth argument to `configure_logging_from_args()` if you
|
|
26
|
+
don't want to use the root logger.
|
|
27
|
+
|
|
28
|
+
Finally, you can also call `configure_logging()` with specific log levels directly if needed.
|
|
29
|
+
|
|
30
|
+
Once configured, each module or scripts should generate log messages with the default logger for
|
|
31
|
+
their name:
|
|
32
|
+
|
|
33
|
+
import logging
|
|
34
|
+
LOG = logging.getLogger(__name__)
|
|
35
|
+
|
|
36
|
+
Then just write to the log using that logger:
|
|
37
|
+
|
|
38
|
+
LOG.debug('This is a debug level log message.')
|
|
39
|
+
LOG.info('This is an info level log message.')
|
|
40
|
+
LOG.warning('This is a warning level log message.')
|
|
41
|
+
LOG.error('This is an error level log message.')
|
|
42
|
+
LOG.critical('This is a critical level log message.')
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
import sys
|
|
46
|
+
import argparse
|
|
47
|
+
import importlib.metadata
|
|
48
|
+
import logging
|
|
49
|
+
|
|
50
|
+
__version__ = importlib.metadata.version("loggery")
|
|
51
|
+
__author__ = "erskin@eldritch.org"
|
|
52
|
+
|
|
53
|
+
# A set of loggers which we generally DON'T want to be quite as verbose as our main logger.
|
|
54
|
+
# By default, if we are being more verbose, these are set to one log level less verbose than the
|
|
55
|
+
# main log. If needed, you can add or remove items from this list before calling and configure
|
|
56
|
+
# logging function.
|
|
57
|
+
SPAMMY_LOGGERS = [
|
|
58
|
+
"google.auth.transport.requests",
|
|
59
|
+
"urllib3.util.retry",
|
|
60
|
+
"boxsdk.auth.oauth2",
|
|
61
|
+
"boxsdk.network.default_network",
|
|
62
|
+
"urllib3.connectionpool",
|
|
63
|
+
"gql.transport.requests",
|
|
64
|
+
"requests",
|
|
65
|
+
]
|
|
66
|
+
|
|
67
|
+
DEFAULT_LOG_LEVEL = logging.WARNING
|
|
68
|
+
DEFAULT_SPAMMY_LOG_LEVEL = logging.WARNING
|
|
69
|
+
|
|
70
|
+
_LOG_LEVELS = [
|
|
71
|
+
logging.CRITICAL,
|
|
72
|
+
logging.ERROR,
|
|
73
|
+
logging.WARNING,
|
|
74
|
+
logging.INFO,
|
|
75
|
+
logging.DEBUG,
|
|
76
|
+
]
|
|
77
|
+
|
|
78
|
+
_LOG_FORMAT = "{asctime} [{levelname}] {name} - {message}"
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def add_logging_args(parser: argparse.ArgumentParser):
|
|
82
|
+
"""Add --verbose, --quiet, --log-stdout arguments to the given argument parser."""
|
|
83
|
+
|
|
84
|
+
parser.add_argument(
|
|
85
|
+
"-v",
|
|
86
|
+
"--verbose",
|
|
87
|
+
action="count",
|
|
88
|
+
default=0,
|
|
89
|
+
help=(
|
|
90
|
+
"increase level of feedback output. Use -vv for even more detail. Log level defaults "
|
|
91
|
+
"to " + repr(logging.getLevelName(DEFAULT_LOG_LEVEL))
|
|
92
|
+
),
|
|
93
|
+
)
|
|
94
|
+
parser.add_argument(
|
|
95
|
+
"-q",
|
|
96
|
+
"--quiet",
|
|
97
|
+
action="count",
|
|
98
|
+
default=0,
|
|
99
|
+
help="decrease level of feedback output. Use -qq for even less detail",
|
|
100
|
+
)
|
|
101
|
+
parser.add_argument(
|
|
102
|
+
"--log-stdout",
|
|
103
|
+
action="store_true",
|
|
104
|
+
help="direct all logging to standard out instead of standard error",
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def configure_logging_from_args(
|
|
109
|
+
verbose: int = 0, quiet: int = 0, log_stdout: bool = False, logger_name: str | None = None
|
|
110
|
+
):
|
|
111
|
+
"""Configure logging based on the number of verbose and quiet flags and the log_stdout flag
|
|
112
|
+
added by add_logging_args().
|
|
113
|
+
|
|
114
|
+
Spammy loggers are those which, at more verbose log levels, are usually more noisy than useful.
|
|
115
|
+
|
|
116
|
+
Known spammy loggers in `loggery.SPAMMY_LOGGERS` are set up to two levels less verbose than
|
|
117
|
+
the main logger, but only when we are increasing the default verbosity.
|
|
118
|
+
|
|
119
|
+
With the standard defaults, this means that as verbosity goes up, the main logger will go from
|
|
120
|
+
WARNING to INFO, then DEBUG, and only then will the spammy loggers be set INFO, and then DEBUG.
|
|
121
|
+
But, as verbosity goes down, both the main and spammy loggers will be set to ERROR, and then
|
|
122
|
+
CRITICAL together.
|
|
123
|
+
"""
|
|
124
|
+
|
|
125
|
+
# Set the log verbosity to the default, plus the count of verbose flags minus the count of
|
|
126
|
+
# quiet flags.
|
|
127
|
+
default_index = _LOG_LEVELS.index(DEFAULT_LOG_LEVEL)
|
|
128
|
+
verbosity = default_index + verbose - quiet
|
|
129
|
+
|
|
130
|
+
# Set the spammy loggers to track the verbosity of the main loggers to start.
|
|
131
|
+
spammy_verbosity = verbosity
|
|
132
|
+
if verbosity > default_index:
|
|
133
|
+
# But if we are being more verbose than normal, keep the spammy loggers quieter than the
|
|
134
|
+
# main ones, though never more quiet than the default spammy level.
|
|
135
|
+
default_spammy_index = _LOG_LEVELS.index(DEFAULT_SPAMMY_LOG_LEVEL)
|
|
136
|
+
spammy_verbosity = max(verbosity - 2, default_spammy_index)
|
|
137
|
+
|
|
138
|
+
# Cap the verbosity and spammy verbosity to indexes of LOG_LEVELS.
|
|
139
|
+
verbosity = min(verbosity, len(_LOG_LEVELS) - 1)
|
|
140
|
+
verbosity = max(verbosity, 0)
|
|
141
|
+
spammy_verbosity = min(spammy_verbosity, len(_LOG_LEVELS) - 1)
|
|
142
|
+
spammy_verbosity = max(spammy_verbosity, 0)
|
|
143
|
+
|
|
144
|
+
configure_logging(
|
|
145
|
+
_LOG_LEVELS[verbosity], _LOG_LEVELS[spammy_verbosity], log_stdout, logger_name
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def configure_logging(
|
|
150
|
+
log_level: int | None = None,
|
|
151
|
+
spammy_log_level: int | None = None,
|
|
152
|
+
log_stdout: bool = False,
|
|
153
|
+
logger_name: str | None = None,
|
|
154
|
+
):
|
|
155
|
+
"""Setup the specified logger to the given level and the standard format, and the level of
|
|
156
|
+
known spammy loggers.
|
|
157
|
+
|
|
158
|
+
This will remove any existing log handlers.
|
|
159
|
+
"""
|
|
160
|
+
|
|
161
|
+
if not log_level:
|
|
162
|
+
log_level = DEFAULT_LOG_LEVEL
|
|
163
|
+
|
|
164
|
+
if not spammy_log_level:
|
|
165
|
+
spammy_log_level = DEFAULT_SPAMMY_LOG_LEVEL
|
|
166
|
+
|
|
167
|
+
logger = logging.getLogger(logger_name)
|
|
168
|
+
logger.setLevel(log_level)
|
|
169
|
+
|
|
170
|
+
# Remove any existing handlers so we overwrite the configuration.
|
|
171
|
+
for handler in logger.handlers[:]:
|
|
172
|
+
logger.removeHandler(handler)
|
|
173
|
+
handler.close()
|
|
174
|
+
|
|
175
|
+
# Setup the message handler and formatter.
|
|
176
|
+
stream = sys.stdout if log_stdout else sys.stderr
|
|
177
|
+
handler = logging.StreamHandler(stream)
|
|
178
|
+
handler.setLevel(log_level)
|
|
179
|
+
formatter = logging.Formatter(_LOG_FORMAT, style="{")
|
|
180
|
+
handler.setFormatter(formatter)
|
|
181
|
+
|
|
182
|
+
# Add the handler to the logger.
|
|
183
|
+
logger.addHandler(handler)
|
|
184
|
+
|
|
185
|
+
# Set the known spammy loggers to the request log level.
|
|
186
|
+
for spammy_logger_name in SPAMMY_LOGGERS:
|
|
187
|
+
logging.getLogger(spammy_logger_name).setLevel(spammy_log_level)
|
|
File without changes
|