2013-07-06 07:51:09 +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 json
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
|
|
|
|
try:
|
|
|
|
from collections import OrderedDict
|
|
|
|
except ImportError:
|
|
|
|
from .packages.ordereddict import OrderedDict
|
|
|
|
|
|
|
|
|
|
|
|
class CustomEncoder(json.JSONEncoder):
|
|
|
|
|
|
|
|
def default(self, obj):
|
2013-07-30 06:04:43 +00:00
|
|
|
if isinstance(obj, bytes):
|
|
|
|
obj = bytes.decode(obj)
|
|
|
|
return json.dumps(obj)
|
2013-07-06 07:51:09 +00:00
|
|
|
return super(CustomEncoder, self).default(obj)
|
|
|
|
|
|
|
|
|
|
|
|
class JsonFormatter(logging.Formatter):
|
|
|
|
|
2013-07-09 20:34:22 +00:00
|
|
|
def setup(self, timestamp, endtime, isWrite, targetFile, version, plugin):
|
2013-07-06 07:51:09 +00:00
|
|
|
self.timestamp = timestamp
|
|
|
|
self.endtime = endtime
|
|
|
|
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),
|
2013-07-07 23:28:18 +00:00
|
|
|
('time', self.timestamp),
|
2013-07-06 07:51:09 +00:00
|
|
|
('endtime', self.endtime),
|
|
|
|
('isWrite', self.isWrite),
|
|
|
|
('file', self.targetFile),
|
|
|
|
('level', record.levelname),
|
|
|
|
('message', record.msg),
|
|
|
|
])
|
|
|
|
if not self.endtime:
|
|
|
|
del data['endtime']
|
|
|
|
if not self.plugin:
|
|
|
|
del data['plugin']
|
|
|
|
if not self.isWrite:
|
|
|
|
del data['isWrite']
|
|
|
|
return CustomEncoder().encode(data)
|
|
|
|
|
|
|
|
def formatException(self, exc_info):
|
|
|
|
return exec_info[2].format_exc()
|
|
|
|
|
|
|
|
|
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):
|
2013-08-08 09:09:54 +00:00
|
|
|
logger = logging.getLogger()
|
2013-08-09 02:11:15 +00:00
|
|
|
set_log_level(logger, args)
|
2013-08-08 09:09:54 +00:00
|
|
|
if len(logger.handlers) > 0:
|
|
|
|
return logger
|
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-07-12 07:09:10 +00:00
|
|
|
formatter = JsonFormatter(datefmt='%a %b %d %H:%M:%S %Z %Y')
|
2013-07-09 20:34:22 +00:00
|
|
|
formatter.setup(
|
2013-07-06 07:51:09 +00:00
|
|
|
timestamp=args.timestamp,
|
|
|
|
endtime=args.endtime,
|
|
|
|
isWrite=args.isWrite,
|
|
|
|
targetFile=args.targetFile,
|
|
|
|
version=version,
|
|
|
|
plugin=args.plugin,
|
|
|
|
)
|
|
|
|
handler.setFormatter(formatter)
|
|
|
|
logger.addHandler(handler)
|
|
|
|
return logger
|