本文整理汇总了Python中logzero.LogFormatter方法的典型用法代码示例。如果您正苦于以下问题:Python logzero.LogFormatter方法的具体用法?Python logzero.LogFormatter怎么用?Python logzero.LogFormatter使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类logzero
的用法示例。
在下文中一共展示了logzero.LogFormatter方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_custom_formatter
# 需要导入模块: import logzero [as 别名]
# 或者: from logzero import LogFormatter [as 别名]
def test_custom_formatter():
"""
Should work with a custom formatter.
"""
logzero.reset_default_logger()
temp = tempfile.NamedTemporaryFile()
try:
log_format = '%(color)s[%(levelname)1.1s %(asctime)s customnametest:%(lineno)d]%(end_color)s %(message)s'
formatter = logzero.LogFormatter(fmt=log_format)
logger = logzero.setup_logger(logfile=temp.name, formatter=formatter)
logger.info("test log output")
with open(temp.name) as f:
content = f.read()
assert " customnametest:" in content
assert content.endswith("test log output\n")
finally:
temp.close()
示例2: getLogger
# 需要导入模块: import logzero [as 别名]
# 或者: from logzero import LogFormatter [as 别名]
def getLogger(self, component_name: str = None) -> logging.Logger:
"""
Get the logger instance matching ``component_name`` or create a new one if non-existent.
Args:
component_name: a neo-python component name. e.g. network, vm, db
Returns:
a logger for the specified component.
"""
logger_name = self.root + (component_name if component_name else 'generic')
_logger = self.loggers.get(logger_name)
if not _logger:
_logger = logging.getLogger(logger_name)
stdio_handler = logging.StreamHandler()
stdio_handler.setFormatter(LogFormatter())
stdio_handler.setLevel(logging.INFO)
_logger.addHandler(stdio_handler)
_logger.setLevel(logging.DEBUG)
self.loggers[logger_name] = _logger
return _logger
示例3: set_logfile
# 需要导入模块: import logzero [as 别名]
# 或者: from logzero import LogFormatter [as 别名]
def set_logfile(self, filename, max_bytes=0, backup_count=0):
"""
Setup logging to a (rotating) logfile.
Args:
filename (str): Logfile. If filename is None, disable file logging
max_bytes (int): Maximum number of bytes per logfile. If used together with backup_count,
logfile will be rotated when it reaches this amount of bytes.
backup_count (int): Number of rotated logfiles to keep
"""
_logger = logging.getLogger("neo-python")
if not filename and not self.rotating_filehandler:
_logger.removeHandler(self.rotating_filehandler)
else:
self.rotating_filehandler = RotatingFileHandler(filename, mode='a', maxBytes=max_bytes, backupCount=backup_count, encoding=None)
self.rotating_filehandler.setLevel(logging.DEBUG)
self.rotating_filehandler.setFormatter(LogFormatter(color=False))
_logger.addHandler(self.rotating_filehandler)
示例4: test_default_logger
# 需要导入模块: import logzero [as 别名]
# 或者: from logzero import LogFormatter [as 别名]
def test_default_logger(disableStdErrorLogger=False):
"""
Default logger should work and be able to be reconfigured.
"""
logzero.reset_default_logger()
temp = tempfile.NamedTemporaryFile()
try:
logzero.setup_default_logger(logfile=temp.name, disableStderrLogger=disableStdErrorLogger)
logzero.logger.debug("debug1") # will be logged
# Reconfigure with loglevel INFO
logzero.setup_default_logger(logfile=temp.name, level=logging.INFO, disableStderrLogger=disableStdErrorLogger)
logzero.logger.debug("debug2") # will not be logged
logzero.logger.info("info1") # will be logged
# Reconfigure with a different formatter
log_format = '%(color)s[xxx]%(end_color)s %(message)s'
formatter = logzero.LogFormatter(fmt=log_format)
logzero.setup_default_logger(logfile=temp.name, level=logging.INFO, formatter=formatter, disableStderrLogger=disableStdErrorLogger)
logzero.logger.info("info2") # will be logged with new formatter
logzero.logger.debug("debug3") # will not be logged
with open(temp.name) as f:
content = f.read()
test_default_logger_output(content)
finally:
temp.close()
示例5: get_logger
# 需要导入模块: import logzero [as 别名]
# 或者: from logzero import LogFormatter [as 别名]
def get_logger():
# Set a custom formatter
log_format = '%(color)s[%(levelname)1.1s ' \
'%(asctime)s.%(msecs)03d %(module)s:%(lineno)d]' \
'%(end_color)s %(message)s'
formatter = logzero.LogFormatter(fmt=log_format)
logzero.setup_default_logger(formatter=formatter)
logzero.logfile(
'logzero.log',
maxBytes=1e6,
backupCount=3
)
return logzero.logger
示例6: configure_logger
# 需要导入模块: import logzero [as 别名]
# 或者: from logzero import LogFormatter [as 别名]
def configure_logger(verbose: bool = False, log_format: str = "string",
log_file: str = None, logger_name: str = "chaostoolkit",
context_id: str = None):
"""
Configure the chaostoolkit logger.
By default logs as strings to stdout and the given file. When `log_format`
is `"json"`, records are set to the console as JSON strings but remain
as strings in the log file. The rationale is that the log file is mostly
for grepping purpose while records written to the console can be forwarded
out of band to anywhere else.
"""
log_level = logging.INFO
# we define colors ourselves as critical is missing in default ones
colors = {
logging.DEBUG: ForegroundColors.CYAN,
logging.INFO: ForegroundColors.GREEN,
logging.WARNING: ForegroundColors.YELLOW,
logging.ERROR: ForegroundColors.RED,
logging.CRITICAL: ForegroundColors.RED
}
fmt = "%(color)s[%(asctime)s %(levelname)s]%(end_color)s %(message)s"
if verbose:
log_level = logging.DEBUG
fmt = "%(color)s[%(asctime)s %(levelname)s] "\
"[%(module)s:%(lineno)d]%(end_color)s %(message)s"
formatter = LogFormatter(
fmt=fmt, datefmt="%Y-%m-%d %H:%M:%S", colors=colors)
if log_format == 'json':
fmt = "(process) (asctime) (levelname) (module) (lineno) (message)"
if context_id:
fmt = "(context_id) {}".format(fmt)
formatter = jsonlogger.JsonFormatter(
fmt, json_default=encoder, timestamp=True)
# sadly, no other way to specify the name of the default logger publicly
LOGZERO_DEFAULT_LOGGER = logger_name
logger = setup_default_logger(level=log_level, formatter=formatter)
if context_id:
logger.addFilter(ChaosToolkitContextFilter(logger_name, context_id))
if log_file:
# always everything as strings in the log file
logger.setLevel(logging.DEBUG)
fmt = "%(color)s[%(asctime)s %(levelname)s] "\
"[%(module)s:%(lineno)d]%(end_color)s %(message)s"
formatter = LogFormatter(fmt=fmt, datefmt="%Y-%m-%d %H:%M:%S",
colors=colors)
logzero.logfile(log_file, formatter=formatter, mode='a',
loglevel=logging.DEBUG)
示例7: test_setup_logger_reconfiguration
# 需要导入模块: import logzero [as 别名]
# 或者: from logzero import LogFormatter [as 别名]
def test_setup_logger_reconfiguration():
"""
Should be able to reconfigure without loosing custom handlers
"""
logzero.reset_default_logger()
temp = tempfile.NamedTemporaryFile()
temp2 = tempfile.NamedTemporaryFile()
try:
logzero.setup_default_logger(logfile=temp.name)
# Add a custom file handler
filehandler = logging.FileHandler(temp2.name)
filehandler.setLevel(logging.DEBUG)
filehandler.setFormatter(logzero.LogFormatter(color=False))
logzero.logger.addHandler(filehandler)
# First debug message goes to both files
logzero.logger.debug("debug1")
# Reconfigure logger to remove logfile
logzero.setup_default_logger()
logzero.logger.debug("debug2")
# Reconfigure logger to add logfile
logzero.setup_default_logger(logfile=temp.name)
logzero.logger.debug("debug3")
# Reconfigure logger to set minimum loglevel to INFO
logzero.setup_default_logger(logfile=temp.name, level=logging.INFO)
logzero.logger.debug("debug4")
logzero.logger.info("info1")
# Reconfigure logger to set minimum loglevel back to DEBUG
logzero.setup_default_logger(logfile=temp.name, level=logging.DEBUG)
logzero.logger.debug("debug5")
with open(temp.name) as f:
content = f.read()
assert "] debug1" in content
assert "] debug2" not in content
assert "] debug3" in content
assert "] debug4" not in content
assert "] info1" in content
assert "] debug5" in content
with open(temp2.name) as f:
content = f.read()
assert "] debug1" in content
assert "] debug2" in content
assert "] debug3" in content
assert "] debug4" not in content
assert "] info1" in content
assert "] debug5" in content
finally:
temp.close()