本文整理匯總了Python中pythonjsonlogger.jsonlogger.JsonFormatter方法的典型用法代碼示例。如果您正苦於以下問題:Python jsonlogger.JsonFormatter方法的具體用法?Python jsonlogger.JsonFormatter怎麽用?Python jsonlogger.JsonFormatter使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pythonjsonlogger.jsonlogger
的用法示例。
在下文中一共展示了jsonlogger.JsonFormatter方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from pythonjsonlogger import jsonlogger [as 別名]
# 或者: from pythonjsonlogger.jsonlogger import JsonFormatter [as 別名]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Use a unique name for the logger so that multiple instances of EventLog do not write
# to each other's handlers.
log_name = __name__ + '.' + str(id(self))
self.log = logging.getLogger(log_name)
# We don't want events to show up in the default logs
self.log.propagate = False
# We will use log.info to emit
self.log.setLevel(logging.INFO)
if self.handlers:
formatter = jsonlogger.JsonFormatter(json_serializer=_skip_message)
for handler in self.handlers:
handler.setFormatter(formatter)
self.log.addHandler(handler)
self.schemas = {}
示例2: make_logger
# 需要導入模塊: from pythonjsonlogger import jsonlogger [as 別名]
# 或者: from pythonjsonlogger.jsonlogger import JsonFormatter [as 別名]
def make_logger():
logger = logging.getLogger('DownloaderForReddit')
logger.setLevel(logging.DEBUG)
stream_formatter = JsonStreamFormatter('%(asctime)s: %(levelname)s : %(name)s : %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p')
json_formatter = jsonlogger.JsonFormatter(fmt='%(levelname) %(name) %(filename) %(module) %(funcName) %(lineno) '
'%(message) %(asctime)', datefmt='%m/%d/%Y %I:%M:%S %p')
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.DEBUG)
stream_handler.setFormatter(stream_formatter)
log_path = os.path.join(SystemUtil.get_data_directory(), 'DownloaderForReddit.log')
file_handler = RotatingFileHandler(log_path, maxBytes=1024*1024, backupCount=2)
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(json_formatter)
logger.addHandler(stream_handler)
logger.addHandler(file_handler)
示例3: __init__
# 需要導入模塊: from pythonjsonlogger import jsonlogger [as 別名]
# 或者: from pythonjsonlogger.jsonlogger import JsonFormatter [as 別名]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.log = logging.getLogger(__name__)
# We don't want events to show up in the default logs
self.log.propagate = False
self.log.setLevel(logging.INFO)
if self.handlers_maker:
self.handlers = self.handlers_maker(self)
formatter = jsonlogger.JsonFormatter(json_serializer=_skip_message)
for handler in self.handlers:
handler.setFormatter(formatter)
self.log.addHandler(handler)
self.schemas = {}
示例4: configure_logging
# 需要導入模塊: from pythonjsonlogger import jsonlogger [as 別名]
# 或者: from pythonjsonlogger.jsonlogger import JsonFormatter [as 別名]
def configure_logging(app):
from pythonjsonlogger import jsonlogger
if os.environ.get("SUPPRESS_LOGGING"):
while app.logger.handlers:
app.logger.removeHandler(app.logger.handlers[0])
handler = logging.NullHandler()
app.logger.addHandler(handler)
elif not app.config["DEBUG"]:
while app.logger.handlers:
app.logger.removeHandler(app.logger.handlers[0])
handler = logging.StreamHandler()
handler.setFormatter(
jsonlogger.JsonFormatter("%(message)%(levelname)%(name)%(asctime)")
)
app.logger.addHandler(handler)
if app.config.get("LOG_LEVEL"):
app.logger.setLevel(getattr(logging, app.config["LOG_LEVEL"].upper()))
示例5: _get_formatter
# 需要導入模塊: from pythonjsonlogger import jsonlogger [as 別名]
# 或者: from pythonjsonlogger.jsonlogger import JsonFormatter [as 別名]
def _get_formatter(self, json):
'''
Return the proper log formatter
@param json: Boolean value
'''
if json:
return jsonlogger.JsonFormatter()
else:
return logging.Formatter(self.format_string)
示例6: __init__
# 需要導入模塊: from pythonjsonlogger import jsonlogger [as 別名]
# 或者: from pythonjsonlogger.jsonlogger import JsonFormatter [as 別名]
def __init__(self, fmt="%(levelname) %(message)", *args, **kwargs):
jsonlogger.JsonFormatter.__init__(self, fmt=fmt, *args, **kwargs)
示例7: setup_logging
# 需要導入模塊: from pythonjsonlogger import jsonlogger [as 別名]
# 或者: from pythonjsonlogger.jsonlogger import JsonFormatter [as 別名]
def setup_logging(log_level):
logger = logging.getLogger(__name__)
logger.setLevel(log_level)
json_handler = logging.StreamHandler()
formatter = jsonlogger.JsonFormatter(
fmt='%(asctime)s %(levelname)s %(name)s %(message)s'
)
json_handler.setFormatter(formatter)
logger.addHandler(json_handler)
示例8: _create_json_formatter
# 需要導入模塊: from pythonjsonlogger import jsonlogger [as 別名]
# 或者: from pythonjsonlogger.jsonlogger import JsonFormatter [as 別名]
def _create_json_formatter() -> logging.Formatter: # pragma: no cover
supported_keys = [
"asctime",
# 'created',
"filename",
"funcName",
"levelname",
# 'levelno',
"lineno",
"module",
# 'msecs',
"message",
"name",
"pathname",
# 'process',
# 'processName',
# 'relativeCreated',
# 'thread',
# 'threadName'
]
def log_format(x):
"""Used to give JsonFormatter proper parameter format"""
return ["%({0:s})".format(i) for i in x]
custom_format = " ".join(log_format(supported_keys))
return jsonlogger.JsonFormatter(custom_format)
示例9: setup_logging
# 需要導入模塊: from pythonjsonlogger import jsonlogger [as 別名]
# 或者: from pythonjsonlogger.jsonlogger import JsonFormatter [as 別名]
def setup_logging(home, verbose):
"""Configure logging for honeycomb."""
logging.setLoggerClass(MyLogger)
logging.config.dictConfig({
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"console": {
"format": "%(levelname)-8s [%(asctime)s %(name)s] %(filename)s:%(lineno)s %(funcName)s: %(message)s",
},
"json": {
"()": jsonlogger.JsonFormatter,
"format": "%(levelname)s %(asctime)s %(name)s %(filename)s %(lineno)s %(funcName)s %(message)s",
},
},
"handlers": {
"default": {
"level": "DEBUG" if verbose else "INFO",
"class": "logging.StreamHandler",
"formatter": "console",
},
"file": {
"level": "DEBUG",
"class": "logging.handlers.WatchedFileHandler",
"filename": os.path.join(home, DEBUG_LOG_FILE),
"formatter": "json",
},
},
"loggers": {
"": {
"handlers": ["default", "file"],
"level": "DEBUG",
"propagate": True,
},
}
})
示例10: make_logger
# 需要導入模塊: from pythonjsonlogger import jsonlogger [as 別名]
# 或者: from pythonjsonlogger.jsonlogger import JsonFormatter [as 別名]
def make_logger(scraper):
""" Create two log handlers, one to output info-level ouput to the
console, the other to store all logging in a JSON file which will
later be used to generate reports. """
logger = logging.getLogger('')
logger.setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests")
requests_log.setLevel(logging.WARNING)
json_handler = logging.FileHandler(log_path(scraper))
json_handler.setLevel(logging.DEBUG)
json_formatter = jsonlogger.JsonFormatter(make_json_format())
json_handler.setFormatter(json_formatter)
logger.addHandler(json_handler)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
fmt = '%(name)s [%(levelname)-8s]: %(message)s'
formatter = logging.Formatter(fmt)
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
logger = logging.getLogger(scraper.name)
logger = TaskAdapter(logger, scraper)
return logger
示例11: __init__
# 需要導入模塊: from pythonjsonlogger import jsonlogger [as 別名]
# 或者: from pythonjsonlogger.jsonlogger import JsonFormatter [as 別名]
def __init__(self,
fmt="%(asctime) %(name) %(processName) %(filename) %(funcName) %(levelname) %(lineno) %(module) %(threadName) %(message)",
datefmt="%Y-%m-%dT%H:%M:%SZ%z",
style='%',
extra={}, *args, **kwargs):
self._extra = extra
jsonlogger.JsonFormatter.__init__(self, fmt=fmt, datefmt=datefmt, *args, **kwargs)
示例12: process_log_record
# 需要導入模塊: from pythonjsonlogger import jsonlogger [as 別名]
# 或者: from pythonjsonlogger.jsonlogger import JsonFormatter [as 別名]
def process_log_record(self, log_record):
# Enforce the presence of a timestamp
if "asctime" in log_record:
log_record["timestamp"] = log_record["asctime"]
else:
log_record["timestamp"] = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%fZ%z")
if self._extra is not None:
for key, value in self._extra.items():
log_record[key] = value
return super(JsonFormatter, self).process_log_record(log_record)
# Derive from object to force a new-style class and thus allow super() to work
# on Python 2.6
示例13: process_log_record
# 需要導入模塊: from pythonjsonlogger import jsonlogger [as 別名]
# 或者: from pythonjsonlogger.jsonlogger import JsonFormatter [as 別名]
def process_log_record(self, log_record):
"""Add customer record keys and rename threadName key."""
log_record["version"] = __version__
log_record["program"] = PROGRAM_NAME
log_record["service_name"] = log_record.pop('threadName', None)
# return jsonlogger.JsonFormatter.process_log_record(self, log_record)
return log_record
示例14: __init__
# 需要導入模塊: from pythonjsonlogger import jsonlogger [as 別名]
# 或者: from pythonjsonlogger.jsonlogger import JsonFormatter [as 別名]
def __init__(self, fmt="%(levelname) %(name) %(message)", style='%', *args, **kwargs):
jsonlogger.JsonFormatter.__init__(self, fmt=fmt, *args, **kwargs)
示例15: initialize
# 需要導入模塊: from pythonjsonlogger import jsonlogger [as 別名]
# 或者: from pythonjsonlogger.jsonlogger import JsonFormatter [as 別名]
def initialize(self):
"""Init repo2docker configuration before start"""
# FIXME: Remove this function, move it to setters / traitlet reactors
if self.json_logs:
# register JSON excepthook to avoid non-JSON output on errors
sys.excepthook = self.json_excepthook
# Need to reset existing handlers, or we repeat messages
logHandler = logging.StreamHandler()
formatter = jsonlogger.JsonFormatter()
logHandler.setFormatter(formatter)
self.log = logging.getLogger("repo2docker")
self.log.handlers = []
self.log.addHandler(logHandler)
self.log.setLevel(self.log_level)
else:
# due to json logger stuff above,
# our log messages include carriage returns, newlines, etc.
# remove the additional newline from the stream handler
self.log.handlers[0].terminator = ""
# We don't want a [Repo2Docker] on all messages
self.log.handlers[0].formatter = logging.Formatter(fmt="%(message)s")
if self.dry_run and (self.run or self.push):
raise ValueError("Cannot push or run image if we are not building it")
if self.volumes and not self.run:
raise ValueError("Cannot mount volumes if container is not run")