本文整理汇总了Python中logzero.setup_default_logger方法的典型用法代码示例。如果您正苦于以下问题:Python logzero.setup_default_logger方法的具体用法?Python logzero.setup_default_logger怎么用?Python logzero.setup_default_logger使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类logzero
的用法示例。
在下文中一共展示了logzero.setup_default_logger方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setup_default_logger
# 需要导入模块: import logzero [as 别名]
# 或者: from logzero import setup_default_logger [as 别名]
def setup_default_logger(logfile=None, level=logging.DEBUG, formatter=None, maxBytes=0, backupCount=0, disableStderrLogger=False):
"""
Deprecated. Use `logzero.loglevel(..)`, `logzero.logfile(..)`, etc.
Globally reconfigures the default `logzero.logger` instance.
Usage:
.. code-block:: python
from logzero import logger, setup_default_logger
setup_default_logger(level=logging.WARN)
logger.info("hello") # this will not be displayed anymore because minimum loglevel was set to WARN
:arg string logfile: If set, also write logs to the specified filename.
:arg int level: Minimum `logging-level <https://docs.python.org/2/library/logging.html#logging-levels>`_ to display (default: `logging.DEBUG`).
:arg Formatter formatter: `Python logging Formatter object <https://docs.python.org/2/library/logging.html#formatter-objects>`_ (by default uses the internal LogFormatter).
:arg int maxBytes: Size of the logfile when rollover should occur. Defaults to 0, rollover never occurs.
:arg int backupCount: Number of backups to keep. Defaults to 0, rollover never occurs.
:arg bool disableStderrLogger: Should the default stderr logger be disabled. Defaults to False.
"""
global logger
logger = setup_logger(name=LOGZERO_DEFAULT_LOGGER, logfile=logfile, level=level, formatter=formatter, disableStderrLogger=disableStderrLogger)
return logger
示例2: setup_default_logger
# 需要导入模块: import logzero [as 别名]
# 或者: from logzero import setup_default_logger [as 别名]
def setup_default_logger(logfile=None, level=logging.DEBUG, formatter=None, maxBytes=0, backupCount=0):
"""
Deprecated. Use `logzero.loglevel(..)`, `logzero.logfile(..)`, etc.
Globally reconfigures the default `logzero.logger` instance.
Usage:
.. code-block:: python
from logzero import logger, setup_default_logger
setup_default_logger(level=logging.WARN)
logger.info("hello") # this will not be displayed anymore because minimum loglevel was set to WARN
:arg string logfile: If set, also write logs to the specified filename.
:arg int level: Minimum `logging-level <https://docs.python.org/2/library/logging.html#logging-levels>`_ to display (default: `logging.DEBUG`).
:arg Formatter formatter: `Python logging Formatter object <https://docs.python.org/2/library/logging.html#formatter-objects>`_ (by default uses the internal LogFormatter).
:arg int maxBytes: Size of the logfile when rollover should occur. Defaults to 0, rollover never occurs.
:arg int backupCount: Number of backups to keep. Defaults to 0, rollover never occurs.
"""
global logger
logger = setup_logger(name=LOGZERO_DEFAULT_LOGGER, logfile=logfile, level=level, formatter=formatter)
return logger
示例3: test_default_logger
# 需要导入模块: import logzero [as 别名]
# 或者: from logzero import setup_default_logger [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()
示例4: get_logger
# 需要导入模块: import logzero [as 别名]
# 或者: from logzero import setup_default_logger [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
示例5: configure_logger
# 需要导入模块: import logzero [as 别名]
# 或者: from logzero import setup_default_logger [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)
示例6: test_setup_logger_reconfiguration
# 需要导入模块: import logzero [as 别名]
# 或者: from logzero import setup_default_logger [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()