rana-cli/wakatime/logger.py

134 lines
3.9 KiB
Python
Raw Normal View History

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
import sys
2013-07-06 07:51:09 +00:00
from .compat import u
2013-07-06 07:51:09 +00:00
try:
2015-08-11 23:38:17 +00:00
from collections import OrderedDict # pragma: nocover
2013-07-06 07:51:09 +00:00
except ImportError:
2015-08-11 23:38:17 +00:00
from .packages.ordereddict import OrderedDict # pragma: nocover
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):
2013-07-30 06:04:43 +00:00
if isinstance(obj, bytes):
obj = bytes.decode(obj)
return json.dumps(obj)
try:
encoded = super(CustomEncoder, self).default(obj)
except UnicodeDecodeError:
obj = u(obj)
encoded = super(CustomEncoder, self).default(obj)
return encoded
2013-07-06 07:51:09 +00:00
class JsonFormatter(logging.Formatter):
def setup(self, timestamp, isWrite, entity, version, plugin, verbose,
warnings=False):
2013-07-06 07:51:09 +00:00
self.timestamp = timestamp
self.isWrite = isWrite
self.entity = entity
2013-07-06 07:51:09 +00:00
self.version = version
self.plugin = plugin
self.verbose = verbose
self.warnings = warnings
2013-07-06 07:51:09 +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
])
data['version'] = self.version
data['plugin'] = self.plugin
data['time'] = self.timestamp
if self.verbose:
data['caller'] = record.pathname
data['lineno'] = record.lineno
data['isWrite'] = self.isWrite
data['file'] = self.entity
if not self.isWrite:
del data['isWrite']
data['level'] = record.levelname
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)
def formatException(self, exc_info):
return sys.exec_info[2].format_exc()
2013-07-06 07:51:09 +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):
logger = logging.getLogger('WakaTime')
set_log_level(logger, args)
if len(logger.handlers) > 0:
formatter = JsonFormatter(datefmt='%Y/%m/%d %H:%M:%S %z')
formatter.setup(
timestamp=args.timestamp,
isWrite=args.isWrite,
entity=args.entity,
version=version,
plugin=args.plugin,
verbose=args.verbose,
)
logger.handlers[0].setFormatter(formatter)
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))
formatter = JsonFormatter(datefmt='%Y/%m/%d %H:%M:%S %z')
formatter.setup(
2013-07-06 07:51:09 +00:00
timestamp=args.timestamp,
isWrite=args.isWrite,
entity=args.entity,
2013-07-06 07:51:09 +00:00
version=version,
plugin=args.plugin,
verbose=args.verbose,
2013-07-06 07:51:09 +00:00
)
handler.setFormatter(formatter)
logger.addHandler(handler)
warnings_formatter = JsonFormatter(datefmt='%Y/%m/%d %H:%M:%S %z')
warnings_formatter.setup(
timestamp=args.timestamp,
isWrite=args.isWrite,
entity=args.entity,
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
2013-07-06 07:51:09 +00:00
return logger