mirror of
https://github.com/lilydjwg/nvchecker.git
synced 2025-03-10 06:14:02 +00:00
update nicelogger
This commit is contained in:
parent
0890c7e966
commit
0f441b0bf2
1 changed files with 29 additions and 42 deletions
|
@ -7,14 +7,16 @@ A Tornado-inspired logging formatter, with displayed time with millisecond accur
|
||||||
FYI: pyftpdlib also has a Tornado-style logger.
|
FYI: pyftpdlib also has a Tornado-style logger.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
class Colors:
|
class TornadoLogFormatter(logging.Formatter):
|
||||||
def __init__(self, color=None):
|
def __init__(self, color, *args, **kwargs):
|
||||||
if color is None:
|
super().__init__(*args, **kwargs)
|
||||||
color = support_color()
|
self._color = color
|
||||||
if color:
|
if color:
|
||||||
import curses
|
import curses
|
||||||
curses.setupterm()
|
curses.setupterm()
|
||||||
|
@ -23,32 +25,19 @@ class Colors:
|
||||||
curses.tigetstr("setf") or "", "ascii")
|
curses.tigetstr("setf") or "", "ascii")
|
||||||
else:
|
else:
|
||||||
fg_color = curses.tigetstr("setaf") or curses.tigetstr("setf") or b""
|
fg_color = curses.tigetstr("setaf") or curses.tigetstr("setf") or b""
|
||||||
|
|
||||||
self.blue = str(curses.tparm(fg_color, 4), "ascii")
|
|
||||||
self.yellow = str(curses.tparm(fg_color, 3), "ascii")
|
|
||||||
self.green = str(curses.tparm(fg_color, 2), "ascii")
|
|
||||||
self.red = str(curses.tparm(fg_color, 1), "ascii")
|
|
||||||
self.bright_red = str(curses.tparm(fg_color, 9), "ascii")
|
|
||||||
self.normal = str(curses.tigetstr("sgr0"), "ascii")
|
|
||||||
|
|
||||||
else:
|
|
||||||
self.blue = self.yellow = self.green = self.red = self.bright_red = self.normal = ""
|
|
||||||
|
|
||||||
|
|
||||||
class TornadoLogFormatter(logging.Formatter):
|
|
||||||
def __init__(self, color, *args, **kwargs):
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
self._color = color
|
|
||||||
if color:
|
|
||||||
colors = Colors(color=color)
|
|
||||||
self._colors = {
|
self._colors = {
|
||||||
logging.DEBUG: colors.blue,
|
logging.DEBUG: str(curses.tparm(fg_color, 4), # Blue
|
||||||
logging.INFO: colors.green,
|
"ascii"),
|
||||||
logging.WARNING: colors.yellow,
|
logging.INFO: str(curses.tparm(fg_color, 2), # Green
|
||||||
logging.ERROR: colors.red,
|
"ascii"),
|
||||||
logging.CRITICAL: colors.bright_red,
|
logging.WARNING: str(curses.tparm(fg_color, 3), # Yellow
|
||||||
|
"ascii"),
|
||||||
|
logging.ERROR: str(curses.tparm(fg_color, 1), # Red
|
||||||
|
"ascii"),
|
||||||
|
logging.CRITICAL: str(curses.tparm(fg_color, 9), # Bright Red
|
||||||
|
"ascii"),
|
||||||
}
|
}
|
||||||
self._normal = colors.normal
|
self._normal = str(curses.tigetstr("sgr0"), "ascii")
|
||||||
|
|
||||||
def format(self, record):
|
def format(self, record):
|
||||||
try:
|
try:
|
||||||
|
@ -71,6 +60,7 @@ class TornadoLogFormatter(logging.Formatter):
|
||||||
'filename', 'exc_info', 'exc_text', 'created', 'funcName',
|
'filename', 'exc_info', 'exc_text', 'created', 'funcName',
|
||||||
'processName', 'process', 'msecs', 'relativeCreated', 'thread',
|
'processName', 'process', 'msecs', 'relativeCreated', 'thread',
|
||||||
'threadName', 'name', 'levelno', 'msg', 'pathname', 'stack_info',
|
'threadName', 'name', 'levelno', 'msg', 'pathname', 'stack_info',
|
||||||
|
'taskName',
|
||||||
})
|
})
|
||||||
|
|
||||||
if record.exc_info:
|
if record.exc_info:
|
||||||
|
@ -80,18 +70,6 @@ class TornadoLogFormatter(logging.Formatter):
|
||||||
formatted = formatted.rstrip() + "\n" + record.exc_text
|
formatted = formatted.rstrip() + "\n" + record.exc_text
|
||||||
return formatted.replace("\n", "\n ")
|
return formatted.replace("\n", "\n ")
|
||||||
|
|
||||||
def support_color(stream=sys.stderr):
|
|
||||||
if stream.isatty():
|
|
||||||
try:
|
|
||||||
import curses
|
|
||||||
curses.setupterm()
|
|
||||||
if curses.tigetnum("colors") > 0:
|
|
||||||
return True
|
|
||||||
except:
|
|
||||||
import traceback
|
|
||||||
traceback.print_exc()
|
|
||||||
return False
|
|
||||||
|
|
||||||
def enable_pretty_logging(level=logging.DEBUG, handler=None, color=None):
|
def enable_pretty_logging(level=logging.DEBUG, handler=None, color=None):
|
||||||
'''
|
'''
|
||||||
handler: specify a handler instead of default StreamHandler
|
handler: specify a handler instead of default StreamHandler
|
||||||
|
@ -103,8 +81,17 @@ def enable_pretty_logging(level=logging.DEBUG, handler=None, color=None):
|
||||||
h = logging.StreamHandler()
|
h = logging.StreamHandler()
|
||||||
else:
|
else:
|
||||||
h = handler
|
h = handler
|
||||||
if color is None and handler is None:
|
if color is None:
|
||||||
color = support_color()
|
color = False
|
||||||
|
if handler is None and sys.stderr.isatty():
|
||||||
|
try:
|
||||||
|
import curses
|
||||||
|
curses.setupterm()
|
||||||
|
if curses.tigetnum("colors") > 0:
|
||||||
|
color = True
|
||||||
|
except:
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
formatter = TornadoLogFormatter(color=color)
|
formatter = TornadoLogFormatter(color=color)
|
||||||
h.setLevel(level)
|
h.setLevel(level)
|
||||||
h.setFormatter(formatter)
|
h.setFormatter(formatter)
|
||||||
|
|
Loading…
Add table
Reference in a new issue