本文整理汇总了Python中logging.html方法的典型用法代码示例。如果您正苦于以下问题:Python logging.html方法的具体用法?Python logging.html怎么用?Python logging.html使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类logging
的用法示例。
在下文中一共展示了logging.html方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: capture_output
# 需要导入模块: import logging [as 别名]
# 或者: from logging import html [as 别名]
def capture_output(log_level: int) -> bool:
"""Utility function used in subprocess for captured output.
Available log levels are: https://docs.python.org/3/library/logging.html#levels
10 is the value for Debug, so if it's not "DEBUG", return true and capture output.
Args:
log_level: the logging level
Returns:
Bool indicator on capturing output
"""
return log_level != 10
####################################################################################################
# CLEAN TRIAL RUNNING FUNCTIONS
####################################################################################################
示例2: modify_logger
# 需要导入模块: import logging [as 别名]
# 或者: from logging import html [as 别名]
def modify_logger(logger, log_file):
# refer: https://docs.python.org/3.5/library/logging.html#logrecord-attributes
formatter = logging.Formatter(
fmt='\n'.join([
'[%(name)s] %(asctime)s.%(msecs)d',
'\t%(pathname)s [line: %(lineno)d]',
'\t%(processName)s[%(process)d] => %(threadName)s[%(thread)d] => %(module)s.%(filename)s:%(funcName)s()',
'\t%(levelname)s: %(message)s\n'
]),
datefmt='%Y-%m-%d %H:%M:%S'
)
# stream_handler = logging.StreamHandler()
# stream_handler.setFormatter(formatter)
# logger.addHandler(stream_handler)
file_handler = logging.FileHandler(log_file, mode='a', encoding='utf-8')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
logger.setLevel(logging.DEBUG)
return logger
示例3: log
# 需要导入模块: import logging [as 别名]
# 或者: from logging import html [as 别名]
def log(self, msg, lvl=logging.INFO, *args, **kwargs):
"""Log a message within a set level
This method abstracts the logging.Logger.log() method. We use this
method during major state changes, errors, or critical events during
the optimization run.
You can check logging levels on this `link`_. In essence, DEBUG is 10,
INFO is 20, WARNING is 30, ERROR is 40, and CRITICAL is 50.
.. _link: https://docs.python.org/3/library/logging.html#logging-levels
Parameters
----------
msg : str
Message to be logged
lvl : int, optional
Logging level. Default is `logging.INFO`
"""
self.logger.log(lvl, msg, *args, **kwargs)
示例4: log_to_console
# 需要导入模块: import logging [as 别名]
# 或者: from logging import html [as 别名]
def log_to_console(status=True, level=None):
"""Log events to the console.
Parameters
----------
status: bool, optional, default=True
Whether logging to console should be turned on(True) or off(False)
level: string, optional, default=None
Level of logging; whichever level is chosen all higher levels
will be logged. See: https://docs.python.org/2/library/logging.html#levels
"""
set_log_level(level)
if status:
console_handler = logging.StreamHandler()
formatter = logging.Formatter(_LOGGER_FORMAT_STR)
console_handler.setFormatter(formatter)
_LOGGER.addHandler(console_handler)
else:
_remove_log_handler(logging.StreamHandler)
示例5: tlog
# 需要导入模块: import logging [as 别名]
# 或者: from logging import html [as 别名]
def tlog(self, level, text, *args):
"""
记录文本日志信息,较高级别的日志(FATAL)会被收集到总控统一存储以备后续追查。
实际的文本日志组装和输出是由标准库\
`logging — Logging facility for Python <https://docs.python.org/2.7/library/logging.html>`_\ 提供的
:param int level: 日志级别,级别包括:DEBUG < INFO < WARNING < ERROR < CRITICAL
:param obj text: 要输出的文本信息,通过python字符串的%语法可以获得类似c语言printf的效果
:param args: 格式化字符串中占位符对应的变量值,如果变量是对象则打印成json
:returns: 无返回
:rtype: None
"""
logger = logging.getLogger(self._log_name)
texts, json_args = self._format_request(logger.getEffectiveLevel(), text, args)
if len(json_args) > 0:
logger.log(level, texts, *json_args)
else:
logger.log(level, texts)
示例6: _formatTime
# 需要导入模块: import logging [as 别名]
# 或者: from logging import html [as 别名]
def _formatTime(self) -> str:
"""
Return the creation time of the specified log event as formatted text.
This code is borrowed from:
https://docs.python.org/3/library/logging.html#logging.Formatter.formatTime
The basic behaviour is as follows: an ISO8601-like (or RFC 3339-like) format is used.
This function uses `time.localtime()` to convert the creation time to a tuple.
"""
_converter = time.localtime
_formatter = logging.Formatter()
now = time.time()
msecs = (now - int(now)) * 1000
ct = _converter(now) # type: ignore
t = time.strftime(_formatter.default_time_format, ct)
s = _formatter.default_msec_format % (t, msecs)
return s
示例7: _heartbeat
# 需要导入模块: import logging [as 别名]
# 或者: from logging import html [as 别名]
def _heartbeat(self) -> None:
if not self.heartbeatInterval:
return
# check if `heartbeatInterval` seconds have passed.
# if they have, emit a heartbeat log record to the target handler
_now = time.monotonic()
_diff = _now - self._s_time
if _diff >= self.heartbeatInterval:
self._s_time = _now
# see: https://docs.python.org/3/library/logging.html#logging.LogRecord
record = logging.makeLogRecord(
{
"level": logging.INFO,
"name": "BreachHandler",
"pathname": ".../naz/naz/log.py",
"func": "BreachHandler._heartbeat",
"msg": {
"event": "naz.BreachHandler.heartbeat",
"heartbeatInterval": self.heartbeatInterval,
},
}
)
self.target.emit(record=record) # type: ignore # pytype: disable=attribute-error
示例8: setup_default_logger
# 需要导入模块: import logging [as 别名]
# 或者: from logging import html [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
示例9: loglevel
# 需要导入模块: import logging [as 别名]
# 或者: from logging import html [as 别名]
def loglevel(level=logging.DEBUG, update_custom_handlers=False):
"""
Set the minimum loglevel for the default logger (`logzero.logger`).
This reconfigures only the internal handlers of the default logger (eg. stream and logfile).
You can also update the loglevel for custom handlers by using `update_custom_handlers=True`.
:arg int level: Minimum `logging-level <https://docs.python.org/2/library/logging.html#logging-levels>`_ to display (default: `logging.DEBUG`).
:arg bool update_custom_handlers: If you added custom handlers to this logger and want this to update them too, you need to set `update_custom_handlers` to `True`
"""
logger.setLevel(level)
# Reconfigure existing internal handlers
for handler in list(logger.handlers):
if hasattr(handler, LOGZERO_INTERNAL_LOGGER_ATTR) or update_custom_handlers:
# Don't update the loglevel if this handler uses a custom one
if hasattr(handler, LOGZERO_INTERNAL_HANDLER_IS_CUSTOM_LOGLEVEL):
continue
# Update the loglevel for all default handlers
handler.setLevel(level)
global _loglevel
_loglevel = level
示例10: formatter
# 需要导入模块: import logging [as 别名]
# 或者: from logging import html [as 别名]
def formatter(formatter, update_custom_handlers=False):
"""
Set the formatter for all handlers of the default logger (``logzero.logger``).
This reconfigures only the logzero internal handlers by default, but you can also
reconfigure custom handlers by using ``update_custom_handlers=True``.
Beware that setting a formatter which uses colors also may write the color codes
to logfiles.
:arg Formatter formatter: `Python logging Formatter object <https://docs.python.org/2/library/logging.html#formatter-objects>`_ (by default uses the internal LogFormatter).
:arg bool update_custom_handlers: If you added custom handlers to this logger and want this to update them too, you need to set ``update_custom_handlers`` to `True`
"""
for handler in list(logger.handlers):
if hasattr(handler, LOGZERO_INTERNAL_LOGGER_ATTR) or update_custom_handlers:
handler.setFormatter(formatter)
global _formatter
_formatter = formatter
示例11: setup_default_logger
# 需要导入模块: import logging [as 别名]
# 或者: from logging import html [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
示例12: getLoggerPassFilters
# 需要导入模块: import logging [as 别名]
# 或者: from logging import html [as 别名]
def getLoggerPassFilters(loggerName: str) -> Logger:
"""
Returns a logger instance where the filters of all the parent chain are applied to it.
This is needed since Filters do NOT get inherited from parent logger to child logger.
See: https://docs.python.org/3/library/logging.html#filter-objects
"""
logger = logging.getLogger(loggerName)
subLoggerNames = loggerName.split(".")
filterList = []
parentLoggerName = ""
for subLoggerName in subLoggerNames:
parentLoggerName += subLoggerName
parentLogger = logging.getLogger(parentLoggerName)
filterList += parentLogger.filters
parentLoggerName += "."
[logger.addFilter(parentFilter) for parentFilter in filterList]
return logger
示例13: __init__
# 需要导入模块: import logging [as 别名]
# 或者: from logging import html [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
示例14: _json_default
# 需要导入模块: import logging [as 别名]
# 或者: from logging import html [as 别名]
def _json_default(obj):
"""
Coerce everything to strings.
All objects representing time get output as ISO8601.
"""
if isinstance(obj, (datetime.date, datetime.time, datetime.datetime)):
return obj.isoformat()
elif isinstance(obj, Exception):
return "Exception: %s" % str(obj)
return str(obj)
# skip natural LogRecord attributes
# http://docs.python.org/library/logging.html#logrecord-attributes
示例15: __init__
# 需要导入模块: import logging [as 别名]
# 或者: from logging import html [as 别名]
def __init__(self, *args, **kwargs):
"""
:param json_default: a function for encoding non-standard objects
as outlined in http://docs.python.org/2/library/json.html
:param json_encoder: optional custom encoder
:param json_serializer: a :meth:`json.dumps`-compatible callable
that will be used to serialize the log record.
:param prefix: an optional key prefix to nest logs
"""
self.json_default = kwargs.pop("json_default", _json_default)
self.json_encoder = kwargs.pop("json_encoder", None)
self.json_serializer = kwargs.pop("json_serializer", json.dumps)
self.default_values = kwargs.pop("default_extra", {})
self.prefix_key = kwargs.pop("prefix_key", "data")
logging.Formatter.__init__(self, *args, **kwargs)
self._fmt_parameters = self._parse_format_string()
self._skip_fields = set(self._fmt_parameters)
self._skip_fields.update(RESERVED_ATTRS)