wai-logging 0.0.3__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.
@@ -0,0 +1,20 @@
1
+ Changelog
2
+ =========
3
+
4
+ 0.0.3 (2025-01-14)
5
+ ------------------
6
+
7
+ - using underscore in project name now
8
+
9
+
10
+ 0.0.2 (2025-01-14)
11
+ ------------------
12
+
13
+ - `init_logging` now supports specifying `stream` or `filename` or `handlers` as well as the `log_format`
14
+
15
+
16
+ 0.0.1 (2023-11-30)
17
+ ------------------
18
+
19
+ - initial release
20
+
@@ -0,0 +1,8 @@
1
+ Python library with helper methods for logging.
2
+
3
+ * `init_logging` - initializes the logging with a default level, which can be overridden with an environment variable
4
+ * `set_logging_level` - sets the logging level of a `logging.Logger` instance
5
+ * `str_to_logging_level` - turns a string logging level into a `logging` module one, used by `init_logging` and `set_logging_level`
6
+ * `add_logging_level` - adds an option for the logging level to an `argparse.ArgumentParser` instance
7
+ * `add_logger_name` - adds an option for a custom name for a logger to an `argparse.ArgumentParser` instance
8
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 University of Waikato - Data Mining
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,3 @@
1
+ include DESCRIPTION.rst
2
+ include CHANGES.rst
3
+ include LICENSE
@@ -0,0 +1,43 @@
1
+ Metadata-Version: 2.1
2
+ Name: wai_logging
3
+ Version: 0.0.3
4
+ Summary: Python library with helper methods for logging.
5
+ Home-page: https://github.com/waikato-datamining/wai-logging
6
+ Author: Peter Reutemann
7
+ Author-email: fracpete@waikato.ac.nz
8
+ License: MIT
9
+ Classifier: Development Status :: 4 - Beta
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
12
+ Classifier: Programming Language :: Python :: 3
13
+ License-File: LICENSE
14
+
15
+ Python library with helper methods for logging.
16
+
17
+ * `init_logging` - initializes the logging with a default level, which can be overridden with an environment variable
18
+ * `set_logging_level` - sets the logging level of a `logging.Logger` instance
19
+ * `str_to_logging_level` - turns a string logging level into a `logging` module one, used by `init_logging` and `set_logging_level`
20
+ * `add_logging_level` - adds an option for the logging level to an `argparse.ArgumentParser` instance
21
+ * `add_logger_name` - adds an option for a custom name for a logger to an `argparse.ArgumentParser` instance
22
+
23
+
24
+ Changelog
25
+ =========
26
+
27
+ 0.0.3 (2025-01-14)
28
+ ------------------
29
+
30
+ - using underscore in project name now
31
+
32
+
33
+ 0.0.2 (2025-01-14)
34
+ ------------------
35
+
36
+ - `init_logging` now supports specifying `stream` or `filename` or `handlers` as well as the `log_format`
37
+
38
+
39
+ 0.0.1 (2023-11-30)
40
+ ------------------
41
+
42
+ - initial release
43
+
@@ -0,0 +1,108 @@
1
+ # wai-logging
2
+ Simple helper library for logging.
3
+
4
+
5
+ ## Installation
6
+
7
+ From PyPI:
8
+
9
+ ```bash
10
+ pip install wai_logging
11
+ ```
12
+
13
+ From Github:
14
+
15
+ ```bash
16
+ pip install git+https://github.com/waikato-datamining/wai-logging.git
17
+ ```
18
+
19
+
20
+ ## Methods
21
+
22
+ The following helper methods are available:
23
+
24
+ * `init_logging` - initializes the logging with a default level, which can be overridden with an environment variable
25
+ * `set_logging_level` - sets the logging level of a `logging.Logger` instance
26
+ * `str_to_logging_level` - turns a string logging level into a `logging` module one, used by `init_logging` and `set_logging_level`
27
+ * `add_logging_level` - adds an option for the logging level to an `argparse.ArgumentParser` instance
28
+ * `add_logger_name` - adds an option for a custom name for a logger to an `argparse.ArgumentParser` instance
29
+
30
+
31
+ ## Usage
32
+
33
+ Below are some examples on how to use the aforementioned methods and what the
34
+ output may look like.
35
+
36
+ ### Example 1
37
+
38
+ The following logging setup only outputs on stderr using a simple format without timestamps:
39
+
40
+ ```python
41
+ from wai.logging import init_logging, LOGGING_INFO, set_logging_level
42
+ import logging
43
+
44
+ init_logging(LOGGING_INFO)
45
+ logger = logging.getLogger("my.test")
46
+ set_logging_level(logger, LOGGING_INFO)
47
+ logger.debug("debugging output")
48
+ logger.info("info output")
49
+ logger.warning("warning output")
50
+ ```
51
+
52
+ The output:
53
+
54
+ ```
55
+ INFO:my.test:info output
56
+ WARNING:my.test:warning output
57
+ ```
58
+
59
+ ### Example 2
60
+
61
+ This configuration uses timestamps in its output (`log_format`; see [format strings](https://docs.python.org/3/library/logging.html#logrecord-attributes))
62
+ and logs to a file as well (`filename`):
63
+
64
+ ```python
65
+ from wai.logging import init_logging, LOGGING_INFO, set_logging_level, TIMESTAMP_LOG_FORMAT
66
+ import logging
67
+
68
+ init_logging(LOGGING_INFO, filename="./out.log", log_format=TIMESTAMP_LOG_FORMAT)
69
+ logger = logging.getLogger("my.test")
70
+ set_logging_level(logger, LOGGING_INFO)
71
+ logger.debug("debugging output")
72
+ logger.info("info output")
73
+ logger.warning("warning output")
74
+ ```
75
+
76
+ The output:
77
+
78
+ ```
79
+ 2025-01-14 09:25:22,837 - INFO - my.test - info output
80
+ 2025-01-14 09:25:22,837 - WARNING - my.test - warning output
81
+ ```
82
+
83
+ ### Example 3
84
+
85
+ The following setup uses a [rotating file handler](https://docs.python.org/3/library/logging.handlers.html#logging.handlers.RotatingFileHandler)
86
+ and keeps a maximum of three backups (`backupCount`; log files get a numeric suffix like `.1`).
87
+ A rotation is initiated when the process is restarted and the log file exceeds 100 bytes (`maxBytes`).
88
+
89
+ ```python
90
+ from wai.logging import init_logging, LOGGING_INFO, set_logging_level
91
+ import logging.handlers
92
+
93
+ init_logging(LOGGING_INFO, handlers=[
94
+ logging.StreamHandler(),
95
+ logging.handlers.RotatingFileHandler(filename="./out.log", backupCount=3, maxBytes=100)])
96
+ logger = logging.getLogger("my.test")
97
+ set_logging_level(logger, LOGGING_INFO)
98
+ logger.debug("debugging output")
99
+ logger.info("info output")
100
+ logger.warning("warning output")
101
+ ```
102
+
103
+ The output:
104
+
105
+ ```
106
+ INFO:my.test:info output
107
+ WARNING:my.test:warning output
108
+ ```
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,38 @@
1
+ from setuptools import setup
2
+
3
+
4
+ def _read(f) -> bytes:
5
+ """
6
+ Reads in the content of the file.
7
+ :param f: the file to read
8
+ :type f: str
9
+ :return: the content
10
+ :rtype: str
11
+ """
12
+ return open(f, 'rb').read()
13
+
14
+
15
+ setup(
16
+ name="wai_logging",
17
+ description="Python library with helper methods for logging.",
18
+ long_description=(
19
+ _read('DESCRIPTION.rst') + b'\n' +
20
+ _read('CHANGES.rst')).decode('utf-8'),
21
+ url="https://github.com/waikato-datamining/wai-logging",
22
+ classifiers=[
23
+ 'Development Status :: 4 - Beta',
24
+ 'License :: OSI Approved :: MIT License',
25
+ 'Topic :: Scientific/Engineering :: Mathematics',
26
+ 'Programming Language :: Python :: 3',
27
+ ],
28
+ license='MIT',
29
+ package_dir={
30
+ '': 'src'
31
+ },
32
+ packages=[
33
+ "wai.logging",
34
+ ],
35
+ version="0.0.3",
36
+ author='Peter Reutemann',
37
+ author_email='fracpete@waikato.ac.nz',
38
+ )
@@ -0,0 +1,4 @@
1
+ from ._core import LOGGING_LEVELS, LOGGING_DEBUG, LOGGING_INFO, LOGGING_WARN, LOGGING_WARNING, LOGGING_ERROR, LOGGING_CRITICAL
2
+ from ._core import SIMPLE_LOG_FORMAT, TIMESTAMP_LOG_FORMAT
3
+ from ._core import init_logging, set_logging_level, str_to_logging_level
4
+ from ._argparse import add_logging_level, add_logger_name
@@ -0,0 +1,41 @@
1
+ import argparse
2
+
3
+ from ._core import LOGGING_LEVELS, LOGGING_WARN
4
+
5
+
6
+ def add_logging_level(parser: argparse.ArgumentParser, short_opt: str = "-l", long_opt: str = "--logging_level",
7
+ help_str: str = None):
8
+ """
9
+ Adds an option for the logging level to the parser.
10
+
11
+ :param parser: the parser to append.
12
+ :type parser: argparse.ArgumentParser
13
+ :param short_opt: the short option flag to use (eg -l)
14
+ :type short_opt: str
15
+ :param long_opt: the long option flag to use (eg --logging_level)
16
+ :type long_opt: str
17
+ :param help_str: the help string to use
18
+ :type help_str: str
19
+ """
20
+ if help_str is None:
21
+ help_str = "The logging level to use."
22
+ parser.add_argument(short_opt, long_opt, choices=LOGGING_LEVELS, default=LOGGING_WARN, help=help_str)
23
+
24
+
25
+ def add_logger_name(parser: argparse.ArgumentParser, short_opt: str = "-N", long_opt: str = "--logger_name",
26
+ help_str: str = None):
27
+ """
28
+ Adds an option for the logger name to the parser.
29
+
30
+ :param parser: the parser to append.
31
+ :type parser: argparse.ArgumentParser
32
+ :param short_opt: the short option flag to use (eg -N)
33
+ :type short_opt: str
34
+ :param long_opt: the long option flag to use (eg --logger_name)
35
+ :type long_opt: str
36
+ :param help_str: the help string to use
37
+ :type help_str: str
38
+ """
39
+ if help_str is None:
40
+ help_str = "The custom name to use for the logger"
41
+ parser.add_argument(short_opt, long_opt, type=str, default=None, help=help_str, required=False)
@@ -0,0 +1,97 @@
1
+ import logging
2
+ import os
3
+ from typing import List, Any
4
+
5
+
6
+ LOGGING_DEBUG = "DEBUG"
7
+ LOGGING_INFO = "INFO"
8
+ LOGGING_WARN = "WARN"
9
+ LOGGING_WARNING = "WARNING"
10
+ LOGGING_ERROR = "ERROR"
11
+ LOGGING_CRITICAL = "CRITICAL"
12
+ LOGGING_LEVELS = [
13
+ LOGGING_DEBUG,
14
+ LOGGING_INFO,
15
+ LOGGING_WARNING,
16
+ LOGGING_ERROR,
17
+ LOGGING_CRITICAL,
18
+ ]
19
+
20
+
21
+ SIMPLE_LOG_FORMAT = "%(levelname)s:%(name)s:%(message)s"
22
+
23
+ TIMESTAMP_LOG_FORMAT = "%(asctime)s - %(levelname)s - %(name)s - %(message)s"
24
+
25
+
26
+ def str_to_logging_level(level: str) -> int:
27
+ """
28
+ Turns a logging level string into the corresponding integer constant.
29
+
30
+ :param level: the level to convert
31
+ :type level: str
32
+ :return: the int level
33
+ :rtype: int
34
+ """
35
+ level = level.upper()
36
+ if level == LOGGING_WARN:
37
+ level = LOGGING_WARNING
38
+ if level not in LOGGING_LEVELS:
39
+ raise Exception("Invalid logging level (%s): %s" % ("|".join(LOGGING_LEVELS), level))
40
+ if level == LOGGING_CRITICAL:
41
+ return logging.CRITICAL
42
+ elif level == LOGGING_ERROR:
43
+ return logging.ERROR
44
+ elif level == LOGGING_WARNING:
45
+ return logging.WARNING
46
+ elif level == LOGGING_INFO:
47
+ return logging.INFO
48
+ elif level == LOGGING_DEBUG:
49
+ return logging.DEBUG
50
+ else:
51
+ raise Exception("Unhandled logging level: %s" % level)
52
+
53
+
54
+ def init_logging(default_level: str = LOGGING_WARNING, env_var: str = None, stream: Any = None, filename: str = None,
55
+ handlers: List[Any] = None, log_format: str = SIMPLE_LOG_FORMAT):
56
+ """
57
+ Initializes the logging.
58
+
59
+ :param default_level: the default level to use
60
+ :type default_level: str
61
+ :param env_var: the environment variable to check for a level (overrides the default level), ignored if None
62
+ :type env_var: str
63
+ :param stream: the stream to use, uses stderr if None (default: None); cannot be used together with 'filename'
64
+ :param filename: the filename to log to, ignored if None (default: None); cannot be used together with 'stream'
65
+ :type filename: str
66
+ :param handlers: the explicit handlers to use
67
+ :type handlers: list
68
+ :param log_format: the format string for the logging output
69
+ :type log_format: str
70
+ """
71
+ level = str_to_logging_level(default_level)
72
+ if env_var is not None:
73
+ if os.getenv(env_var) is not None:
74
+ level = str_to_logging_level(os.getenv(env_var))
75
+ if handlers is None:
76
+ handlers = []
77
+ if filename is not None:
78
+ handlers.append(logging.FileHandler(filename))
79
+ handlers.append(logging.StreamHandler(stream=stream))
80
+ else:
81
+ if stream is not None:
82
+ raise Exception("Cannot specify 'handlers' and 'stream' together!")
83
+ if filename is not None:
84
+ raise Exception("Cannot specify 'handlers' and 'filename' together!")
85
+ logging.basicConfig(level=level, handlers=handlers, format=log_format)
86
+
87
+
88
+ def set_logging_level(logger: logging.Logger, level: str):
89
+ """
90
+ Sets the logging level of the logger.
91
+
92
+ :param logger: the logger to update
93
+ :type logger: logging.Logger
94
+ :param level: the level string, see LOGGING_LEVELS
95
+ :type level: str
96
+ """
97
+ logger.setLevel(str_to_logging_level(level))
@@ -0,0 +1,43 @@
1
+ Metadata-Version: 2.1
2
+ Name: wai-logging
3
+ Version: 0.0.3
4
+ Summary: Python library with helper methods for logging.
5
+ Home-page: https://github.com/waikato-datamining/wai-logging
6
+ Author: Peter Reutemann
7
+ Author-email: fracpete@waikato.ac.nz
8
+ License: MIT
9
+ Classifier: Development Status :: 4 - Beta
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
12
+ Classifier: Programming Language :: Python :: 3
13
+ License-File: LICENSE
14
+
15
+ Python library with helper methods for logging.
16
+
17
+ * `init_logging` - initializes the logging with a default level, which can be overridden with an environment variable
18
+ * `set_logging_level` - sets the logging level of a `logging.Logger` instance
19
+ * `str_to_logging_level` - turns a string logging level into a `logging` module one, used by `init_logging` and `set_logging_level`
20
+ * `add_logging_level` - adds an option for the logging level to an `argparse.ArgumentParser` instance
21
+ * `add_logger_name` - adds an option for a custom name for a logger to an `argparse.ArgumentParser` instance
22
+
23
+
24
+ Changelog
25
+ =========
26
+
27
+ 0.0.3 (2025-01-14)
28
+ ------------------
29
+
30
+ - using underscore in project name now
31
+
32
+
33
+ 0.0.2 (2025-01-14)
34
+ ------------------
35
+
36
+ - `init_logging` now supports specifying `stream` or `filename` or `handlers` as well as the `log_format`
37
+
38
+
39
+ 0.0.1 (2023-11-30)
40
+ ------------------
41
+
42
+ - initial release
43
+
@@ -0,0 +1,13 @@
1
+ CHANGES.rst
2
+ DESCRIPTION.rst
3
+ LICENSE
4
+ MANIFEST.in
5
+ README.md
6
+ setup.py
7
+ src/wai/logging/__init__.py
8
+ src/wai/logging/_argparse.py
9
+ src/wai/logging/_core.py
10
+ src/wai_logging.egg-info/PKG-INFO
11
+ src/wai_logging.egg-info/SOURCES.txt
12
+ src/wai_logging.egg-info/dependency_links.txt
13
+ src/wai_logging.egg-info/top_level.txt