2013-07-06 07:51:09 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
2015-04-01 19:49:47 +00:00
|
|
|
wakatime.logger
|
|
|
|
~~~~~~~~~~~~~~~
|
2013-07-06 07:51:09 +00:00
|
|
|
|
|
|
|
Provides the configured logger for writing JSON to the log file.
|
|
|
|
|
|
|
|
:copyright: (c) 2013 Alan Hamlett.
|
|
|
|
:license: BSD, see LICENSE for more details.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import os
|
2015-10-23 06:15:57 +00:00
|
|
|
import traceback
|
2013-07-06 07:51:09 +00:00
|
|
|
|
2014-09-30 16:09:30 +00:00
|
|
|
from .compat import u
|
2015-10-23 07:41:46 +00:00
|
|
|
from .packages.requests.packages import urllib3
|
2013-07-06 07:51:09 +00:00
|
|
|
try:
|
2015-08-11 23:38:17 +00:00
|
|
|
from collections import OrderedDict # pragma: nocover
|
2015-09-27 01:56:33 +00:00
|
|
|
except ImportError: # pragma: nocover
|
|
|
|
from .packages.ordereddict import OrderedDict
|
2015-08-11 20:52:14 +00:00
|
|
|
try:
|
2015-08-11 23:38:17 +00:00
|
|
|
from .packages import simplejson as json # pragma: nocover
|
2015-09-08 03:57:21 +00:00
|
|
|
except (ImportError, SyntaxError): # pragma: nocover
|
|
|
|
import json
|
2013-07-06 07:51:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CustomEncoder(json.JSONEncoder):
|
|
|
|
|
|
|
|
def default(self, obj):
|
2015-09-08 21:15:35 +00:00
|
|
|
if isinstance(obj, bytes): # pragma: nocover
|
|
|
|
obj = u(obj)
|
2013-07-30 06:04:43 +00:00
|
|
|
return json.dumps(obj)
|
2015-09-08 22:15:16 +00:00
|
|
|
try: # pragma: nocover
|
2013-11-21 09:06:50 +00:00
|
|
|
encoded = super(CustomEncoder, self).default(obj)
|
2015-09-08 22:15:16 +00:00
|
|
|
except UnicodeDecodeError: # pragma: nocover
|
2014-09-30 16:09:30 +00:00
|
|
|
obj = u(obj)
|
2013-11-21 09:06:50 +00:00
|
|
|
encoded = super(CustomEncoder, self).default(obj)
|
|
|
|
return encoded
|
2013-07-06 07:51:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class JsonFormatter(logging.Formatter):
|
|
|
|
|
2015-08-30 01:14:07 +00:00
|
|
|
def setup(self, timestamp, isWrite, entity, version, plugin, verbose,
|
2015-05-31 17:09:39 +00:00
|
|
|
warnings=False):
|
2013-07-06 07:51:09 +00:00
|
|
|
self.timestamp = timestamp
|
|
|
|
self.isWrite = isWrite
|
2015-08-30 01:14:07 +00:00
|
|
|
self.entity = entity
|
2013-07-06 07:51:09 +00:00
|
|
|
self.version = version
|
|
|
|
self.plugin = plugin
|
2015-05-13 10:00:00 +00:00
|
|
|
self.verbose = verbose
|
2015-05-31 17:09:39 +00:00
|
|
|
self.warnings = warnings
|
2013-07-06 07:51:09 +00:00
|
|
|
|
2015-05-31 17:09:39 +00:00
|
|
|
def format(self, record, *args):
|
2013-07-06 07:51:09 +00:00
|
|
|
data = OrderedDict([
|
2013-12-03 01:36:52 +00:00
|
|
|
('now', self.formatTime(record, self.datefmt)),
|
2013-07-06 07:51:09 +00:00
|
|
|
])
|
2015-05-05 21:27:09 +00:00
|
|
|
data['version'] = self.version
|
|
|
|
data['plugin'] = self.plugin
|
|
|
|
data['time'] = self.timestamp
|
2015-05-13 10:00:00 +00:00
|
|
|
if self.verbose:
|
|
|
|
data['caller'] = record.pathname
|
|
|
|
data['lineno'] = record.lineno
|
|
|
|
data['isWrite'] = self.isWrite
|
2015-08-30 01:14:07 +00:00
|
|
|
data['file'] = self.entity
|
2015-05-13 10:00:00 +00:00
|
|
|
if not self.isWrite:
|
|
|
|
del data['isWrite']
|
2015-05-05 21:27:09 +00:00
|
|
|
data['level'] = record.levelname
|
2015-05-31 17:09:39 +00:00
|
|
|
data['message'] = record.getMessage() if self.warnings else record.msg
|
2013-07-06 07:51:09 +00:00
|
|
|
if not self.plugin:
|
|
|
|
del data['plugin']
|
|
|
|
return CustomEncoder().encode(data)
|
|
|
|
|
2015-10-23 06:15:57 +00:00
|
|
|
|
|
|
|
def traceback_formatter(*args, **kwargs):
|
2016-02-18 19:48:11 +00:00
|
|
|
if 'level' in kwargs and (kwargs['level'].lower() == 'warn' or kwargs['level'].lower() == 'warning'):
|
|
|
|
logging.getLogger('WakaTime').warning(traceback.format_exc())
|
|
|
|
elif 'level' in kwargs and kwargs['level'].lower() == 'info':
|
|
|
|
logging.getLogger('WakaTime').info(traceback.format_exc())
|
|
|
|
elif 'level' in kwargs and kwargs['level'].lower() == 'debug':
|
|
|
|
logging.getLogger('WakaTime').debug(traceback.format_exc())
|
|
|
|
else:
|
|
|
|
logging.getLogger('WakaTime').error(traceback.format_exc())
|
2013-07-06 07:51:09 +00:00
|
|
|
|
|
|
|
|
2013-08-09 02:11:15 +00:00
|
|
|
def set_log_level(logger, args):
|
|
|
|
level = logging.WARN
|
|
|
|
if args.verbose:
|
|
|
|
level = logging.DEBUG
|
|
|
|
logger.setLevel(level)
|
|
|
|
|
|
|
|
|
2013-07-06 07:51:09 +00:00
|
|
|
def setup_logging(args, version):
|
2015-10-23 07:41:46 +00:00
|
|
|
urllib3.disable_warnings()
|
2014-07-25 07:59:25 +00:00
|
|
|
logger = logging.getLogger('WakaTime')
|
2015-09-08 22:15:16 +00:00
|
|
|
for handler in logger.handlers:
|
|
|
|
logger.removeHandler(handler)
|
2013-08-09 02:11:15 +00:00
|
|
|
set_log_level(logger, args)
|
2013-07-06 07:51:09 +00:00
|
|
|
logfile = args.logfile
|
|
|
|
if not logfile:
|
|
|
|
logfile = '~/.wakatime.log'
|
|
|
|
handler = logging.FileHandler(os.path.expanduser(logfile))
|
2013-12-03 01:34:00 +00:00
|
|
|
formatter = JsonFormatter(datefmt='%Y/%m/%d %H:%M:%S %z')
|
2013-07-09 20:34:22 +00:00
|
|
|
formatter.setup(
|
2013-07-06 07:51:09 +00:00
|
|
|
timestamp=args.timestamp,
|
|
|
|
isWrite=args.isWrite,
|
2015-08-30 01:14:07 +00:00
|
|
|
entity=args.entity,
|
2013-07-06 07:51:09 +00:00
|
|
|
version=version,
|
|
|
|
plugin=args.plugin,
|
2015-05-13 10:00:00 +00:00
|
|
|
verbose=args.verbose,
|
2013-07-06 07:51:09 +00:00
|
|
|
)
|
|
|
|
handler.setFormatter(formatter)
|
|
|
|
logger.addHandler(handler)
|
2015-05-31 17:09:39 +00:00
|
|
|
|
2015-10-23 06:15:57 +00:00
|
|
|
# add custom traceback logging method
|
|
|
|
logger.traceback = traceback_formatter
|
|
|
|
|
2015-05-31 17:09:39 +00:00
|
|
|
warnings_formatter = JsonFormatter(datefmt='%Y/%m/%d %H:%M:%S %z')
|
|
|
|
warnings_formatter.setup(
|
|
|
|
timestamp=args.timestamp,
|
|
|
|
isWrite=args.isWrite,
|
2015-08-30 01:14:07 +00:00
|
|
|
entity=args.entity,
|
2015-05-31 17:09:39 +00:00
|
|
|
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)
|
2015-09-27 02:10:12 +00:00
|
|
|
except AttributeError: # pragma: nocover
|
2015-05-31 17:09:39 +00:00
|
|
|
pass # Python >= 2.7 is needed to capture warnings
|
|
|
|
|
2013-07-06 07:51:09 +00:00
|
|
|
return logger
|