/usr/lib/python2.7/dist-packages/kopano/log.py is in python-kopano 8.5.5-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | """
Part of the high-level python bindings for Kopano
Copyright 2005 - 2016 Zarafa and its licensors (see LICENSE file)
Copyright 2016 - Kopano and its licensors (see LICENSE file)
"""
import contextlib
import logging.handlers
try:
from Queue import Empty
except ImportError:
from queue import Empty
import sys
import threading
import traceback
def _loglevel(options, config):
if options and getattr(options, 'loglevel', None):
log_level = options.loglevel
elif config:
log_level = config.get('log_level')
else:
log_level = 'warning'
return { # XXX NONE?
'0': logging.NOTSET,
'1': logging.CRITICAL,
'2': logging.ERROR,
'3': logging.WARNING,
'4': logging.INFO,
'5': logging.INFO,
'6': logging.DEBUG,
'DEBUG': logging.DEBUG,
'INFO': logging.INFO,
'WARNING': logging.WARNING,
'ERROR': logging.ERROR,
'CRITICAL': logging.CRITICAL,
}[log_level.upper()]
def logger(service, options=None, stdout=False, config=None, name=''):
logger = logging.getLogger(name or service)
if logger.handlers:
return logger
fmt = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
formatter = logging.Formatter(fmt)
log_method = 'file'
log_file = '/var/log/kopano/%s.log' % service
if config:
log_method = config.get('log_method') or log_method
log_file = config.get('log_file') or log_file
log_level = _loglevel(options, config)
if name:
log_file = log_file.replace(service, name) # XXX
fh = None
if log_method == 'file':
if log_file == '-':
fh = logging.StreamHandler(sys.stderr)
else:
fh = logging.handlers.WatchedFileHandler(log_file)
elif log_method == 'syslog':
fh = logging.handlers.SysLogHandler(address='/dev/log')
if fh:
fh.setLevel(log_level)
fh.setFormatter(formatter)
logger.addHandler(fh)
if not getattr(options, 'service', True):
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(log_level)
ch.setFormatter(formatter)
if stdout or (options and options.foreground):
logger.addHandler(ch)
logger.setLevel(log_level)
return logger
# it logs errors, that's all you need to know :-)
@contextlib.contextmanager
def log_exc(log, stats=None):
"""
Context-manager to log any exception in sub-block to given logger instance
:param log: logger instance
Example usage::
with log_exc(log):
.. # any exception will be logged when exiting sub-block
"""
try: yield
except Exception:
log.error(traceback.format_exc())
if stats:
stats['errors'] += 1
# log-to-queue handler copied from Vinay Sajip
class QueueHandler(logging.Handler):
def __init__(self, queue):
logging.Handler.__init__(self)
self.queue = queue
def enqueue(self, record):
self.queue.put_nowait(record)
def prepare(self, record):
self.format(record)
record.msg, record.args, record.exc_info = record.message, None, None
return record
def emit(self, record):
try:
self.enqueue(self.prepare(record))
except (KeyboardInterrupt, SystemExit):
raise
except:
self.handleError(record)
# log-to-queue listener copied from Vinay Sajip
class QueueListener(object):
def __init__(self, queue, *handlers):
self.queue = queue
self.handlers = handlers
self._stop = threading.Event()
self._thread = None
def start(self):
self._thread = t = threading.Thread(target=self._monitor)
t.setDaemon(True)
t.start()
def handle(self, record):
for handler in self.handlers:
handler.handle(record)
def _monitor(self):
q = self.queue
has_task_done = hasattr(q, 'task_done')
while True:
try:
record = self.queue.get(True, 1) # block, timeout 1 sec
self.handle(record)
if has_task_done:
q.task_done()
except (Empty, EOFError):
if self._stop.isSet():
break
def stop(self):
self._stop.set()
self._thread.join()
self._thread = None
|