sublime-rana/packages/wakatime/logger.py

124 lines
3.6 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
"""
2015-04-04 18:03:55 +00: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
import sys
2014-09-30 16:27:35 +00:00
from .compat import u
try:
2015-08-25 07:42:37 +00:00
from collections import OrderedDict # pragma: nocover
2015-09-29 10:11:25 +00:00
except ImportError: # pragma: nocover
from .packages.ordereddict import OrderedDict
2015-08-25 07:42:37 +00:00
try:
from .packages import simplejson as json # pragma: nocover
2015-09-29 10:11:25 +00:00
except (ImportError, SyntaxError): # pragma: nocover
import json
class CustomEncoder(json.JSONEncoder):
def default(self, obj):
2015-09-29 10:11:25 +00:00
if isinstance(obj, bytes): # pragma: nocover
obj = u(obj)
2015-03-31 22:44:48 +00:00
return json.dumps(obj)
2015-09-29 10:11:25 +00:00
try: # pragma: nocover
encoded = super(CustomEncoder, self).default(obj)
2015-09-29 10:11:25 +00:00
except UnicodeDecodeError: # pragma: nocover
2014-09-30 16:27:35 +00:00
obj = u(obj)
encoded = super(CustomEncoder, self).default(obj)
return encoded
class JsonFormatter(logging.Formatter):
2015-09-29 10:11:25 +00:00
def setup(self, timestamp, isWrite, entity, version, plugin, verbose,
2015-06-21 17:35:14 +00:00
warnings=False):
self.timestamp = timestamp
self.isWrite = isWrite
2015-09-29 10:11:25 +00:00
self.entity = entity
self.version = version
self.plugin = plugin
2015-05-15 22:33:03 +00:00
self.verbose = verbose
2015-06-21 17:35:14 +00:00
self.warnings = warnings
2015-06-21 17:35:14 +00:00
def format(self, record, *args):
data = OrderedDict([
('now', self.formatTime(record, self.datefmt)),
])
data['version'] = self.version
data['plugin'] = self.plugin
data['time'] = self.timestamp
2015-05-15 22:33:03 +00:00
if self.verbose:
data['caller'] = record.pathname
data['lineno'] = record.lineno
data['isWrite'] = self.isWrite
2015-09-29 10:11:25 +00:00
data['file'] = self.entity
2015-05-15 22:33:03 +00:00
if not self.isWrite:
del data['isWrite']
data['level'] = record.levelname
2015-06-21 17:35:14 +00:00
data['message'] = record.getMessage() if self.warnings else record.msg
if not self.plugin:
del data['plugin']
return CustomEncoder().encode(data)
def formatException(self, exc_info):
2015-03-09 22:23:29 +00:00
return sys.exec_info[2].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):
logger = logging.getLogger('WakaTime')
2015-09-29 10:11:25 +00:00
for handler in logger.handlers:
logger.removeHandler(handler)
set_log_level(logger, args)
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(
timestamp=args.timestamp,
isWrite=args.isWrite,
2015-09-29 10:11:25 +00:00
entity=args.entity,
version=version,
plugin=args.plugin,
2015-05-15 22:33:03 +00:00
verbose=args.verbose,
)
handler.setFormatter(formatter)
logger.addHandler(handler)
2015-06-21 17:35:14 +00:00
warnings_formatter = JsonFormatter(datefmt='%Y/%m/%d %H:%M:%S %z')
warnings_formatter.setup(
timestamp=args.timestamp,
isWrite=args.isWrite,
2015-09-29 10:11:25 +00:00
entity=args.entity,
2015-06-21 17:35:14 +00: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 10:11:25 +00:00
except AttributeError: # pragma: nocover
2015-06-21 17:35:14 +00:00
pass # Python >= 2.7 is needed to capture warnings
return logger