correctly log message from py.warnings module
This commit is contained in:
parent
2cdbf6e763
commit
7f6b6c9c1f
1 changed files with 24 additions and 5 deletions
|
@ -37,15 +37,17 @@ class CustomEncoder(json.JSONEncoder):
|
||||||
|
|
||||||
class JsonFormatter(logging.Formatter):
|
class JsonFormatter(logging.Formatter):
|
||||||
|
|
||||||
def setup(self, timestamp, isWrite, targetFile, version, plugin, verbose):
|
def setup(self, timestamp, isWrite, targetFile, version, plugin, verbose,
|
||||||
|
warnings=False):
|
||||||
self.timestamp = timestamp
|
self.timestamp = timestamp
|
||||||
self.isWrite = isWrite
|
self.isWrite = isWrite
|
||||||
self.targetFile = targetFile
|
self.targetFile = targetFile
|
||||||
self.version = version
|
self.version = version
|
||||||
self.plugin = plugin
|
self.plugin = plugin
|
||||||
self.verbose = verbose
|
self.verbose = verbose
|
||||||
|
self.warnings = warnings
|
||||||
|
|
||||||
def format(self, record):
|
def format(self, record, *args):
|
||||||
data = OrderedDict([
|
data = OrderedDict([
|
||||||
('now', self.formatTime(record, self.datefmt)),
|
('now', self.formatTime(record, self.datefmt)),
|
||||||
])
|
])
|
||||||
|
@ -60,7 +62,7 @@ class JsonFormatter(logging.Formatter):
|
||||||
if not self.isWrite:
|
if not self.isWrite:
|
||||||
del data['isWrite']
|
del data['isWrite']
|
||||||
data['level'] = record.levelname
|
data['level'] = record.levelname
|
||||||
data['message'] = record.msg
|
data['message'] = record.getMessage() if self.warnings else record.msg
|
||||||
if not self.plugin:
|
if not self.plugin:
|
||||||
del data['plugin']
|
del data['plugin']
|
||||||
return CustomEncoder().encode(data)
|
return CustomEncoder().encode(data)
|
||||||
|
@ -77,7 +79,6 @@ def set_log_level(logger, args):
|
||||||
|
|
||||||
|
|
||||||
def setup_logging(args, version):
|
def setup_logging(args, version):
|
||||||
logging.captureWarnings(True)
|
|
||||||
logger = logging.getLogger('WakaTime')
|
logger = logging.getLogger('WakaTime')
|
||||||
set_log_level(logger, args)
|
set_log_level(logger, args)
|
||||||
if len(logger.handlers) > 0:
|
if len(logger.handlers) > 0:
|
||||||
|
@ -107,5 +108,23 @@ def setup_logging(args, version):
|
||||||
)
|
)
|
||||||
handler.setFormatter(formatter)
|
handler.setFormatter(formatter)
|
||||||
logger.addHandler(handler)
|
logger.addHandler(handler)
|
||||||
logging.getLogger('py.warnings').addHandler(handler)
|
|
||||||
|
warnings_formatter = JsonFormatter(datefmt='%Y/%m/%d %H:%M:%S %z')
|
||||||
|
warnings_formatter.setup(
|
||||||
|
timestamp=args.timestamp,
|
||||||
|
isWrite=args.isWrite,
|
||||||
|
targetFile=args.targetFile,
|
||||||
|
version=version,
|
||||||
|
plugin=args.plugin,
|
||||||
|
verbose=args.verbose,
|
||||||
|
warnings=True,
|
||||||
|
)
|
||||||
|
warnings_handler = logging.FileHandler(os.path.expanduser(logfile))
|
||||||
|
warnings_handler.setFormatter(warnings_formatter)
|
||||||
|
logging.getLogger('py.warnings').addHandler(warnings_handler)
|
||||||
|
try:
|
||||||
|
logging.captureWarnings(True)
|
||||||
|
except AttributeError:
|
||||||
|
pass # Python >= 2.7 is needed to capture warnings
|
||||||
|
|
||||||
return logger
|
return logger
|
||||||
|
|
Loading…
Reference in a new issue