hanaro 0.0.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.
- hanaro-0.0.1/LICENSE +21 -0
- hanaro-0.0.1/PKG-INFO +116 -0
- hanaro-0.0.1/README.md +96 -0
- hanaro-0.0.1/hanaro/ConfigFilter.py +25 -0
- hanaro-0.0.1/hanaro/ContextInjectionFilter.py +38 -0
- hanaro-0.0.1/hanaro/QueuedHandler.py +25 -0
- hanaro-0.0.1/hanaro/__init__.py +21 -0
- hanaro-0.0.1/hanaro/utils.py +102 -0
- hanaro-0.0.1/hanaro.egg-info/PKG-INFO +116 -0
- hanaro-0.0.1/hanaro.egg-info/SOURCES.txt +12 -0
- hanaro-0.0.1/hanaro.egg-info/dependency_links.txt +1 -0
- hanaro-0.0.1/hanaro.egg-info/top_level.txt +3 -0
- hanaro-0.0.1/pyproject.toml +37 -0
- hanaro-0.0.1/setup.cfg +4 -0
hanaro-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (C) Shaun Wilson
|
|
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.
|
hanaro-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: hanaro
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A non-invasive `logging` configurator and facilitator.
|
|
5
|
+
Author-email: Shaun Wilson <mrshaunwilson@msn.com>
|
|
6
|
+
Project-URL: Documentation, https://hanaro.readthedocs.io/
|
|
7
|
+
Project-URL: Homepage, https://github.com/wilson0x4d/hanaro
|
|
8
|
+
Project-URL: Repository, https://github.com/wilson0x4d/hanaro.git
|
|
9
|
+
Keywords: logging,configuration,appsettings2,async
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Requires-Python: >=3.12
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Dynamic: license-file
|
|
20
|
+
|
|
21
|
+
`hanaro` (하나로) is a non-invasive `logging` configurator and facilitator for Python.
|
|
22
|
+
|
|
23
|
+
This README is only a high-level introduction to **hanaro**. For more detailed documentation, please view the official docs at [https://hanaro.readthedocs.io](https://hanaro.readthedocs.io).
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
**hanaro** can be installed from pypi through the usual means:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
pip install hanaro
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
Let's try a "learn by example" approach. The following two snippets are the contents of a configuration file that contains a "logging" configuration section, and a Python code file that initializes Python's standard `logging` system using that configuration. This is by no means an exhaustive example, it only intends to touch on the major offerings of **hanaro**.
|
|
36
|
+
|
|
37
|
+
> NOTE: Configuration handling is performed using `appsettings2` which supports json, toml, yaml, xml, command-line args, and environment variables to provide a unified configuration solution. This example, however, uses json because of json's wider familiarity. For the sake of demonstration assume this content is in a file named `appsettings.json`.
|
|
38
|
+
|
|
39
|
+
```json
|
|
40
|
+
{
|
|
41
|
+
"logging": {
|
|
42
|
+
"level": "INFO",
|
|
43
|
+
"format": "[%(asctime)s] %(message)s level=%(levelname)s source=%(name)s %(meta)s",
|
|
44
|
+
"datefmt": "%Y-%m-%dT%H:%M:%S",
|
|
45
|
+
"handlers": [
|
|
46
|
+
{
|
|
47
|
+
"type": "console",
|
|
48
|
+
"level": "DEBUG"
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
"type": "file",
|
|
52
|
+
"level": "DEBUG",
|
|
53
|
+
"path": "logs/",
|
|
54
|
+
"name": "debug.log",
|
|
55
|
+
"max_size": "4KiB",
|
|
56
|
+
"max_count": 10,
|
|
57
|
+
"format": "[%(asctime)s] level=%(levelname)s %(message)s source=\"%(name)s\" func=\"%(funcName)s\" %(meta)s"
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
"type": "custom",
|
|
61
|
+
"canonical": "myapp.mymodule.myhandler",
|
|
62
|
+
"level": "WARNING",
|
|
63
|
+
"format": "msg=\"%(message)s\" level=\"%(levelname)s\" source=\"%(name)s\" func=\"%(funcName)s\" %(meta)s"
|
|
64
|
+
}
|
|
65
|
+
],
|
|
66
|
+
"filters": {
|
|
67
|
+
"asyncio": {
|
|
68
|
+
"level": "WARNING"
|
|
69
|
+
},
|
|
70
|
+
"mysql.connector": {
|
|
71
|
+
"level": "WARNING"
|
|
72
|
+
},
|
|
73
|
+
"urllib3.connectionpool": {
|
|
74
|
+
"level": "WARNING"
|
|
75
|
+
},
|
|
76
|
+
"websockets.client": {
|
|
77
|
+
"level": "WARNING"
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
This code sample is a minimum-viable solution. The `custom` handler above is omitted, but for the sake of demonstration know that `canonical` is the fully-qualified type name of a `logging.Handler` subclass and **hanaro** will create an instance of that class and configure as it does all other handlers.
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
from appsettings2 import getConfiguration
|
|
88
|
+
from hanaro import configureLogging
|
|
89
|
+
import logging
|
|
90
|
+
|
|
91
|
+
configureLogging(getConfiguration())
|
|
92
|
+
|
|
93
|
+
logger = logging.getLogger(__name__)
|
|
94
|
+
|
|
95
|
+
logger.info('Hello, World!')
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
When executed the program outputs the following:
|
|
99
|
+
|
|
100
|
+
```plaintext
|
|
101
|
+
[2025-12-31T12:34:56] Hello, World! level=INFO source=__main__
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Notables..
|
|
105
|
+
|
|
106
|
+
Things not obvious given the example above:
|
|
107
|
+
|
|
108
|
+
* All configuration options are optional, you can reduce the config to specifying only those things you wish to customize.
|
|
109
|
+
* It is not necessary to load a configuration at all, a call to `configureLogging()` will still apply reasonable defaults such as adding a console handler, applying a line format, applying a default date format (ISO 9601), etc.
|
|
110
|
+
* If no handlers are configured, a default handler for "console" is configured.
|
|
111
|
+
|
|
112
|
+
**hanaro** only has a single direct dependency: [``appsettings2``](https://pypi.org/project/appsettings2/).
|
|
113
|
+
|
|
114
|
+
## Contact
|
|
115
|
+
|
|
116
|
+
You can reach me on [Discord](https://discordapp.com/users/307684202080501761) or [open an Issue on Github](https://github.com/wilson0x4d/hanaro/issues/new/choose).
|
hanaro-0.0.1/README.md
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
`hanaro` (하나로) is a non-invasive `logging` configurator and facilitator for Python.
|
|
2
|
+
|
|
3
|
+
This README is only a high-level introduction to **hanaro**. For more detailed documentation, please view the official docs at [https://hanaro.readthedocs.io](https://hanaro.readthedocs.io).
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
**hanaro** can be installed from pypi through the usual means:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install hanaro
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
Let's try a "learn by example" approach. The following two snippets are the contents of a configuration file that contains a "logging" configuration section, and a Python code file that initializes Python's standard `logging` system using that configuration. This is by no means an exhaustive example, it only intends to touch on the major offerings of **hanaro**.
|
|
16
|
+
|
|
17
|
+
> NOTE: Configuration handling is performed using `appsettings2` which supports json, toml, yaml, xml, command-line args, and environment variables to provide a unified configuration solution. This example, however, uses json because of json's wider familiarity. For the sake of demonstration assume this content is in a file named `appsettings.json`.
|
|
18
|
+
|
|
19
|
+
```json
|
|
20
|
+
{
|
|
21
|
+
"logging": {
|
|
22
|
+
"level": "INFO",
|
|
23
|
+
"format": "[%(asctime)s] %(message)s level=%(levelname)s source=%(name)s %(meta)s",
|
|
24
|
+
"datefmt": "%Y-%m-%dT%H:%M:%S",
|
|
25
|
+
"handlers": [
|
|
26
|
+
{
|
|
27
|
+
"type": "console",
|
|
28
|
+
"level": "DEBUG"
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
"type": "file",
|
|
32
|
+
"level": "DEBUG",
|
|
33
|
+
"path": "logs/",
|
|
34
|
+
"name": "debug.log",
|
|
35
|
+
"max_size": "4KiB",
|
|
36
|
+
"max_count": 10,
|
|
37
|
+
"format": "[%(asctime)s] level=%(levelname)s %(message)s source=\"%(name)s\" func=\"%(funcName)s\" %(meta)s"
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
"type": "custom",
|
|
41
|
+
"canonical": "myapp.mymodule.myhandler",
|
|
42
|
+
"level": "WARNING",
|
|
43
|
+
"format": "msg=\"%(message)s\" level=\"%(levelname)s\" source=\"%(name)s\" func=\"%(funcName)s\" %(meta)s"
|
|
44
|
+
}
|
|
45
|
+
],
|
|
46
|
+
"filters": {
|
|
47
|
+
"asyncio": {
|
|
48
|
+
"level": "WARNING"
|
|
49
|
+
},
|
|
50
|
+
"mysql.connector": {
|
|
51
|
+
"level": "WARNING"
|
|
52
|
+
},
|
|
53
|
+
"urllib3.connectionpool": {
|
|
54
|
+
"level": "WARNING"
|
|
55
|
+
},
|
|
56
|
+
"websockets.client": {
|
|
57
|
+
"level": "WARNING"
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
This code sample is a minimum-viable solution. The `custom` handler above is omitted, but for the sake of demonstration know that `canonical` is the fully-qualified type name of a `logging.Handler` subclass and **hanaro** will create an instance of that class and configure as it does all other handlers.
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
from appsettings2 import getConfiguration
|
|
68
|
+
from hanaro import configureLogging
|
|
69
|
+
import logging
|
|
70
|
+
|
|
71
|
+
configureLogging(getConfiguration())
|
|
72
|
+
|
|
73
|
+
logger = logging.getLogger(__name__)
|
|
74
|
+
|
|
75
|
+
logger.info('Hello, World!')
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
When executed the program outputs the following:
|
|
79
|
+
|
|
80
|
+
```plaintext
|
|
81
|
+
[2025-12-31T12:34:56] Hello, World! level=INFO source=__main__
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Notables..
|
|
85
|
+
|
|
86
|
+
Things not obvious given the example above:
|
|
87
|
+
|
|
88
|
+
* All configuration options are optional, you can reduce the config to specifying only those things you wish to customize.
|
|
89
|
+
* It is not necessary to load a configuration at all, a call to `configureLogging()` will still apply reasonable defaults such as adding a console handler, applying a line format, applying a default date format (ISO 9601), etc.
|
|
90
|
+
* If no handlers are configured, a default handler for "console" is configured.
|
|
91
|
+
|
|
92
|
+
**hanaro** only has a single direct dependency: [``appsettings2``](https://pypi.org/project/appsettings2/).
|
|
93
|
+
|
|
94
|
+
## Contact
|
|
95
|
+
|
|
96
|
+
You can reach me on [Discord](https://discordapp.com/users/307684202080501761) or [open an Issue on Github](https://github.com/wilson0x4d/hanaro/issues/new/choose).
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: Copyright (C) Shaun Wilson
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import logging
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class _ConfigFilterSettings:
|
|
8
|
+
|
|
9
|
+
def __init__(self, source:str, settings:dict[str,str]):
|
|
10
|
+
self.source = source
|
|
11
|
+
self.level = getattr(logging, settings.get("level", "DEBUG").upper())
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class ConfigFilter(logging.Filter):
|
|
15
|
+
|
|
16
|
+
def __init__(self, config:dict[str,dict[str,str]]):
|
|
17
|
+
self.__settings = [_ConfigFilterSettings(k, v) for k,v in config.items()]
|
|
18
|
+
super().__init__()
|
|
19
|
+
|
|
20
|
+
def filter(self, record:logging.LogRecord) -> bool:
|
|
21
|
+
for e in self.__settings:
|
|
22
|
+
if record.name == e.source:
|
|
23
|
+
if record.levelno < e.level:
|
|
24
|
+
return False
|
|
25
|
+
return True
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: Copyright (C) Shaun Wilson
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import logging
|
|
5
|
+
import re
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class ContextInjectionFilter(logging.Filter):
|
|
9
|
+
|
|
10
|
+
def __init__(self, context:dict[str,str] = None, isMetadata:bool = False, metadataName:str = 'metadata'):
|
|
11
|
+
self.__context = context if context is not None else {}
|
|
12
|
+
self.__isMetadata = isMetadata
|
|
13
|
+
self.__metadataName = metadataName if metadataName is not None and len(metadataName) > 0 else 'metadata'
|
|
14
|
+
super().__init__()
|
|
15
|
+
|
|
16
|
+
def __getitem__(self, key:str) -> str|None:
|
|
17
|
+
self.__context.get(key, None)
|
|
18
|
+
|
|
19
|
+
def __setitem__(self, key:str, value:str|None) -> None:
|
|
20
|
+
self.__context[key] = value
|
|
21
|
+
|
|
22
|
+
def __delitem__(self, key:str) -> None:
|
|
23
|
+
self.__context.pop(key, None)
|
|
24
|
+
|
|
25
|
+
def filter(self, record:logging.LogRecord) -> bool:
|
|
26
|
+
if self.__isMetadata:
|
|
27
|
+
metadata = f' {getattr(record, self.__metadataName, '')}'
|
|
28
|
+
for k,v in self.__context.items():
|
|
29
|
+
if not hasattr(record, k):
|
|
30
|
+
metadata = f'{metadata} {k}="{v}"'
|
|
31
|
+
else:
|
|
32
|
+
metadata = re.sub(f' {k}="[^"]*"', f' {k}="{v}"', metadata)
|
|
33
|
+
setattr(record, k, v)
|
|
34
|
+
setattr(record, self.__metadataName, metadata.lstrip())
|
|
35
|
+
else:
|
|
36
|
+
for k,v in self.__context.items():
|
|
37
|
+
setattr(record, k, v)
|
|
38
|
+
return True
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: Copyright (C) Shaun Wilson
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import logging
|
|
5
|
+
import queue
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class QueuedHandler(logging.Handler):
|
|
9
|
+
|
|
10
|
+
__s_queue:queue.Queue[logging.LogRecord] = queue.Queue()
|
|
11
|
+
|
|
12
|
+
def __init__(self):
|
|
13
|
+
super().__init__()
|
|
14
|
+
|
|
15
|
+
def emit(self, record:logging.LogRecord) -> None:
|
|
16
|
+
QueuedHandler.__s_queue.put(record)
|
|
17
|
+
|
|
18
|
+
@staticmethod
|
|
19
|
+
def getLogRecord() -> logging.LogRecord|None:
|
|
20
|
+
record:logging.LogRecord = None
|
|
21
|
+
try:
|
|
22
|
+
record = QueuedHandler.__s_queue.get_nowait()
|
|
23
|
+
except:
|
|
24
|
+
pass
|
|
25
|
+
return record
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: Copyright (C) Shaun Wilson
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
from .utils import configureLogging, getQueuedLogger
|
|
5
|
+
from .ConfigFilter import ConfigFilter
|
|
6
|
+
from .ContextInjectionFilter import ContextInjectionFilter
|
|
7
|
+
from .QueuedHandler import QueuedHandler
|
|
8
|
+
from . import utils
|
|
9
|
+
|
|
10
|
+
__version__ = '0.0.1'
|
|
11
|
+
__commit__ = '3ee5ba9'
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
'__version__', '__commit__',
|
|
15
|
+
'ConfigFilter',
|
|
16
|
+
'ContextInjectionFilter',
|
|
17
|
+
'configureLogging',
|
|
18
|
+
'getQueuedLogger',
|
|
19
|
+
'QueuedHandler',
|
|
20
|
+
'utils'
|
|
21
|
+
]
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: Copyright (C) Shaun Wilson
|
|
2
|
+
# SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import importlib
|
|
5
|
+
import os
|
|
6
|
+
import appsettings2
|
|
7
|
+
import logging
|
|
8
|
+
import logging.handlers
|
|
9
|
+
import sys
|
|
10
|
+
|
|
11
|
+
from .ConfigFilter import ConfigFilter
|
|
12
|
+
from .ContextInjectionFilter import ContextInjectionFilter
|
|
13
|
+
from .QueuedHandler import QueuedHandler
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def configureLogging(config:appsettings2.Configuration = None) -> list[logging.Handler]:
|
|
17
|
+
config = config if config is not None else appsettings2.Configuration()
|
|
18
|
+
handlers = []
|
|
19
|
+
defaultLevel = getattr(logging, config.get('logging__level', 'DEBUG').upper())
|
|
20
|
+
defaultFormat = config.get('logging__format', logging.BASIC_FORMAT)
|
|
21
|
+
filters:appsettings2.Configuration = config.get('logging__filters', None)
|
|
22
|
+
configFilter = ConfigFilter(filters.toDictionary() if filters is not None else {})
|
|
23
|
+
contextInjectorFilter = ContextInjectionFilter({}, True)
|
|
24
|
+
datefmt = config.get('logging__datefmt', '%Y-%m-%dT%H:%M:%S')
|
|
25
|
+
# create configured handlers
|
|
26
|
+
handler_configs = config.get('logging__handlers')
|
|
27
|
+
if handler_configs != None:
|
|
28
|
+
for handler_config in handler_configs:
|
|
29
|
+
handler = None
|
|
30
|
+
match str(handler_config.get('type')).lower():
|
|
31
|
+
case 'custom':
|
|
32
|
+
module_name, class_name = handler_config.get('class').rsplit('.', 1)
|
|
33
|
+
module = importlib.import_module(module_name)
|
|
34
|
+
handler_class = getattr(module, class_name)
|
|
35
|
+
args = handler_config.get('args')
|
|
36
|
+
handler = handler_class(**(args.toDictionary() if args is not None else {}))
|
|
37
|
+
case 'console':
|
|
38
|
+
handler = logging.StreamHandler(sys.stdout)
|
|
39
|
+
case 'file':
|
|
40
|
+
log_path = handler_config.get('path')
|
|
41
|
+
if log_path == None:
|
|
42
|
+
log_path = '.'
|
|
43
|
+
log_path = os.path.abspath(log_path)
|
|
44
|
+
os.makedirs(log_path, exist_ok=True)
|
|
45
|
+
log_name = handler_config.get('name')
|
|
46
|
+
if log_name == None:
|
|
47
|
+
log_name = 'adamantium.log'
|
|
48
|
+
log_name = os.path.join(log_path, log_name)
|
|
49
|
+
max_size = handler_config.get('max_size')
|
|
50
|
+
if max_size == None:
|
|
51
|
+
max_size = 4 * 1024 * 1024
|
|
52
|
+
elif not type(max_size) is int:
|
|
53
|
+
size_unit = max_size[len(max_size)-3:].upper()
|
|
54
|
+
match size_unit:
|
|
55
|
+
case 'KIB':
|
|
56
|
+
max_size = int(max_size[:-3]) * 1024
|
|
57
|
+
case 'MIB':
|
|
58
|
+
max_size = int(max_size[:-3]) * 1024 * 1024
|
|
59
|
+
case 'GIB':
|
|
60
|
+
max_size = int(max_size[:-3]) * 1024 * 1024 * 1024
|
|
61
|
+
case _:
|
|
62
|
+
max_size = int(max_size)
|
|
63
|
+
max_count = handler_config.get('max_count')
|
|
64
|
+
if max_count == None:
|
|
65
|
+
max_count = 10
|
|
66
|
+
else:
|
|
67
|
+
max_count = int(max_count)
|
|
68
|
+
handler = logging.handlers.RotatingFileHandler(
|
|
69
|
+
filename = log_name,
|
|
70
|
+
encoding = 'utf-8',
|
|
71
|
+
maxBytes = max_size,
|
|
72
|
+
backupCount = max_count)
|
|
73
|
+
if handler != None:
|
|
74
|
+
handler.setLevel(getattr(logging, handler_config.get('level', defaultLevel).upper()))
|
|
75
|
+
handler.formatter = logging.Formatter(handler_config.get('format', defaultFormat), datefmt)
|
|
76
|
+
handler.addFilter(configFilter)
|
|
77
|
+
handler.addFilter(contextInjectorFilter)
|
|
78
|
+
handlers.append(handler)
|
|
79
|
+
# log to stdout if no handlers configured
|
|
80
|
+
if len(handlers) == 0:
|
|
81
|
+
handlers.append(logging.StreamHandler(sys.stdout))
|
|
82
|
+
# init
|
|
83
|
+
logging.basicConfig(
|
|
84
|
+
format = defaultFormat,
|
|
85
|
+
datefmt = datefmt,
|
|
86
|
+
handlers = handlers,
|
|
87
|
+
level = defaultLevel,
|
|
88
|
+
force = True
|
|
89
|
+
)
|
|
90
|
+
# return handlers so they can be registered with other loggers if necessary
|
|
91
|
+
logger = logging.getLogger(__name__)
|
|
92
|
+
return handlers
|
|
93
|
+
|
|
94
|
+
def getQueuedLogger(name:str) -> logging.Logger:
|
|
95
|
+
logger = logging.Logger(name)
|
|
96
|
+
logger.addHandler(QueuedHandler())
|
|
97
|
+
return logger
|
|
98
|
+
|
|
99
|
+
__all__ = [
|
|
100
|
+
'configureLogging',
|
|
101
|
+
'getQueuedLogger'
|
|
102
|
+
]
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: hanaro
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A non-invasive `logging` configurator and facilitator.
|
|
5
|
+
Author-email: Shaun Wilson <mrshaunwilson@msn.com>
|
|
6
|
+
Project-URL: Documentation, https://hanaro.readthedocs.io/
|
|
7
|
+
Project-URL: Homepage, https://github.com/wilson0x4d/hanaro
|
|
8
|
+
Project-URL: Repository, https://github.com/wilson0x4d/hanaro.git
|
|
9
|
+
Keywords: logging,configuration,appsettings2,async
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Requires-Python: >=3.12
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Dynamic: license-file
|
|
20
|
+
|
|
21
|
+
`hanaro` (하나로) is a non-invasive `logging` configurator and facilitator for Python.
|
|
22
|
+
|
|
23
|
+
This README is only a high-level introduction to **hanaro**. For more detailed documentation, please view the official docs at [https://hanaro.readthedocs.io](https://hanaro.readthedocs.io).
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
**hanaro** can be installed from pypi through the usual means:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
pip install hanaro
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
Let's try a "learn by example" approach. The following two snippets are the contents of a configuration file that contains a "logging" configuration section, and a Python code file that initializes Python's standard `logging` system using that configuration. This is by no means an exhaustive example, it only intends to touch on the major offerings of **hanaro**.
|
|
36
|
+
|
|
37
|
+
> NOTE: Configuration handling is performed using `appsettings2` which supports json, toml, yaml, xml, command-line args, and environment variables to provide a unified configuration solution. This example, however, uses json because of json's wider familiarity. For the sake of demonstration assume this content is in a file named `appsettings.json`.
|
|
38
|
+
|
|
39
|
+
```json
|
|
40
|
+
{
|
|
41
|
+
"logging": {
|
|
42
|
+
"level": "INFO",
|
|
43
|
+
"format": "[%(asctime)s] %(message)s level=%(levelname)s source=%(name)s %(meta)s",
|
|
44
|
+
"datefmt": "%Y-%m-%dT%H:%M:%S",
|
|
45
|
+
"handlers": [
|
|
46
|
+
{
|
|
47
|
+
"type": "console",
|
|
48
|
+
"level": "DEBUG"
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
"type": "file",
|
|
52
|
+
"level": "DEBUG",
|
|
53
|
+
"path": "logs/",
|
|
54
|
+
"name": "debug.log",
|
|
55
|
+
"max_size": "4KiB",
|
|
56
|
+
"max_count": 10,
|
|
57
|
+
"format": "[%(asctime)s] level=%(levelname)s %(message)s source=\"%(name)s\" func=\"%(funcName)s\" %(meta)s"
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
"type": "custom",
|
|
61
|
+
"canonical": "myapp.mymodule.myhandler",
|
|
62
|
+
"level": "WARNING",
|
|
63
|
+
"format": "msg=\"%(message)s\" level=\"%(levelname)s\" source=\"%(name)s\" func=\"%(funcName)s\" %(meta)s"
|
|
64
|
+
}
|
|
65
|
+
],
|
|
66
|
+
"filters": {
|
|
67
|
+
"asyncio": {
|
|
68
|
+
"level": "WARNING"
|
|
69
|
+
},
|
|
70
|
+
"mysql.connector": {
|
|
71
|
+
"level": "WARNING"
|
|
72
|
+
},
|
|
73
|
+
"urllib3.connectionpool": {
|
|
74
|
+
"level": "WARNING"
|
|
75
|
+
},
|
|
76
|
+
"websockets.client": {
|
|
77
|
+
"level": "WARNING"
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
This code sample is a minimum-viable solution. The `custom` handler above is omitted, but for the sake of demonstration know that `canonical` is the fully-qualified type name of a `logging.Handler` subclass and **hanaro** will create an instance of that class and configure as it does all other handlers.
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
from appsettings2 import getConfiguration
|
|
88
|
+
from hanaro import configureLogging
|
|
89
|
+
import logging
|
|
90
|
+
|
|
91
|
+
configureLogging(getConfiguration())
|
|
92
|
+
|
|
93
|
+
logger = logging.getLogger(__name__)
|
|
94
|
+
|
|
95
|
+
logger.info('Hello, World!')
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
When executed the program outputs the following:
|
|
99
|
+
|
|
100
|
+
```plaintext
|
|
101
|
+
[2025-12-31T12:34:56] Hello, World! level=INFO source=__main__
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Notables..
|
|
105
|
+
|
|
106
|
+
Things not obvious given the example above:
|
|
107
|
+
|
|
108
|
+
* All configuration options are optional, you can reduce the config to specifying only those things you wish to customize.
|
|
109
|
+
* It is not necessary to load a configuration at all, a call to `configureLogging()` will still apply reasonable defaults such as adding a console handler, applying a line format, applying a default date format (ISO 9601), etc.
|
|
110
|
+
* If no handlers are configured, a default handler for "console" is configured.
|
|
111
|
+
|
|
112
|
+
**hanaro** only has a single direct dependency: [``appsettings2``](https://pypi.org/project/appsettings2/).
|
|
113
|
+
|
|
114
|
+
## Contact
|
|
115
|
+
|
|
116
|
+
You can reach me on [Discord](https://discordapp.com/users/307684202080501761) or [open an Issue on Github](https://github.com/wilson0x4d/hanaro/issues/new/choose).
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
hanaro/ConfigFilter.py
|
|
5
|
+
hanaro/ContextInjectionFilter.py
|
|
6
|
+
hanaro/QueuedHandler.py
|
|
7
|
+
hanaro/__init__.py
|
|
8
|
+
hanaro/utils.py
|
|
9
|
+
hanaro.egg-info/PKG-INFO
|
|
10
|
+
hanaro.egg-info/SOURCES.txt
|
|
11
|
+
hanaro.egg-info/dependency_links.txt
|
|
12
|
+
hanaro.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "hanaro"
|
|
3
|
+
version = "0.0.1"
|
|
4
|
+
description = "A non-invasive `logging` configurator and facilitator."
|
|
5
|
+
keywords = ["logging", "configuration", "appsettings2", "async"]
|
|
6
|
+
authors = [
|
|
7
|
+
{ name="Shaun Wilson", email="mrshaunwilson@msn.com" }
|
|
8
|
+
]
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.12"
|
|
11
|
+
classifiers = [
|
|
12
|
+
"License :: OSI Approved :: MIT License",
|
|
13
|
+
"Operating System :: OS Independent",
|
|
14
|
+
"Programming Language :: Python :: 3.12",
|
|
15
|
+
"Programming Language :: Python :: 3.13",
|
|
16
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
17
|
+
"Intended Audience :: Developers"
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
[project.urls]
|
|
21
|
+
Documentation = "https://hanaro.readthedocs.io/"
|
|
22
|
+
Homepage = "https://github.com/wilson0x4d/hanaro"
|
|
23
|
+
Repository = "https://github.com/wilson0x4d/hanaro.git"
|
|
24
|
+
|
|
25
|
+
[build-system]
|
|
26
|
+
requires = ["setuptools"]
|
|
27
|
+
build-backend = "setuptools.build_meta"
|
|
28
|
+
|
|
29
|
+
[tool.setuptools.packages.find]
|
|
30
|
+
exclude = [
|
|
31
|
+
"docs",
|
|
32
|
+
"tests",
|
|
33
|
+
"tests.*",
|
|
34
|
+
".scripts",
|
|
35
|
+
".gitea",
|
|
36
|
+
".venv-*"
|
|
37
|
+
]
|
hanaro-0.0.1/setup.cfg
ADDED