custom-python-logger 1.0.2__tar.gz → 1.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.
- {custom_python_logger-1.0.2/custom_python_logger.egg-info → custom_python_logger-1.0.3}/PKG-INFO +1 -1
- custom_python_logger-1.0.3/custom_python_logger/__init__.py +3 -0
- custom_python_logger-1.0.3/custom_python_logger/logger.py +146 -0
- custom_python_logger-1.0.3/custom_python_logger/usage_example.py +39 -0
- {custom_python_logger-1.0.2 → custom_python_logger-1.0.3/custom_python_logger.egg-info}/PKG-INFO +1 -1
- {custom_python_logger-1.0.2 → custom_python_logger-1.0.3}/custom_python_logger.egg-info/SOURCES.txt +3 -0
- custom_python_logger-1.0.3/custom_python_logger.egg-info/top_level.txt +1 -0
- {custom_python_logger-1.0.2 → custom_python_logger-1.0.3}/setup.py +3 -2
- custom_python_logger-1.0.2/custom_python_logger.egg-info/top_level.txt +0 -1
- {custom_python_logger-1.0.2 → custom_python_logger-1.0.3}/LICENSE +0 -0
- {custom_python_logger-1.0.2 → custom_python_logger-1.0.3}/MANIFEST.in +0 -0
- {custom_python_logger-1.0.2 → custom_python_logger-1.0.3}/README.md +0 -0
- {custom_python_logger-1.0.2 → custom_python_logger-1.0.3}/custom_python_logger.egg-info/dependency_links.txt +0 -0
- {custom_python_logger-1.0.2 → custom_python_logger-1.0.3}/custom_python_logger.egg-info/requires.txt +0 -0
- {custom_python_logger-1.0.2 → custom_python_logger-1.0.3}/pyproject.toml +0 -0
- {custom_python_logger-1.0.2 → custom_python_logger-1.0.3}/requirements.txt +0 -0
- {custom_python_logger-1.0.2 → custom_python_logger-1.0.3}/setup.cfg +0 -0
- {custom_python_logger-1.0.2 → custom_python_logger-1.0.3}/tests/test_logger.py +0 -0
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
# logger.py
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
import os
|
|
5
|
+
import time
|
|
6
|
+
from logging import LoggerAdapter, Logger
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import Optional, Any
|
|
9
|
+
from colorlog import ColoredFormatter
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def get_root_project_path() -> Path:
|
|
13
|
+
if 'venv' in str(Path(__file__)):
|
|
14
|
+
return Path(__file__).parents[5]
|
|
15
|
+
return Path(__file__).parent
|
|
16
|
+
|
|
17
|
+
def print_before_logger(project_name: str) -> None:
|
|
18
|
+
main_string = f'Start "{project_name}" Process'
|
|
19
|
+
|
|
20
|
+
number_of_ladder = "#" * len(f"### {main_string} ###")
|
|
21
|
+
print(f"\n{number_of_ladder}")
|
|
22
|
+
print(f"### {main_string} ###")
|
|
23
|
+
print(f"{number_of_ladder}\n")
|
|
24
|
+
time.sleep(0.3)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class CustomLoggerAdapter(logging.LoggerAdapter):
|
|
28
|
+
def exception(self, msg: str, *args, **kwargs):
|
|
29
|
+
level_no = 45
|
|
30
|
+
logging.addLevelName(level_no, "EXCEPTION")
|
|
31
|
+
kwargs.setdefault('stacklevel', 2)
|
|
32
|
+
self.log(level_no, msg, *args, exc_info=True, **kwargs)
|
|
33
|
+
|
|
34
|
+
def step(self, msg: str, *args, **kwargs):
|
|
35
|
+
level_no = 25
|
|
36
|
+
logging.addLevelName(level_no, "STEP")
|
|
37
|
+
kwargs.setdefault('stacklevel', 2)
|
|
38
|
+
self.log(level_no, msg, *args, exc_info=False, **kwargs)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def configure_logging(
|
|
42
|
+
log_format: str,
|
|
43
|
+
utc: bool,
|
|
44
|
+
log_level: int = logging.INFO,
|
|
45
|
+
log_file: Optional[str] = None,
|
|
46
|
+
console_output: bool = True,
|
|
47
|
+
) -> None:
|
|
48
|
+
"""
|
|
49
|
+
Configure global logging settings.
|
|
50
|
+
|
|
51
|
+
Args:
|
|
52
|
+
log_level: Logging level (default: INFO)
|
|
53
|
+
log_format: Format string for log messages
|
|
54
|
+
log_file: Path to log file (if None, no file logging)
|
|
55
|
+
console_output: Whether to output logs to console
|
|
56
|
+
utc: Whether to use UTC time for log timestamps
|
|
57
|
+
"""
|
|
58
|
+
if utc:
|
|
59
|
+
logging.Formatter.converter = time.gmtime
|
|
60
|
+
|
|
61
|
+
root_logger = logging.getLogger()
|
|
62
|
+
root_logger.setLevel(log_level)
|
|
63
|
+
|
|
64
|
+
# Clear existing handlers
|
|
65
|
+
for handler in root_logger.handlers[:]:
|
|
66
|
+
root_logger.removeHandler(handler)
|
|
67
|
+
|
|
68
|
+
# Add file handler if specified
|
|
69
|
+
if log_file is not None:
|
|
70
|
+
log_file_formatter = logging.Formatter(log_format)
|
|
71
|
+
|
|
72
|
+
# Create directory if it doesn't exist
|
|
73
|
+
log_dir = os.path.dirname(log_file)
|
|
74
|
+
if log_dir and not os.path.exists(log_dir):
|
|
75
|
+
os.makedirs(log_dir)
|
|
76
|
+
|
|
77
|
+
file_handler = logging.FileHandler(log_file)
|
|
78
|
+
|
|
79
|
+
file_handler.setFormatter(log_file_formatter)
|
|
80
|
+
root_logger.addHandler(file_handler)
|
|
81
|
+
|
|
82
|
+
# Add console handler if specified
|
|
83
|
+
if console_output:
|
|
84
|
+
# log_console_formatter = logging.Formatter('%(log_color)s ' + log_format)
|
|
85
|
+
log_console_formatter = ColoredFormatter(
|
|
86
|
+
'%(log_color)s ' + log_format,
|
|
87
|
+
log_colors={
|
|
88
|
+
'DEBUG': 'white',
|
|
89
|
+
'INFO': 'green',
|
|
90
|
+
'WARNING': 'yellow',
|
|
91
|
+
'STEP': 'blue',
|
|
92
|
+
'ERROR': 'red,bold',
|
|
93
|
+
'EXCEPTION': 'light_red,bold',
|
|
94
|
+
'CRITICAL': 'red,bg_white',
|
|
95
|
+
}
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
console_handler = logging.StreamHandler()
|
|
99
|
+
console_handler.setFormatter(log_console_formatter)
|
|
100
|
+
root_logger.addHandler(console_handler)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def get_logger(
|
|
104
|
+
project_name: str,
|
|
105
|
+
extra: Optional[dict[str, Any]] = None,
|
|
106
|
+
log_format: str = "%(asctime)s | %(levelname)-10s(l.%(levelno)s) | %(filename)s:%(lineno)s | %(message)s",
|
|
107
|
+
log_level: int = logging.INFO,
|
|
108
|
+
log_file: str = None,
|
|
109
|
+
console_output: bool = True,
|
|
110
|
+
utc: bool = False,
|
|
111
|
+
) -> CustomLoggerAdapter[Logger | LoggerAdapter[Any] | Any] | Logger:
|
|
112
|
+
"""
|
|
113
|
+
Get a named logger with optional extra context.
|
|
114
|
+
|
|
115
|
+
Args:
|
|
116
|
+
project_name: Name of the project
|
|
117
|
+
log_level: Optional specific log level
|
|
118
|
+
extra: Optional dictionary of extra context values
|
|
119
|
+
log_format: Format string for log messages
|
|
120
|
+
log_file: Path to log file (if None, no file logging)
|
|
121
|
+
console_output: Whether to output logs to console
|
|
122
|
+
utc: Whether to use UTC time for log timestamps
|
|
123
|
+
|
|
124
|
+
Returns:
|
|
125
|
+
Configured logger
|
|
126
|
+
"""
|
|
127
|
+
print_before_logger(project_name=project_name)
|
|
128
|
+
|
|
129
|
+
if not log_file:
|
|
130
|
+
log_file = f'{get_root_project_path()}/logs/{project_name}.log'
|
|
131
|
+
log_file = log_file.lower().replace(' ', '_')
|
|
132
|
+
|
|
133
|
+
configure_logging(
|
|
134
|
+
log_level=logging.DEBUG,
|
|
135
|
+
log_format=log_format,
|
|
136
|
+
log_file=log_file,
|
|
137
|
+
console_output=console_output,
|
|
138
|
+
utc=utc
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
logger = logging.getLogger()
|
|
142
|
+
|
|
143
|
+
if log_level is not None:
|
|
144
|
+
logger.setLevel(log_level)
|
|
145
|
+
|
|
146
|
+
return CustomLoggerAdapter(logger, extra)
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class LoggerTest:
|
|
5
|
+
def __init__(self):
|
|
6
|
+
self.logger = logging.getLogger(self.__class__.__name__)
|
|
7
|
+
|
|
8
|
+
def main(self):
|
|
9
|
+
self.logger.info('Hello World')
|
|
10
|
+
self.logger.debug('Hello World')
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def main():
|
|
14
|
+
from custom_python_logger.logger import get_logger
|
|
15
|
+
|
|
16
|
+
logger = get_logger(
|
|
17
|
+
project_name='Logger Project Test',
|
|
18
|
+
log_level=logging.DEBUG,
|
|
19
|
+
# extra={'user': 'test_user'}
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
logger.debug("This is a debug message.")
|
|
23
|
+
logger.info("This is an info message.")
|
|
24
|
+
logger.step("This is a step message.")
|
|
25
|
+
logger.warning("This is a warning message.")
|
|
26
|
+
|
|
27
|
+
try:
|
|
28
|
+
_ = 1 / 0
|
|
29
|
+
except ZeroDivisionError:
|
|
30
|
+
logger.exception("This is an exception message.")
|
|
31
|
+
|
|
32
|
+
logger.critical("This is a critical message.")
|
|
33
|
+
|
|
34
|
+
logger_test = LoggerTest()
|
|
35
|
+
logger_test.main()
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
if __name__ == '__main__':
|
|
39
|
+
main()
|
{custom_python_logger-1.0.2 → custom_python_logger-1.0.3}/custom_python_logger.egg-info/SOURCES.txt
RENAMED
|
@@ -4,6 +4,9 @@ README.md
|
|
|
4
4
|
pyproject.toml
|
|
5
5
|
requirements.txt
|
|
6
6
|
setup.py
|
|
7
|
+
custom_python_logger/__init__.py
|
|
8
|
+
custom_python_logger/logger.py
|
|
9
|
+
custom_python_logger/usage_example.py
|
|
7
10
|
custom_python_logger.egg-info/PKG-INFO
|
|
8
11
|
custom_python_logger.egg-info/SOURCES.txt
|
|
9
12
|
custom_python_logger.egg-info/dependency_links.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
custom_python_logger
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
from setuptools import setup, find_packages
|
|
2
2
|
|
|
3
|
-
package_version = '1.0.
|
|
3
|
+
package_version = '1.0.3'
|
|
4
4
|
|
|
5
5
|
package_name = 'custom-python-logger'
|
|
6
6
|
package_description = 'A custom logger with color support and additional features.'
|
|
7
7
|
|
|
8
|
+
package_name_ = package_name.replace('-', '_')
|
|
8
9
|
package_long_description_content_type = 'text/markdown'
|
|
9
10
|
package_url = f'https://github.com/aviz92/{package_name}'
|
|
10
11
|
package_python_requires = '>=3.11'
|
|
@@ -21,7 +22,7 @@ with open('README.md', 'r') as file:
|
|
|
21
22
|
setup(
|
|
22
23
|
name=package_name,
|
|
23
24
|
version=package_version,
|
|
24
|
-
packages=find_packages(include=[
|
|
25
|
+
packages=find_packages(include=[package_name_, f'{package_name_}.*']),
|
|
25
26
|
install_requires=package_install_requires,
|
|
26
27
|
author=package_author,
|
|
27
28
|
author_email='',
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{custom_python_logger-1.0.2 → custom_python_logger-1.0.3}/custom_python_logger.egg-info/requires.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|