0.13.0a3 add ColorFormatter()
This commit is contained in:
parent
3988a620a8
commit
a9e790eb94
4 changed files with 52 additions and 4 deletions
|
|
@ -7,6 +7,7 @@
|
||||||
* removed deprecated alias `entity_base()`. use `declarative_base()` instead.
|
* removed deprecated alias `entity_base()`. use `declarative_base()` instead.
|
||||||
* fix imports.
|
* fix imports.
|
||||||
+ Module `functools`: add `cooldown()`
|
+ Module `functools`: add `cooldown()`
|
||||||
|
+ Module `color`: add `ColorFormatter()`
|
||||||
|
|
||||||
## 0.12.6
|
## 0.12.6
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -35,13 +35,15 @@ from .strtools import PrefixIdentifier
|
||||||
from .validators import matches, not_less_than, not_greater_than, yesno
|
from .validators import matches, not_less_than, not_greater_than, yesno
|
||||||
from .redact import redact_url_password
|
from .redact import redact_url_password
|
||||||
from .http import WantsContentType
|
from .http import WantsContentType
|
||||||
from .color import OKLabColor, chalk, WebColor, RGBColor, LinearRGBColor, XYZColor, OKLCHColor
|
from .color import OKLabColor, chalk, WebColor, RGBColor, LinearRGBColor, \
|
||||||
|
XYZColor, OKLCHColor, ColorFormatter
|
||||||
from .mat import Matrix
|
from .mat import Matrix
|
||||||
from .argparse import LetterSubparsers
|
from .argparse import LetterSubparsers
|
||||||
|
|
||||||
__version__ = "0.13.0a2"
|
__version__ = "0.13.0a3"
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
|
'ColorFormatter',
|
||||||
'ConfigOptions', 'ConfigParserConfigSource', 'ConfigSource', 'ConfigValue',
|
'ConfigOptions', 'ConfigParserConfigSource', 'ConfigSource', 'ConfigValue',
|
||||||
'DictConfigSource', 'EnvConfigSource', 'I18n', 'Incomplete', 'JsonI18n',
|
'DictConfigSource', 'EnvConfigSource', 'I18n', 'Incomplete', 'JsonI18n',
|
||||||
'LetterSubparsers', 'LinearRGBColor',
|
'LetterSubparsers', 'LinearRGBColor',
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import sys
|
import sys
|
||||||
from typing import Callable, Mapping
|
from typing import Callable
|
||||||
|
|
||||||
class LetterSubparsers(object):
|
class LetterSubparsers(object):
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ from __future__ import annotations
|
||||||
|
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
|
import logging
|
||||||
import math
|
import math
|
||||||
|
|
||||||
from suou.functools import deprecated
|
from suou.functools import deprecated
|
||||||
|
|
@ -331,4 +332,48 @@ class OKLCHColor(namedtuple('_OKLCHColor', 'l c h')):
|
||||||
return sum(abs(i - j) / k for i, j, k in zip(self, other, (1, 1, 36)))
|
return sum(abs(i - j) / k for i, j, k in zip(self, other, (1, 1, 36)))
|
||||||
|
|
||||||
|
|
||||||
__all__ = ('chalk', 'WebColor', "RGBColor", 'LinearRGBColor', 'XYZColor', 'OKLabColor', 'OKLCHColor')
|
class ColorFormatter(logging.Formatter):
|
||||||
|
"""
|
||||||
|
Colored logging formatter.
|
||||||
|
|
||||||
|
Opinionated.
|
||||||
|
|
||||||
|
Taken from https://stackoverflow.com/questions/384076/how-can-i-color-python-logging-output
|
||||||
|
"""
|
||||||
|
|
||||||
|
def _get_base_format(color):
|
||||||
|
return f"[%(asctime)s] {color('%(levelname)s')}{chalk.grey(': ')}{color('%(name)s')}{chalk.grey(': ')}%(message)s"
|
||||||
|
|
||||||
|
FORMATS = {
|
||||||
|
logging.DEBUG: _get_base_format(chalk.cyan),
|
||||||
|
logging.INFO: _get_base_format(chalk.green),
|
||||||
|
logging.WARNING: _get_base_format(chalk.yellow),
|
||||||
|
logging.ERROR: _get_base_format(chalk.red),
|
||||||
|
logging.CRITICAL: _get_base_format(chalk.bold.red)
|
||||||
|
}
|
||||||
|
|
||||||
|
del _get_base_format
|
||||||
|
|
||||||
|
def format(self, record: logging.LogRecord) -> str:
|
||||||
|
log_fmt = self.FORMATS.get(record.levelno)
|
||||||
|
formatter = logging.Formatter(log_fmt)
|
||||||
|
return formatter.format(record)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def apply_handler(cls, logger: logging.Logger, level = logging.INFO):
|
||||||
|
"""
|
||||||
|
Apply the colored formatter to a logger.
|
||||||
|
|
||||||
|
Use this with logging.root, instead of logging.basicConfig().
|
||||||
|
|
||||||
|
Should not be called more than once.
|
||||||
|
"""
|
||||||
|
logger.setLevel(level)
|
||||||
|
ch = logging.StreamHandler()
|
||||||
|
ch.setLevel(level)
|
||||||
|
ch.setFormatter(cls())
|
||||||
|
logger.addHandler(ch)
|
||||||
|
|
||||||
|
|
||||||
|
__all__ = ('chalk', 'WebColor', "RGBColor", 'LinearRGBColor', 'XYZColor',
|
||||||
|
'OKLabColor', 'OKLCHColor', 'ColorFormatter')
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue