/usr/lib/python2.7/dist-packages/datalad/log.py is in python-datalad 0.9.3-1.
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | # emacs: -*- mode: python; py-indent-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-
# ex: set sts=4 ts=4 sw=4 noet:
# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
#
# See COPYING file distributed along with the datalad package for the
# copyright and license terms.
#
# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
import logging
import os
import sys
import platform
import logging.handlers
from os.path import basename, dirname
from .utils import is_interactive
from .support import ansi_colors as colors
__all__ = ['ColorFormatter']
# Snippets from traceback borrowed from duecredit which was borrowed from
# PyMVPA upstream/2.4.0-39-g69ad545 MIT license (the same copyright as DataLad)
def mbasename(s):
"""Custom function to include directory name if filename is too common
Also strip .py at the end
"""
base = basename(s)
if base.endswith('.py'):
base = base[:-3]
if base in set(['base', '__init__']):
base = basename(dirname(s)) + '.' + base
return base
class TraceBack(object):
"""Customized traceback to be included in debug messages
"""
def __init__(self, limit=100, collide=False):
"""Initialize TraceBack metric
Parameters
----------
collide : bool
if True then prefix common with previous invocation gets
replaced with ...
"""
self.__prev = ""
self.limit = limit
self.collide = collide
# delayed imports and preparing the regex substitution
if collide:
import re
self.__prefix_re = re.compile('>[^>]*$')
else:
self.__prefix_re = None
import traceback
self._extract_stack = traceback.extract_stack
def __call__(self):
ftb = self._extract_stack(limit=200)[:-2]
entries = [[mbasename(x[0]), str(x[1])]
for x in ftb if mbasename(x[0]) != 'logging.__init__']
entries = [e for e in entries if e[0] != 'unittest']
if len(entries) > self.limit:
sftb = '...>'
entries = entries[-self.limit:]
else:
sftb = ''
if not entries:
return ""
# lets make it more consize
entries_out = [entries[0]]
for entry in entries[1:]:
if entry[0] == entries_out[-1][0]:
entries_out[-1][1] += ',%s' % entry[1]
else:
entries_out.append(entry)
sftb += '>'.join(
['%s:%s' % (mbasename(x[0]), x[1]) for x in entries_out]
)
if self.collide:
# lets remove part which is common with previous invocation
prev_next = sftb
common_prefix = os.path.commonprefix((self.__prev, sftb))
common_prefix2 = self.__prefix_re.sub('', common_prefix)
if common_prefix2 != "":
sftb = '...' + sftb[len(common_prefix2):]
self.__prev = prev_next
return sftb
# Recipe from http://stackoverflow.com/questions/384076/how-can-i-color-python-logging-output
# by Brandon Thomson
# Adjusted for automagic determination either coloring is needed and
# prefixing of multiline log lines
class ColorFormatter(logging.Formatter):
def __init__(self, use_color=None, log_name=False, log_pid=False):
if use_color is None:
# if 'auto' - use color only if all streams are tty
use_color = is_interactive()
self.use_color = use_color and platform.system() != 'Windows' # don't use color on windows
msg = colors.format_msg(self._get_format(log_name, log_pid),
self.use_color)
log_env = os.environ.get('DATALAD_LOG_TRACEBACK', '')
collide = log_env == 'collide'
limit = 100 if collide else int(log_env) if log_env.isdigit() else 100
self._tb = TraceBack(collide=collide, limit=limit) if log_env else None
logging.Formatter.__init__(self, msg)
def _get_format(self, log_name=False, log_pid=False):
from datalad import cfg
from datalad.config import anything2bool
show_timestamps = anything2bool(cfg.get('datalad.log.timestamp', False))
return (("" if not show_timestamps else "$BOLD%(asctime)-15s$RESET ") +
("%(name)-15s " if log_name else "") +
("{%(process)d}" if log_pid else "") +
"[%(levelname)s] "
"%(message)s ")
def format(self, record):
if record.msg.startswith('| '):
# If we already log smth which supposed to go without formatting, like
# output for running a command, just return the message and be done
return record.msg
levelname = record.levelname
if self.use_color and levelname in colors.LOG_LEVEL_COLORS:
fore_color = colors.LOG_LEVEL_COLORS[levelname]
levelname_color = (colors.COLOR_SEQ % fore_color) + \
("%-7s" % levelname) + colors.RESET_SEQ
record.levelname = levelname_color
record.msg = record.msg.replace("\n", "\n| ")
if self._tb:
record.msg = self._tb() + " " + record.msg
return logging.Formatter.format(self, record)
class LoggerHelper(object):
"""Helper to establish and control a Logger"""
def __init__(self, name='datalad', logtarget=None):
"""
Parameters
----------
name :
logtarget : string, optional
If we want to use our logger for other log targets, while having
a uniform control over them
"""
self.name = name
self.logtarget = logtarget
self.lgr = logging.getLogger(logtarget if logtarget is not None else name)
def _get_config(self, var, default=None):
from datalad import cfg
return cfg.get(self.name.lower() + '.log.' + var, default)
def set_level(self, level=None, default='INFO'):
"""Helper to set loglevel for an arbitrary logger
By default operates for 'datalad'.
TODO: deduce name from upper module name so it could be reused without changes
"""
if level is None:
# see if nothing in the environment
level = self._get_config('level')
if level is None:
level = default
try:
# it might be a string which still represents an int
log_level = int(level)
except ValueError:
# or a string which corresponds to a constant;)
log_level = getattr(logging, level.upper())
self.lgr.setLevel(log_level)
# and set other related/used loggers to the same level to prevent their
# talkativity, if they are not yet known to this python session, so we
# have little chance to "override" possibly set outside levels
for dep in ('git',):
if dep not in logging.Logger.manager.loggerDict:
logging.getLogger(dep).setLevel(log_level)
def get_initialized_logger(self, logtarget=None):
"""Initialize and return the logger
Parameters
----------
target: string, optional
Which log target to request logger for
logtarget: { 'stdout', 'stderr', str }, optional
Where to direct the logs. stdout and stderr stand for standard streams.
Any other string is considered a filename. Multiple entries could be
specified comma-separated
Returns
-------
logging.Logger
"""
# By default mimic previously talkative behavior
logtarget = self._get_config('target', logtarget or 'stderr')
# Allow for multiple handlers being specified, comma-separated
if ',' in logtarget:
for handler_ in logtarget.split(','):
self.get_initialized_logger(logtarget=handler_)
return self.lgr
if logtarget.lower() in ('stdout', 'stderr'):
loghandler = logging.StreamHandler(getattr(sys, logtarget.lower()))
use_color = is_interactive() # explicitly decide here
else:
# must be a simple filename
# Use RotatingFileHandler for possible future parametrization to keep
# log succinct and rotating
loghandler = logging.handlers.RotatingFileHandler(logtarget)
use_color = False
# I had decided not to guard this call and just raise exception to go
# out happen that specified file location is not writable etc.
for names_filter in 'names', 'namesre':
names = self._get_config(names_filter, '')
if names:
import re
# add a filter which would catch those
class LogFilter(object):
"""A log filter to filter based on the log target name(s)"""
def __init__(self, names):
self.target_names = set(n for n in names.split(',')) \
if names_filter == 'names' \
else re.compile(names)
if names_filter == 'names':
def filter(self, record):
return record.name in self.target_names
else:
def filter(self, record):
return self.target_names.match(record.name)
loghandler.addFilter(LogFilter(names))
# But now improve with colors and useful information such as time
loghandler.setFormatter(
ColorFormatter(use_color=use_color,
# TODO: config log.name, pid
log_name=self._get_config("name", False),
log_pid=self._get_config("pid", False),
))
# logging.Formatter('%(asctime)-15s %(levelname)-6s %(message)s'))
self.lgr.addHandler(loghandler)
self.set_level() # set default logging level
return self.lgr
lgr = LoggerHelper().get_initialized_logger()
|