Beispiel #1
0
 def setup_logging():
     """Return back a pretty color-coded logger."""
     logger = logging.getLogger()
     logger.setLevel(logging.INFO)
     handler = rainbow_logging_handler.RainbowLoggingHandler(sys.stdout)
     fmt = '%(asctime)-10s (%(levelname)s) %(message)s'
     formatter = logging.Formatter(fmt)
     handler.setFormatter(formatter)
     logger.addHandler(handler)
     return logger
Beispiel #2
0
def setup_root_logger(level='warn', syslog=None, color=False):
    """Configures the root logger.

    Args:
        level: Logging level string ('warn' is default)
        syslog: String representing syslog facility to output to.  If empty,
        logs are written to console.
        color: Colorize the log output

    Returns:
        A root Logger object
    """

    # Get the logging level string -> object
    level = 'logging.%s' % level.upper()
    level_obj = str_to_class(level)

    # Get our logger
    logger = logging.getLogger()

    # Set our default logging level
    logger.setLevel(level_obj)

    # Set the default logging handler to stream to console..
    if color:
        # Patch the handler's 'is_tty()' method to return True. If the user
        # asked for color, we give them color. The is_tty() method calls the
        # sys.stdout.isatty() method and then refuses to give color output on
        # platforms like Jenkins, where this code is likely to be run.
        rainbow_logging_handler.RainbowLoggingHandler.is_tty = True

        handler = rainbow_logging_handler.RainbowLoggingHandler(
            sys.stdout,

            # Disable colorization of the 'info' log statements. If the code is
            # run in an environment like Jenkins, the background is white, and
            # we don't want to force these log lines to be white as well.
            color_message_info=(None, None, False))
    else:
        handler = logging.StreamHandler()

    # Get our PID .. used below in the log line format.
    details = ''
    if level_obj <= 10:
        details = str(os.getpid()) + ' [%(name)-40s] [%(funcName)-20s]'

    # If syslog enabled, then override the logging handler to go to syslog.
    asctime = '%(asctime)-10s '
    if syslog is not None:
        asctime = ''
        handler = handlers.SysLogHandler(address=('127.0.0.1', 514),
                                         facility=syslog)

    fmt = asctime + '%(levelname)-8s ' + details + ' %(message)s'
    formatter = logging.Formatter(fmt)

    # Append the formatter to the handler, then set the handler as our default
    # handler for the root logger.
    handler.setFormatter(formatter)
    logger.addHandler(handler)

    return logger
Beispiel #3
0
#
parameters.set_args(arg_parser.parse_args())
#
if parameters['user_config']:
    parameters.set_file(parameters['user_config'].get())
#
port = parameters['port'].get() if 'port' in parameters else 8080
host = parameters['host'].get() if 'host' in parameters else '0.0.0.0'
log = logging.getLogger(__name__)
logging.getLogger('').setLevel(logging.DEBUG)

counter = Counter()
try:
    import rainbow_logging_handler

    handler = rainbow_logging_handler.RainbowLoggingHandler(sys.stderr)
    handler._column_color['%(asctime)s'] = ('cyan', None, False)
    handler._column_color['%(levelname)-7s'] = ('green', None, False)
    handler._column_color['%(message)s'][logging.INFO] = ('white', None, False)

    handler.setFormatter(logging.Formatter("%(levelname)-5s %(message)s"))

    root = logging.getLogger("")
    root.addHandler(handler)
except Exception as e:
    print(e)
    console = logging.StreamHandler()
    console.setLevel(logging.DEBUG)
    formatter = logging.Formatter('\n%(levelname)-8s %(name)-12s %(message)s')
    console.setFormatter(formatter)
    logging.getLogger('').addHandler(console)