rana-cli/wakatime/logger.py

119 lines
3.5 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
2015-10-23 06:15:57 +00:00
import traceback
2013-07-06 07:51:09 +00:00
2016-08-31 22:41:21 +00:00
from .compat import u
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
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 JsonFormatter(logging.Formatter):
def setup(self, timestamp, is_write, entity, version, plugin, verbose,
warnings=False):
2013-07-06 07:51:09 +00:00
self.timestamp = timestamp
self.is_write = is_write
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'] = u(self.version)
if self.plugin:
data['plugin'] = u(self.plugin)
data['time'] = self.timestamp
if self.verbose:
data['caller'] = u(record.pathname)
data['lineno'] = record.lineno
if self.is_write:
data['is_write'] = self.is_write
data['file'] = u(self.entity)
data['level'] = record.levelname
data['message'] = u(record.getMessage() if self.warnings else record.msg)
return json.dumps(data)
2013-07-06 07:51:09 +00:00
def traceback(self, lvl=None):
logger = logging.getLogger('WakaTime')
if not lvl:
lvl = logger.getEffectiveLevel()
logger.log(lvl, traceback.format_exc())
2015-10-23 06:15:57 +00:00
def formatException(self, exc_info):
raise RuntimeError('Use traceback method instead.')
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):
urllib3.disable_warnings()
logger = logging.getLogger('WakaTime')
2015-09-08 22:15:16 +00:00
for handler in logger.handlers:
logger.removeHandler(handler)
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))
formatter = JsonFormatter(datefmt='%Y/%m/%d %H:%M:%S %z')
formatter.setup(
2013-07-06 07:51:09 +00:00
timestamp=args.timestamp,
is_write=args.is_write,
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)
2015-10-23 06:15:57 +00:00
# add custom traceback logging method
logger.traceback = formatter.traceback
2015-10-23 06:15:57 +00:00
warnings_formatter = JsonFormatter(datefmt='%Y/%m/%d %H:%M:%S %z')
warnings_formatter.setup(
timestamp=args.timestamp,
is_write=args.is_write,
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: # pragma: nocover
pass # Python >= 2.7 is needed to capture warnings
2013-07-06 07:51:09 +00:00
return logger