[logging] Switching to mondrian, who got all our formating code.
This commit is contained in:
@ -1,12 +1,12 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import traceback
|
import traceback
|
||||||
|
import logging
|
||||||
|
import mondrian
|
||||||
|
|
||||||
from bonobo import settings, logging
|
from bonobo import settings
|
||||||
from bonobo.commands.base import BaseCommand, BaseGraphCommand
|
from bonobo.commands.base import BaseCommand, BaseGraphCommand
|
||||||
from bonobo.util.errors import print_error
|
from bonobo.util.errors import print_error
|
||||||
|
|
||||||
logger = logging.get_logger()
|
|
||||||
|
|
||||||
|
|
||||||
def entrypoint(args=None):
|
def entrypoint(args=None):
|
||||||
"""
|
"""
|
||||||
@ -16,6 +16,8 @@ def entrypoint(args=None):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
logger = mondrian.getLogger()
|
||||||
|
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('--debug', '-D', action='store_true')
|
parser.add_argument('--debug', '-D', action='store_true')
|
||||||
|
|
||||||
@ -48,7 +50,7 @@ def entrypoint(args=None):
|
|||||||
if parsed_args.pop('debug', False):
|
if parsed_args.pop('debug', False):
|
||||||
settings.DEBUG.set(True)
|
settings.DEBUG.set(True)
|
||||||
settings.LOGGING_LEVEL.set(logging.DEBUG)
|
settings.LOGGING_LEVEL.set(logging.DEBUG)
|
||||||
logging.set_level(settings.LOGGING_LEVEL.get())
|
logger.setLevel(settings.LOGGING_LEVEL.get())
|
||||||
|
|
||||||
logger.debug('Command: ' + parsed_args['command'] + ' Arguments: ' + repr(parsed_args))
|
logger.debug('Command: ' + parsed_args['command'] + ' Arguments: ' + repr(parsed_args))
|
||||||
|
|
||||||
|
|||||||
@ -1,12 +1,12 @@
|
|||||||
import argparse
|
import argparse
|
||||||
|
import logging
|
||||||
import runpy
|
import runpy
|
||||||
import sys
|
import sys
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
|
|
||||||
import bonobo.util.environ
|
import bonobo.util.environ
|
||||||
from bonobo import logging
|
|
||||||
from bonobo.util.environ import get_argument_parser, parse_args
|
|
||||||
from bonobo.util import get_name
|
from bonobo.util import get_name
|
||||||
|
from bonobo.util.environ import get_argument_parser, parse_args
|
||||||
|
|
||||||
|
|
||||||
class BaseCommand:
|
class BaseCommand:
|
||||||
@ -20,7 +20,7 @@ class BaseCommand:
|
|||||||
try:
|
try:
|
||||||
return self._logger
|
return self._logger
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
self._logger = logging.get_logger(get_name(self))
|
self._logger = logging.getLogger(get_name(self))
|
||||||
return self._logger
|
return self._logger
|
||||||
|
|
||||||
def add_arguments(self, parser):
|
def add_arguments(self, parser):
|
||||||
|
|||||||
@ -1,86 +0,0 @@
|
|||||||
import logging
|
|
||||||
import sys
|
|
||||||
import textwrap
|
|
||||||
from logging import CRITICAL, DEBUG, ERROR, INFO, WARNING
|
|
||||||
|
|
||||||
from colorama import Fore, Style
|
|
||||||
|
|
||||||
from bonobo import settings
|
|
||||||
from bonobo.util.term import CLEAR_EOL
|
|
||||||
|
|
||||||
iswindows = (sys.platform == 'win32')
|
|
||||||
|
|
||||||
|
|
||||||
def get_format():
|
|
||||||
yield '{b}[%(fg)s%(levelname)s{b}][{w}'
|
|
||||||
yield '{b}][{w}'.join(('%(spent)04d', '%(name)s'))
|
|
||||||
yield '{b}]'
|
|
||||||
yield ' %(fg)s%(message)s{r}'
|
|
||||||
if not iswindows:
|
|
||||||
yield CLEAR_EOL
|
|
||||||
|
|
||||||
|
|
||||||
colors = {
|
|
||||||
'b': '' if iswindows else Fore.BLACK,
|
|
||||||
'w': '' if iswindows else Fore.LIGHTBLACK_EX,
|
|
||||||
'r': '' if iswindows else Style.RESET_ALL,
|
|
||||||
}
|
|
||||||
format = (''.join(get_format())).format(**colors)
|
|
||||||
|
|
||||||
|
|
||||||
class Filter(logging.Filter):
|
|
||||||
def filter(self, record):
|
|
||||||
record.spent = record.relativeCreated // 1000
|
|
||||||
if iswindows:
|
|
||||||
record.fg = ''
|
|
||||||
elif record.levelname == 'DEBG':
|
|
||||||
record.fg = Fore.LIGHTBLACK_EX
|
|
||||||
elif record.levelname == 'INFO':
|
|
||||||
record.fg = Fore.LIGHTWHITE_EX
|
|
||||||
elif record.levelname == 'WARN':
|
|
||||||
record.fg = Fore.LIGHTYELLOW_EX
|
|
||||||
elif record.levelname == 'ERR ':
|
|
||||||
record.fg = Fore.LIGHTRED_EX
|
|
||||||
elif record.levelname == 'CRIT':
|
|
||||||
record.fg = Fore.RED
|
|
||||||
else:
|
|
||||||
record.fg = Fore.LIGHTWHITE_EX
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
class Formatter(logging.Formatter):
|
|
||||||
def formatException(self, ei):
|
|
||||||
tb = super().formatException(ei)
|
|
||||||
if iswindows:
|
|
||||||
return textwrap.indent(tb, ' | ')
|
|
||||||
else:
|
|
||||||
return textwrap.indent(tb, Fore.BLACK + ' | ' + Fore.WHITE)
|
|
||||||
|
|
||||||
|
|
||||||
def setup(level):
|
|
||||||
logging.addLevelName(DEBUG, 'DEBG')
|
|
||||||
logging.addLevelName(INFO, 'INFO')
|
|
||||||
logging.addLevelName(WARNING, 'WARN')
|
|
||||||
logging.addLevelName(ERROR, 'ERR ')
|
|
||||||
logging.addLevelName(CRITICAL, 'CRIT')
|
|
||||||
handler = logging.StreamHandler(sys.stderr)
|
|
||||||
handler.setFormatter(Formatter(format))
|
|
||||||
handler.addFilter(Filter())
|
|
||||||
root = logging.getLogger()
|
|
||||||
root.addHandler(handler)
|
|
||||||
root.setLevel(level)
|
|
||||||
|
|
||||||
|
|
||||||
def set_level(level):
|
|
||||||
logging.getLogger().setLevel(level)
|
|
||||||
|
|
||||||
|
|
||||||
def get_logger(name='bonobo'):
|
|
||||||
return logging.getLogger(name)
|
|
||||||
|
|
||||||
|
|
||||||
# Compatibility with python logging
|
|
||||||
getLogger = get_logger
|
|
||||||
|
|
||||||
# Setup formating and level.
|
|
||||||
setup(level=settings.LOGGING_LEVEL.get())
|
|
||||||
Reference in New Issue
Block a user