本文整理匯總了Python中colorlog.ColoredFormatter方法的典型用法代碼示例。如果您正苦於以下問題:Python colorlog.ColoredFormatter方法的具體用法?Python colorlog.ColoredFormatter怎麽用?Python colorlog.ColoredFormatter使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類colorlog
的用法示例。
在下文中一共展示了colorlog.ColoredFormatter方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: create_console_handler
# 需要導入模塊: import colorlog [as 別名]
# 或者: from colorlog import ColoredFormatter [as 別名]
def create_console_handler(verbose_level):
'''Setup console logging.'''
del verbose_level # unused
clog = logging.StreamHandler()
formatter = ColoredFormatter(
"%(log_color)s[%(asctime)s %(levelname)-8s%(module)s]%(reset)s "
"%(white)s%(message)s",
datefmt="%H:%M:%S",
reset=True,
log_colors={
'DEBUG': 'cyan',
'INFO': 'green',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red',
})
clog.setFormatter(formatter)
clog.setLevel(logging.DEBUG)
return clog
示例2: logger_config
# 需要導入模塊: import colorlog [as 別名]
# 或者: from colorlog import ColoredFormatter [as 別名]
def logger_config(name=None):
import logging
import colorlog
"""
Setup the logging environment
"""
if not name:
log = logging.getLogger() # root logger
else:
log = logging.getLogger(name)
log.setLevel(logging.INFO)
format_str = '%(asctime)s.%(msecs)d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s'
date_format = '%Y-%m-%d %H:%M:%S'
cformat = '%(log_color)s' + format_str
colors = {'DEBUG': 'green',
'INFO': 'cyan',
'WARNING': 'bold_yellow',
'ERROR': 'bold_red',
'CRITICAL': 'bold_purple'}
formatter = colorlog.ColoredFormatter(cformat, date_format,
log_colors=colors)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
log.addHandler(stream_handler)
return log
示例3: InitiateColorLogger
# 需要導入模塊: import colorlog [as 別名]
# 或者: from colorlog import ColoredFormatter [as 別名]
def InitiateColorLogger(name='twisted'):
from colorlog import ColoredFormatter
logger = logging.getLogger(name)
stream = logging.StreamHandler()
LogFormat = " %(reset)s%(log_color)s%(levelname)-8s%(reset)s | %(log_color)s%(message)s"
stream.setFormatter(ColoredFormatter(LogFormat, log_colors={
'DEBUG': 'white',
'INFO': 'cyan',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'black,bg_red',
}))
logger.addHandler(stream)
logger.setLevel(logging.DEBUG)
logger.info("Porter running")
return logger
示例4: create_console_handler
# 需要導入模塊: import colorlog [as 別名]
# 或者: from colorlog import ColoredFormatter [as 別名]
def create_console_handler(verbose_level):
clog = logging.StreamHandler()
formatter = ColoredFormatter(
"%(log_color)s[%(asctime)s %(levelname)-8s%(module)s]%(reset)s "
"%(white)s%(message)s",
datefmt="%H:%M:%S",
reset=True,
log_colors={
'DEBUG': 'cyan',
'INFO': 'green',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red',
})
clog.setFormatter(formatter)
if verbose_level == 0:
clog.setLevel(logging.WARN)
elif verbose_level == 1:
clog.setLevel(logging.INFO)
else:
clog.setLevel(logging.DEBUG)
return clog
示例5: get_logger
# 需要導入模塊: import colorlog [as 別名]
# 或者: from colorlog import ColoredFormatter [as 別名]
def get_logger(command, verbose, daemon):
"""Return a Logger object with the right level, formatter and handler."""
if daemon:
handler = SysLogHandler(address=get_domain_socket())
formatter = logging.Formatter(fmt=DAEMON_FORMAT.format(command))
logger = logging.getLogger(command)
else:
handler = colorlog.StreamHandler(stream=sys.stdout)
formatter = colorlog.ColoredFormatter(INTERACTIVE_FORMAT,
log_colors=LOG_COLORS)
logger = colorlog.getLogger(command)
if verbose:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.INFO)
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger
示例6: init_loggers
# 需要導入模塊: import colorlog [as 別名]
# 或者: from colorlog import ColoredFormatter [as 別名]
def init_loggers():
"""
Function initialize loggers
"""
formatter = ColoredFormatter("%(log_color)s%(levelname)-8s%(reset)s %(white)s%(message)s",
datefmt=None,
reset=True,
log_colors={
'DEBUG': 'cyan',
'INFO': 'white',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red,bg_white',
},
secondary_log_colors={},
style='%'
)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
ROOTLOGGER.addHandler(handler)
示例7: init
# 需要導入模塊: import colorlog [as 別名]
# 或者: from colorlog import ColoredFormatter [as 別名]
def init(loop, argv):
parser = argparse.ArgumentParser(description="Test node connector ...")
args = parser.parse_args()
config_file = "test.conf"
log_level = logging.INFO
logger = colorlog.getLogger('cn')
config = configparser.ConfigParser()
config.read(config_file)
ch = logging.StreamHandler()
ch.setLevel(log_level)
formatter = colorlog.ColoredFormatter('%(log_color)s%(asctime)s %(levelname)s: %(message)s (%(module)s:%(lineno)d)')
ch.setFormatter(formatter)
logger.addHandler(ch)
logger.setLevel(log_level)
logger.info("Start")
loop = asyncio.get_event_loop()
app = App(loop, logger, config)
return app
示例8: init_logging
# 需要導入模塊: import colorlog [as 別名]
# 或者: from colorlog import ColoredFormatter [as 別名]
def init_logging():
log = logging.getLogger('CL')
term_handler = logging.StreamHandler()
try:
import colorlog
fmt = colorlog.ColoredFormatter('%(asctime)s %(log_color)s[%(name)12s:%(lineno)3s'
' %(funcName)18s ]\t%(levelname)-.6s %(message)s')
except ImportError:
fmt = logging.Formatter('%(asctime)s [%(name)12s:%(lineno)3s '
'%(funcName)18s ]\t%(levelname)-.6s %(message)s')
term_handler.setFormatter(fmt)
log.addHandler(term_handler)
log.setLevel(logging.DEBUG)
return log
示例9: get
# 需要導入模塊: import colorlog [as 別名]
# 或者: from colorlog import ColoredFormatter [as 別名]
def get(cls, tag):
"""
Get the global logger instance for the given tag
:param tag: the log tag
:return: the logger instance
"""
if tag not in cls._LOGGERS:
if cls._use_color:
handler = colorlog.StreamHandler()
handler.setFormatter(colorlog.ColoredFormatter( \
' %(log_color)s%(name)s/%(levelname)-8s%(reset)s |'
' %(log_color)s%(message)s%(reset)s'))
else:
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter( \
' %(name)s/%(levelname)-8s | %(message)s'))
logger = logging.getLogger(tag)
logger.addHandler(handler)
cls._LOGGERS[tag] = logger
return cls._LOGGERS[tag]
示例10: setup_logging
# 需要導入模塊: import colorlog [as 別名]
# 或者: from colorlog import ColoredFormatter [as 別名]
def setup_logging():
logger = logging.getLogger()
logger.setLevel(logging.INFO)
color_formatter = ColoredFormatter(
"%(log_color)s[%(asctime)s] [%(levelname)-4s]%(reset)s - %(message)s",
datefmt='%d-%m-%y %H:%M:%S',
reset=True,
log_colors={
'DEBUG': 'cyan',
'INFO': 'green',
'WARNING': 'bold_yellow',
'ERROR': 'bold_red',
'CRITICAL': 'bold_red',
},
secondary_log_colors={},
style='%')
logging_handler = logging.StreamHandler()
logging_handler.setFormatter(color_formatter)
logger.addHandler(logging_handler)
# setup logging for script
示例11: setup_logger
# 需要導入模塊: import colorlog [as 別名]
# 或者: from colorlog import ColoredFormatter [as 別名]
def setup_logger(name, level, colors=COLORS,
format_string="%(purple)s%(name)s> %(log_color)s%(message)s"):
"""Set up a particular logger with overridden behavior.
Args:
name (str): The name of the logger.
level (int): The log level to set this logger to.
colors (dict): A dictionary of log colors.
format_string (str): The format of the log message.
"""
logger_ = logging.getLogger(name)
logger_.setLevel(level)
formatter = ColoredFormatter(format_string, reset=True, log_colors=colors)
if logger_.handlers:
logger_.handlers.pop()
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger_.addHandler(handler)
logger_.propagate = False
return logger_
# Make the logger and output_logger available for import from elsewhere.
示例12: configure_logger
# 需要導入模塊: import colorlog [as 別名]
# 或者: from colorlog import ColoredFormatter [as 別名]
def configure_logger():
"""
This function is responsible to configure logging object.
"""
global LOGGER
LOGGER = logging.getLogger("Autobrowser")
# Set logging level
LOGGER.setLevel(logging.DEBUG)
# Create console handler
log_colors = {
'DEBUG': 'bold_blue',
'INFO': 'green',
'WARNING': 'purple',
'ERROR': 'red',
'CRITICAL': 'bold_yellow',
}
formatter = "%(log_color)s[%(asctime)s] - %(message)s%(reset)s"
formatter = ColoredFormatter(formatter, datefmt='%d-%m-%Y %H:%M', log_colors=log_colors)
ch = logging.StreamHandler(sys.stdout)
ch.setFormatter(formatter)
LOGGER.addHandler(ch)
示例13: getLogFormat
# 需要導入模塊: import colorlog [as 別名]
# 或者: from colorlog import ColoredFormatter [as 別名]
def getLogFormat(self, type):
if type == 'stdout':
format = global_config.LOG_STDOUT_FORMAT
elif type == 'file':
format = global_config.LOG_FILE_FORMAT
if format == "json":
return CustomJsonFormatter(
'%(time) %(level) %(component) %(module)%(message) %(pathname)'
' %(lineno)%(processName) %(threadName)%(node)')
else:
return ColoredFormatter(
'%(log_color)s[%(asctime)s] %(levelname)-10s: '
'%(name)s.%(module)-10s -%(message)s '
'[in %(pathname)s:%(lineno)d]%(reset)s'
)
示例14: setup_logging
# 需要導入模塊: import colorlog [as 別名]
# 或者: from colorlog import ColoredFormatter [as 別名]
def setup_logging(): # pragma: no cover
root_logger = logging.getLogger()
root_logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
formatter = ColoredFormatter(
"%(log_color)s%(levelname)-8s%(reset)s %(asctime)s %(green)s%(name)s"
"%(reset)s %(message)s",
reset=True,
log_colors={
'DEBUG': 'cyan',
'INFO': 'blue',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red,bg_white',
}
)
handler.setFormatter(formatter)
root_logger.addHandler(handler)
示例15: __init__
# 需要導入模塊: import colorlog [as 別名]
# 或者: from colorlog import ColoredFormatter [as 別名]
def __init__(self, lvl=logging.DEBUG, format=None):
self._lvl = lvl
if not format:
format = " %(log_color)s%(styledname)-8s%(reset)s | %(log_color)s%(message)s%(reset)s"
self.format = format
logging.root.setLevel(self._lvl)
self.formatter = colorlog.ColoredFormatter(self.format)
self.stream = logging.StreamHandler()
self.stream.setLevel(self._lvl)
self.stream.setFormatter(self.formatter)
self.logger = logging.getLogger('pythonConfig')
self.logger.setLevel(self._lvl)
self.logger.addHandler(self.stream)
self.theme = THEME
self.extra = {"styledname": self.theme[self._lvl]}
# the magic happens here: we use the "extra" argument documented in
# https://docs.python.org/2/library/logging.html#logging.Logger.debug
# to inject new items into the logging.LogRecord objects
# we also create our convenience methods here