2013-07-08 01:38:01 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
wakatime.log
|
|
|
|
~~~~~~~~~~~~
|
|
|
|
|
|
|
|
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 08:16:21 +00:00
|
|
|
import sys
|
2013-07-08 01:38:01 +00:00
|
|
|
|
2013-09-07 05:59:03 +00:00
|
|
|
from .packages import simplejson as json
|
2014-09-30 16:27:35 +00:00
|
|
|
from .compat import u
|
2013-07-08 01:38:01 +00:00
|
|
|
try:
|
|
|
|
from collections import OrderedDict
|
|
|
|
except ImportError:
|
|
|
|
from .packages.ordereddict import OrderedDict
|
|
|
|
|
|
|
|
|
|
|
|
class CustomEncoder(json.JSONEncoder):
|
|
|
|
|
|
|
|
def default(self, obj):
|
2013-07-30 06:11:07 +00:00
|
|
|
if isinstance(obj, bytes):
|
|
|
|
obj = bytes.decode(obj)
|
|
|
|
return json.dumps(obj)
|
2013-11-21 09:09:37 +00:00
|
|
|
try:
|
|
|
|
encoded = super(CustomEncoder, self).default(obj)
|
|
|
|
except UnicodeDecodeError:
|
2014-09-30 16:27:35 +00:00
|
|
|
obj = u(obj)
|
2013-11-21 09:09:37 +00:00
|
|
|
encoded = super(CustomEncoder, self).default(obj)
|
|
|
|
return encoded
|
2013-07-08 01:38:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class JsonFormatter(logging.Formatter):
|
|
|
|
|
2013-09-22 22:04:24 +00:00
|
|
|
def setup(self, timestamp, isWrite, targetFile, version, plugin):
|
2013-07-08 01:38:01 +00:00
|
|
|
self.timestamp = timestamp
|
|
|
|
self.isWrite = isWrite
|
|
|
|
self.targetFile = targetFile
|
|
|
|
self.version = version
|
|
|
|
self.plugin = plugin
|
|
|
|
|
|
|
|
def format(self, record):
|
|
|
|
data = OrderedDict([
|
|
|
|
('now', self.formatTime(record, self.datefmt)),
|
|
|
|
('version', self.version),
|
|
|
|
('plugin', self.plugin),
|
|
|
|
('time', self.timestamp),
|
|
|
|
('isWrite', self.isWrite),
|
|
|
|
('file', self.targetFile),
|
|
|
|
('level', record.levelname),
|
|
|
|
('message', record.msg),
|
|
|
|
])
|
|
|
|
if not self.plugin:
|
|
|
|
del data['plugin']
|
|
|
|
if not self.isWrite:
|
|
|
|
del data['isWrite']
|
|
|
|
return CustomEncoder().encode(data)
|
|
|
|
|
|
|
|
def formatException(self, exc_info):
|
2015-03-09 22:23:29 +00:00
|
|
|
return sys.exec_info[2].format_exc()
|
2013-07-08 01:38:01 +00:00
|
|
|
|
|
|
|
|
2013-09-07 05:59:03 +00:00
|
|
|
def set_log_level(logger, args):
|
|
|
|
level = logging.WARN
|
|
|
|
if args.verbose:
|
|
|
|
level = logging.DEBUG
|
|
|
|
logger.setLevel(level)
|
|
|
|
|
|
|
|
|
2013-07-08 01:38:01 +00:00
|
|
|
def setup_logging(args, version):
|
2014-07-25 08:01:39 +00:00
|
|
|
logger = logging.getLogger('WakaTime')
|
2013-09-07 05:59:03 +00:00
|
|
|
set_log_level(logger, args)
|
2013-08-08 09:17:47 +00:00
|
|
|
if len(logger.handlers) > 0:
|
2013-12-03 01:38:30 +00:00
|
|
|
formatter = JsonFormatter(datefmt='%Y/%m/%d %H:%M:%S %z')
|
2013-09-22 22:04:24 +00:00
|
|
|
formatter.setup(
|
|
|
|
timestamp=args.timestamp,
|
|
|
|
isWrite=args.isWrite,
|
|
|
|
targetFile=args.targetFile,
|
|
|
|
version=version,
|
|
|
|
plugin=args.plugin,
|
|
|
|
)
|
|
|
|
logger.handlers[0].setFormatter(formatter)
|
2013-08-08 09:17:47 +00:00
|
|
|
return logger
|
2013-07-08 01:38:01 +00:00
|
|
|
logfile = args.logfile
|
|
|
|
if not logfile:
|
|
|
|
logfile = '~/.wakatime.log'
|
|
|
|
handler = logging.FileHandler(os.path.expanduser(logfile))
|
2013-12-03 01:38:30 +00:00
|
|
|
formatter = JsonFormatter(datefmt='%Y/%m/%d %H:%M:%S %z')
|
2013-07-10 07:14:44 +00:00
|
|
|
formatter.setup(
|
2013-07-08 01:38:01 +00:00
|
|
|
timestamp=args.timestamp,
|
|
|
|
isWrite=args.isWrite,
|
|
|
|
targetFile=args.targetFile,
|
|
|
|
version=version,
|
|
|
|
plugin=args.plugin,
|
|
|
|
)
|
|
|
|
handler.setFormatter(formatter)
|
|
|
|
logger.addHandler(handler)
|
|
|
|
return logger
|