loggez 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.
loggez-0.1/PKG-INFO ADDED
@@ -0,0 +1,48 @@
1
+ Metadata-Version: 2.1
2
+ Name: loggez
3
+ Version: 0.1
4
+ Summary: Python Easy Logging (LOGG EZ)
5
+ Home-page: https://gitlab.com/meehai/loggez
6
+ License: WTFPL
7
+ Requires-Python: >=3.8
8
+ Description-Content-Type: text/markdown
9
+ Requires-Dist: colorama>=0.4.6
10
+
11
+ # loggez: Python EZ logging
12
+
13
+ Control logging levels with env vars and have a unique logger per name, for library purposes...
14
+
15
+ Only writes to stderr, no files or stuff. Use something else for file logging though you can hack this library too.
16
+
17
+ Usage:
18
+ ```python
19
+ # run.py
20
+ from loggez import make_logger
21
+ from loguru import logger
22
+
23
+ logger.info("loguru hi")
24
+ my_logger = make_logger("my_logger")
25
+ my_logger.info("my_logger hi")
26
+ my_logger.debug("my_logger hi")
27
+ my_logger.debug2("my_logger hi")
28
+ my_logger.debug4("my_logger hi")
29
+
30
+ my_logger2 = make_logger("my_logger2")
31
+ my_logger2.info("my_logger2 hi")
32
+ my_logger2.debug("my_logger2 hi")
33
+ my_logger2.debug2("my_logger2 hi")
34
+ my_logger2.debug4("my_logger hi")
35
+ ```
36
+
37
+ Run with:
38
+ ```
39
+ my_logger_LOGLEVEL=0 run.py
40
+ my_logger_LOGLEVEL=1 run.py
41
+ my_logger_LOGLEVEL=2 run.py
42
+ ```
43
+
44
+ Additional env vars:
45
+ - `my_logger_MESSAGE=...`: see the default in `loggez/loggez.py` to control colors and stuff.
46
+ - `my_logger_INFO_MESSAGE=...`, `my_logger_DEBUG_MESSAGE=...` etc.
47
+
48
+ That's all.
loggez-0.1/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # loggez: Python EZ logging
2
+
3
+ Control logging levels with env vars and have a unique logger per name, for library purposes...
4
+
5
+ Only writes to stderr, no files or stuff. Use something else for file logging though you can hack this library too.
6
+
7
+ Usage:
8
+ ```python
9
+ # run.py
10
+ from loggez import make_logger
11
+ from loguru import logger
12
+
13
+ logger.info("loguru hi")
14
+ my_logger = make_logger("my_logger")
15
+ my_logger.info("my_logger hi")
16
+ my_logger.debug("my_logger hi")
17
+ my_logger.debug2("my_logger hi")
18
+ my_logger.debug4("my_logger hi")
19
+
20
+ my_logger2 = make_logger("my_logger2")
21
+ my_logger2.info("my_logger2 hi")
22
+ my_logger2.debug("my_logger2 hi")
23
+ my_logger2.debug2("my_logger2 hi")
24
+ my_logger2.debug4("my_logger hi")
25
+ ```
26
+
27
+ Run with:
28
+ ```
29
+ my_logger_LOGLEVEL=0 run.py
30
+ my_logger_LOGLEVEL=1 run.py
31
+ my_logger_LOGLEVEL=2 run.py
32
+ ```
33
+
34
+ Additional env vars:
35
+ - `my_logger_MESSAGE=...`: see the default in `loggez/loggez.py` to control colors and stuff.
36
+ - `my_logger_INFO_MESSAGE=...`, `my_logger_DEBUG_MESSAGE=...` etc.
37
+
38
+ That's all.
@@ -0,0 +1,2 @@
1
+ """init file"""
2
+ from .loggez import make_logger
@@ -0,0 +1,123 @@
1
+ """
2
+ Python logger settings.
3
+ Uses ENV variables to control the log level:
4
+ from simple_logging import make_logger
5
+ my_logger = make_logger("MY_KEY")
6
+ my_logger.debug4("message")
7
+ run with:
8
+ MY_KEY=4 python blabla.py
9
+
10
+ """
11
+ from __future__ import annotations
12
+ import os
13
+ import sys
14
+ import logging
15
+ from colorama import Fore, Back, Style
16
+
17
+ D2 = logging.DEBUG2 = logging.DEBUG - 1
18
+ D3 = logging.DEBUG3 = logging.DEBUG - 2
19
+ D4 = logging.DEBUG4 = logging.DEBUG - 3
20
+ logging.addLevelName(D2, "DGB2")
21
+ logging.addLevelName(D3, "DGB3")
22
+ logging.addLevelName(D4, "DGB4")
23
+
24
+
25
+ def _colorize(msg: str) -> str:
26
+ _colors = {
27
+ "cyan": Fore.CYAN,
28
+ "magenta": Fore.MAGENTA,
29
+ "red": Fore.RED,
30
+ "green": Fore.GREEN,
31
+ "yellow": Fore.YELLOW,
32
+ "back_red": Back.RED,
33
+ "back_cyan": Back.CYAN,
34
+ }
35
+ def _get_next(msg: str, replacements: dict[str, str]) -> str:
36
+ for replacement in replacements.keys():
37
+ if msg[0: len(replacement)] == replacement:
38
+ return replacement
39
+ raise RuntimeError(f"Found no next color in {msg} out of {list(replacements)}")
40
+
41
+ active_color = None
42
+ new_message = []
43
+ i = 0
44
+ while i < len(msg):
45
+ if msg[i] == "<":
46
+ assert active_color is None or msg[i + 1] == "/", f"Use </color> before starting a new color: {msg}"
47
+ _color = _get_next(msg[i + 2:], _colors) if active_color else _get_next(msg[i + 1:], _colors)
48
+ assert active_color is None or _color == active_color, f"Active color: {active_color}. Got: {_color}"
49
+ skip = len(_color) + 1 + (active_color is not None)
50
+ assert msg[i + skip] == ">", f"Expected <color>(ch {i}), got: {msg}"
51
+ new_message.append((_colors[_color] if active_color is None else Style.RESET_ALL))
52
+ active_color = None if active_color is not None else _color
53
+ i += skip + 1
54
+ else:
55
+ new_message.append(msg[i])
56
+ i += 1
57
+ return "".join(new_message)
58
+
59
+ class CustomFormatter(logging.Formatter):
60
+ """Custom formatting for logger."""
61
+ def __init__(self, formats, *args, **kwargs):
62
+ self.formats = formats
63
+ super().__init__(*args, **kwargs)
64
+
65
+ def format(self, record):
66
+ log_fmt = self.formats[record.levelno]
67
+ formatter = logging.Formatter(log_fmt)
68
+ formatter.formatTime = self.formatTime
69
+ return formatter.format(record)
70
+
71
+ # here we define the time format.
72
+ def formatTime(self, record, datefmt=None):
73
+ return super().formatTime(record, "%Y-%m-%dT%H:%M:%S")
74
+
75
+ def make_logger(key: str) -> logging.Logger:
76
+ global _simple_loggers
77
+ ENV_KEY = f"{key}_LOGLEVEL"
78
+ # defaults to -1 (no logger!).
79
+ env_var = int(os.environ[ENV_KEY]) if ENV_KEY in os.environ else 0
80
+
81
+ # we need numbers below 5 (last logging module used number)
82
+ try:
83
+ log_levels = {
84
+ -1: logging.NOTSET,
85
+ 0: logging.INFO,
86
+ 1: logging.DEBUG,
87
+ 2: logging.DEBUG2,
88
+ 3: logging.DEBUG3,
89
+ 4: logging.DEBUG4,
90
+ }
91
+ loglvl = log_levels[env_var]
92
+ except KeyError:
93
+ sys.stderr.write(f"You tried to use {key}_LOGLEVEL={env_var}. You need to set it between -1 and 4\n")
94
+ sys.exit(1)
95
+ # add the custom ones in the logger
96
+
97
+ assert key not in (X := logging.Logger.manager.loggerDict), f"'{key}' exists in {list(X.keys())} already."
98
+
99
+ # instantiate logger and set log level
100
+ new_logger = logging.getLogger(key)
101
+ new_logger.setLevel(loglvl)
102
+ new_logger.debug2 = lambda msg, *args: (new_logger._log(D2, msg, args=args) if loglvl > 0 and loglvl <= D2 else "")
103
+ new_logger.debug3 = lambda msg, *args: (new_logger._log(D3, msg, args=args) if loglvl > 0 and loglvl <= D3 else "")
104
+ new_logger.debug4 = lambda msg, *args: (new_logger._log(D4, msg, args=args) if loglvl > 0 and loglvl <= D4 else "")
105
+ # add custom formatter to logger
106
+ handler = logging.StreamHandler()
107
+
108
+ # Example [TIME:LEVEL:NAME] Message [FILE:FUNC:LINE]. We can update some other format here easily
109
+ DEFAULT_FORMATS = {
110
+ "DEBUG": _colorize("<cyan>[%(asctime)s %(name)s-%(levelname)s]</cyan> %(message)s <yellow>(%(filename)s:%(funcName)s:%(lineno)d)</yellow>"),
111
+ "DEBUG2": _colorize("<back_cyan>[%(asctime)s %(name)s-%(levelname)s]</back_cyan> %(message)s <yellow>(%(filename)s:%(funcName)s:%(lineno)d)</yellow>"),
112
+ "DEBUG3": _colorize("<magenta>[%(asctime)s %(name)s-%(levelname)s]</magenta> %(message)s <yellow>(%(filename)s:%(funcName)s:%(lineno)d)</yellow>"),
113
+ "DEBUG4": _colorize("<back_red>[%(asctime)s %(name)s-%(levelname)s]</back_red> %(message)s <yellow>(%(filename)s:%(funcName)s:%(lineno)d)</yellow>"),
114
+ "INFO": _colorize("<green>[%(asctime)s %(name)s-%(levelname)s]</green> %(message)s <yellow>(%(filename)s:%(funcName)s:%(lineno)d)</yellow>"),
115
+ "WARNING": _colorize("<yellow>[%(asctime)s %(name)s-%(levelname)s]</yellow> %(message)s <yellow>(%(filename)s:%(funcName)s:%(lineno)d)</yellow>"),
116
+ "ERROR": _colorize("<red>[%(asctime)s %(name)s-%(levelname)s]</red> %(message)s <yellow>(%(filename)s:%(funcName)s:%(lineno)d)</yellow>"),
117
+ "CRITICAL": _colorize("<back_red>[%(asctime)s %(name)s-%(levelname)s]</back_red> %(message)s <yellow>(%(filename)s:%(funcName)s:%(lineno)d)</yellow>"),
118
+ }
119
+ str_levels = ["DEBUG", "DEBUG2", "DEBUG3", "DEBUG4", "INFO", "WARNING", "ERROR", "CRITICAL"]
120
+ formats = {getattr(logging, k): os.getenv(f"{key}_{k}_MESSAGE", DEFAULT_FORMATS[k]) for k in str_levels}
121
+ handler.setFormatter(CustomFormatter(formats))
122
+ new_logger.addHandler(handler)
123
+ return new_logger
@@ -0,0 +1,48 @@
1
+ Metadata-Version: 2.1
2
+ Name: loggez
3
+ Version: 0.1
4
+ Summary: Python Easy Logging (LOGG EZ)
5
+ Home-page: https://gitlab.com/meehai/loggez
6
+ License: WTFPL
7
+ Requires-Python: >=3.8
8
+ Description-Content-Type: text/markdown
9
+ Requires-Dist: colorama>=0.4.6
10
+
11
+ # loggez: Python EZ logging
12
+
13
+ Control logging levels with env vars and have a unique logger per name, for library purposes...
14
+
15
+ Only writes to stderr, no files or stuff. Use something else for file logging though you can hack this library too.
16
+
17
+ Usage:
18
+ ```python
19
+ # run.py
20
+ from loggez import make_logger
21
+ from loguru import logger
22
+
23
+ logger.info("loguru hi")
24
+ my_logger = make_logger("my_logger")
25
+ my_logger.info("my_logger hi")
26
+ my_logger.debug("my_logger hi")
27
+ my_logger.debug2("my_logger hi")
28
+ my_logger.debug4("my_logger hi")
29
+
30
+ my_logger2 = make_logger("my_logger2")
31
+ my_logger2.info("my_logger2 hi")
32
+ my_logger2.debug("my_logger2 hi")
33
+ my_logger2.debug2("my_logger2 hi")
34
+ my_logger2.debug4("my_logger hi")
35
+ ```
36
+
37
+ Run with:
38
+ ```
39
+ my_logger_LOGLEVEL=0 run.py
40
+ my_logger_LOGLEVEL=1 run.py
41
+ my_logger_LOGLEVEL=2 run.py
42
+ ```
43
+
44
+ Additional env vars:
45
+ - `my_logger_MESSAGE=...`: see the default in `loggez/loggez.py` to control colors and stuff.
46
+ - `my_logger_INFO_MESSAGE=...`, `my_logger_DEBUG_MESSAGE=...` etc.
47
+
48
+ That's all.
@@ -0,0 +1,9 @@
1
+ README.md
2
+ setup.py
3
+ loggez/__init__.py
4
+ loggez/loggez.py
5
+ loggez.egg-info/PKG-INFO
6
+ loggez.egg-info/SOURCES.txt
7
+ loggez.egg-info/dependency_links.txt
8
+ loggez.egg-info/requires.txt
9
+ loggez.egg-info/top_level.txt
@@ -0,0 +1 @@
1
+ colorama>=0.4.6
@@ -0,0 +1 @@
1
+ loggez
loggez-0.1/setup.cfg ADDED
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
loggez-0.1/setup.py ADDED
@@ -0,0 +1,27 @@
1
+ from setuptools import setup, find_packages
2
+ from os import path
3
+
4
+ name = "loggez"
5
+ version = "0.1"
6
+ description = "Python Easy Logging (LOGG EZ)"
7
+ url = "https://gitlab.com/meehai/loggez"
8
+
9
+ loc = path.abspath(path.dirname(__file__))
10
+ with open(f"{loc}/README.md", "r", encoding="utf-8") as fh:
11
+ long_description = fh.read()
12
+
13
+ required = ["colorama>=0.4.6"]
14
+
15
+ setup(
16
+ name=name,
17
+ version=version,
18
+ description=description,
19
+ long_description=long_description,
20
+ long_description_content_type="text/markdown",
21
+ url=url,
22
+ packages=find_packages(),
23
+ install_requires=required,
24
+ dependency_links=[],
25
+ license="WTFPL",
26
+ python_requires=">=3.8"
27
+ )