2013-07-08 05:38:01 +04:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
2015-04-04 21:03:55 +03:00
|
|
|
wakatime.logger
|
|
|
|
~~~~~~~~~~~~~~~
|
2013-07-08 05:38:01 +04: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
|
2013-12-02 12:16:21 +04:00
|
|
|
import sys
|
2013-07-08 05:38:01 +04:00
|
|
|
|
2014-09-30 20:27:35 +04:00
|
|
|
from .compat import u
|
2013-07-08 05:38:01 +04:00
|
|
|
try:
|
2015-08-25 10:42:37 +03:00
|
|
|
from collections import OrderedDict # pragma: nocover
|
2015-09-29 13:11:25 +03:00
|
|
|
except ImportError: # pragma: nocover
|
|
|
|
from .packages.ordereddict import OrderedDict
|
2015-08-25 10:42:37 +03:00
|
|
|
try:
|
|
|
|
from .packages import simplejson as json # pragma: nocover
|
2015-09-29 13:11:25 +03:00
|
|
|
except (ImportError, SyntaxError): # pragma: nocover
|
|
|
|
import json
|
2013-07-08 05:38:01 +04:00
|
|
|
|
|
|
|
|
|
|
|
class CustomEncoder(json.JSONEncoder):
|
|
|
|
|
|
|
|
def default(self, obj):
|
2015-09-29 13:11:25 +03:00
|
|
|
if isinstance(obj, bytes): # pragma: nocover
|
|
|
|
obj = u(obj)
|
2015-04-01 01:44:48 +03:00
|
|
|
return json.dumps(obj)
|
2015-09-29 13:11:25 +03:00
|
|
|
try: # pragma: nocover
|
2013-11-21 13:09:37 +04:00
|
|
|
encoded = super(CustomEncoder, self).default(obj)
|
2015-09-29 13:11:25 +03:00
|
|
|
except UnicodeDecodeError: # pragma: nocover
|
2014-09-30 20:27:35 +04:00
|
|
|
obj = u(obj)
|
2013-11-21 13:09:37 +04:00
|
|
|
encoded = super(CustomEncoder, self).default(obj)
|
|
|
|
return encoded
|
2013-07-08 05:38:01 +04:00
|
|
|
|
|
|
|
|
|
|
|
class JsonFormatter(logging.Formatter):
|
|
|
|
|
2015-09-29 13:11:25 +03:00
|
|
|
def setup(self, timestamp, isWrite, entity, version, plugin, verbose,
|
2015-06-21 20:35:14 +03:00
|
|
|
warnings=False):
|
2013-07-08 05:38:01 +04:00
|
|
|
self.timestamp = timestamp
|
|
|
|
self.isWrite = isWrite
|
2015-09-29 13:11:25 +03:00
|
|
|
self.entity = entity
|
2013-07-08 05:38:01 +04:00
|
|
|
self.version = version
|
|
|
|
self.plugin = plugin
|
2015-05-16 01:33:03 +03:00
|
|
|
self.verbose = verbose
|
2015-06-21 20:35:14 +03:00
|
|
|
self.warnings = warnings
|
2013-07-08 05:38:01 +04:00
|
|
|
|
2015-06-21 20:35:14 +03:00
|
|
|
def format(self, record, *args):
|
2013-07-08 05:38:01 +04:00
|
|
|
data = OrderedDict([
|
|
|
|
('now', self.formatTime(record, self.datefmt)),
|
|
|
|
])
|
2015-05-06 22:30:26 +03:00
|
|
|
data['version'] = self.version
|
|
|
|
data['plugin'] = self.plugin
|
|
|
|
data['time'] = self.timestamp
|
2015-05-16 01:33:03 +03:00
|
|
|
if self.verbose:
|
|
|
|
data['caller'] = record.pathname
|
|
|
|
data['lineno'] = record.lineno
|
|
|
|
data['isWrite'] = self.isWrite
|
2015-09-29 13:11:25 +03:00
|
|
|
data['file'] = self.entity
|
2015-05-16 01:33:03 +03:00
|
|
|
if not self.isWrite:
|
|
|
|
del data['isWrite']
|
2015-05-06 22:30:26 +03:00
|
|
|
data['level'] = record.levelname
|
2015-06-21 20:35:14 +03:00
|
|
|
data['message'] = record.getMessage() if self.warnings else record.msg
|
2013-07-08 05:38:01 +04:00
|
|
|
if not self.plugin:
|
|
|
|
del data['plugin']
|
|
|
|
return CustomEncoder().encode(data)
|
|
|
|
|
|
|
|
def formatException(self, exc_info):
|
2015-03-10 01:23:29 +03:00
|
|
|
return sys.exec_info[2].format_exc()
|
2013-07-08 05:38:01 +04:00
|
|
|
|
|
|
|
|
2013-09-07 09:59:03 +04:00
|
|
|
def set_log_level(logger, args):
|
|
|
|
level = logging.WARN
|
|
|
|
if args.verbose:
|
|
|
|
level = logging.DEBUG
|
|
|
|
logger.setLevel(level)
|
|
|
|
|
|
|
|
|
2013-07-08 05:38:01 +04:00
|
|
|
def setup_logging(args, version):
|
2014-07-25 12:01:39 +04:00
|
|
|
logger = logging.getLogger('WakaTime')
|
2015-09-29 13:11:25 +03:00
|
|
|
for handler in logger.handlers:
|
|
|
|
logger.removeHandler(handler)
|
2013-09-07 09:59:03 +04:00
|
|
|
set_log_level(logger, args)
|
2013-07-08 05:38:01 +04:00
|
|
|
logfile = args.logfile
|
|
|
|
if not logfile:
|
|
|
|
logfile = '~/.wakatime.log'
|
|
|
|
handler = logging.FileHandler(os.path.expanduser(logfile))
|
2013-12-03 05:38:30 +04:00
|
|
|
formatter = JsonFormatter(datefmt='%Y/%m/%d %H:%M:%S %z')
|
2013-07-10 11:14:44 +04:00
|
|
|
formatter.setup(
|
2013-07-08 05:38:01 +04:00
|
|
|
timestamp=args.timestamp,
|
|
|
|
isWrite=args.isWrite,
|
2015-09-29 13:11:25 +03:00
|
|
|
entity=args.entity,
|
2013-07-08 05:38:01 +04:00
|
|
|
version=version,
|
|
|
|
plugin=args.plugin,
|
2015-05-16 01:33:03 +03:00
|
|
|
verbose=args.verbose,
|
2013-07-08 05:38:01 +04:00
|
|
|
)
|
|
|
|
handler.setFormatter(formatter)
|
|
|
|
logger.addHandler(handler)
|
2015-06-21 20:35:14 +03:00
|
|
|
|
|
|
|
warnings_formatter = JsonFormatter(datefmt='%Y/%m/%d %H:%M:%S %z')
|
|
|
|
warnings_formatter.setup(
|
|
|
|
timestamp=args.timestamp,
|
|
|
|
isWrite=args.isWrite,
|
2015-09-29 13:11:25 +03:00
|
|
|
entity=args.entity,
|
2015-06-21 20:35:14 +03: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-29 13:11:25 +03:00
|
|
|
except AttributeError: # pragma: nocover
|
2015-06-21 20:35:14 +03:00
|
|
|
pass # Python >= 2.7 is needed to capture warnings
|
|
|
|
|
2013-07-08 05:38:01 +04:00
|
|
|
return logger
|