我写了一个模块文件,根据Google Stackdriver json标准格式化日志。为此,我遵循了this guide。
这是我的模块:
import logging
import sys
from pythonjsonlogger import jsonlogger
class _MaxLevelFilter(object):
def __init__(self, highest_log_level):
self._highest_log_level = highest_log_level
def filter(self, log_record):
return log_record.levelno <= self._highest_log_level
class StackdriverJsonFormatter(jsonlogger.JsonFormatter, object):
def __init__(self, fmt="%(levelname) %(message)", style='%', *args, **kwargs):
jsonlogger.JsonFormatter.__init__(self, fmt=fmt, *args, **kwargs)
def process_log_record(self, log_record):
log_record['severity'] = log_record['levelname']
del log_record['levelname']
return super(StackdriverJsonFormatter, self).process_log_record(log_record)
formatter = StackdriverJsonFormatter()
"""
//////////////// INFO HANDLER ///////////////
"""
# A handler for low level logs that should be sent to STDOUT
info_handler = logging.StreamHandler(sys.stdout)
info_handler.setFormatter(formatter)
info_handler.setLevel(logging.INFO)
info_handler.addFilter(_MaxLevelFilter(logging.WARNING))
"""----------------------------------------"""
"""
//////////////// ERROR HANDLER ///////////////
"""
# A handler for high level logs that should be sent to STDERR
error_handler = logging.StreamHandler(sys.stderr)
error_handler.setFormatter(formatter)
error_handler.setLevel(logging.ERROR)
"""----------------------------------------"""
try:
root_logger = logging.getLogger('gunicorn.error')
except:
root_logger = logging.getLogger()
# root logger default level is WARNING, so we'll override to be DEBUG
root_logger.addHandler(info_handler)
root_logger.addHandler(error_handler)
它记录格式化的东西,虽然它复制了一个json格式的日志,另一个是文本格式:
[2018-04-17 12:40:48 +0000] [24255] [INFO] This is an INFO
{"message": "This is an INFO", "severity": "INFO"}
[2018-04-17 12:40:48 +0000] [24255] [ERROR] This is an ERROR
{"message": "This is an ERROR", "severity": "ERROR"}
关于我可能做错的任何想法?
答案 0 :(得分:0)
我在GCP StackDriver上获得了所有日志的ERROR
标签。我用下面的代码,它的工作。所有日志均已正确标记,没有重复的日志。
class StackDriverJsonFormatter(jsonlogger.JsonFormatter, object):
def __init__(self, fmt="%(levelname) %(message)", style='%', *args, **kwargs):
jsonlogger.JsonFormatter.__init__(self, fmt=fmt, *args, **kwargs)
def process_log_record(self, log_record):
log_record['severity'] = log_record['levelname']
del log_record['levelname']
return super(StackDriverJsonFormatter, self).process_log_record(log_record)
handler = logging.StreamHandler(sys.stdout)
formatter = StackDriverJsonFormatter()
handler.setFormatter(formatter)
log= logging.getLogger()
log.addHandler(handler)
log.setLevel(logging.INFO)