sublime-wakatime/packages/wakatime/logger.py

116 lines
3.4 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
"""
2015-04-04 21:03:55 +03:00
wakatime.logger
~~~~~~~~~~~~~~~
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
2016-01-07 00:57:03 +03:00
import traceback
2016-09-02 11:50:54 +03:00
from .compat import u
2016-01-07 00:57:03 +03:00
from .packages.requests.packages import urllib3
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
class JsonFormatter(logging.Formatter):
2016-04-29 01:04:46 +03:00
def setup(self, timestamp, is_write, entity, version, plugin, verbose,
2015-06-21 20:35:14 +03:00
warnings=False):
self.timestamp = timestamp
2016-04-29 01:04:46 +03:00
self.is_write = is_write
2015-09-29 13:11:25 +03:00
self.entity = entity
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
2015-06-21 20:35:14 +03:00
def format(self, record, *args):
data = OrderedDict([
('now', self.formatTime(record, self.datefmt)),
])
2016-09-02 11:50:54 +03:00
data['version'] = u(self.version)
if self.plugin:
data['plugin'] = u(self.plugin)
data['time'] = self.timestamp
2015-05-16 01:33:03 +03:00
if self.verbose:
2016-09-02 11:50:54 +03:00
data['caller'] = u(record.pathname)
2015-05-16 01:33:03 +03:00
data['lineno'] = record.lineno
2016-09-02 11:50:54 +03:00
if self.is_write:
data['is_write'] = self.is_write
data['file'] = u(self.entity)
data['level'] = record.levelname
2016-09-02 11:50:54 +03:00
data['message'] = u(record.getMessage() if self.warnings else record.msg)
return json.dumps(data)
def traceback(self, lvl=None):
logger = logging.getLogger('WakaTime')
if not lvl:
lvl = logger.getEffectiveLevel()
logger.log(lvl, traceback.format_exc())
def set_log_level(logger, args):
level = logging.WARN
if args.verbose:
level = logging.DEBUG
logger.setLevel(level)
def setup_logging(args, version):
2016-01-07 00:57:03 +03:00
urllib3.disable_warnings()
logger = logging.getLogger('WakaTime')
2015-09-29 13:11:25 +03:00
for handler in logger.handlers:
logger.removeHandler(handler)
set_log_level(logger, args)
2018-01-05 10:33:07 +03:00
logfile = args.log_file
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(
timestamp=args.timestamp,
2016-04-29 01:04:46 +03:00
is_write=args.is_write,
2015-09-29 13:11:25 +03:00
entity=args.entity,
version=version,
plugin=args.plugin,
2015-05-16 01:33:03 +03:00
verbose=args.verbose,
)
handler.setFormatter(formatter)
logger.addHandler(handler)
2015-06-21 20:35:14 +03:00
2016-01-07 00:57:03 +03:00
# add custom traceback logging method
2016-09-02 11:50:54 +03:00
logger.traceback = formatter.traceback
2016-01-07 00:57:03 +03:00
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,
2016-04-29 01:04:46 +03:00
is_write=args.is_write,
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
return logger