/usr/share/pyshared/fabulous/logs.py is in python-fabulous 0.1.5+dfsg1-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 | """
fabulous.logs
~~~~~~~~~~~~~
I provide utilities for making your logs look fabulous.
"""
import sys
import logging
from fabulous import utils
class TransientStreamHandler(logging.StreamHandler):
"""Standard Python logging Handler for Transient Console Logging
Logging transiently means that verbose logging messages like DEBUG
will only appear on the last line of your terminal for a short
period of time and important messages like WARNING will scroll
like normal text.
This allows you to log lots of messages without the important
stuff getting drowned out.
This module integrates with the standard Python logging module.
"""
def __init__(self, strm=sys.stderr, level=logging.WARNING):
logging.StreamHandler.__init__(self, strm)
if isinstance(level, int):
self.levelno = level
else:
self.levelno = logging._levelNames[level]
self.need_cr = False
self.last = ""
self.parent = logging.StreamHandler
def close(self):
if self.need_cr:
self.stream.write("\n")
self.need_cr = False
self.parent.close(self)
def write(self, data):
if self.need_cr:
width = max(min(utils.term.width, len(self.last)), len(data))
fmt = "\r%-" + str(width) + "s\n" + self.last
else:
fmt = "%s\n"
try:
self.stream.write(fmt % (data))
except UnicodeError:
self.stream.write(fmt % (data.encode("UTF-8")))
def transient_write(self, data):
if self.need_cr:
self.stream.write('\r')
else:
self.need_cr = True
width = utils.term.width
for line in data.rstrip().split('\n'):
if line:
if len(line) > width:
line = line[:width - 3] + '...'
line_width = max(min(width, len(self.last)), len(line))
fmt = "%-" + str(line_width) + "s"
self.last = line
try:
self.stream.write(fmt % (line))
except UnicodeError:
self.stream.write(fmt % (line.encode("UTF-8")))
else:
self.stream.write('\r')
self.stream.flush()
def emit(self, record):
try:
msg = self.format(record)
if record.levelno >= self.levelno:
self.write(msg)
else:
self.transient_write(msg)
except (KeyboardInterrupt, SystemExit):
raise
except:
self.handleError(record)
def basicConfig(level=logging.WARNING, transient_level=logging.NOTSET):
"""Shortcut for setting up transient logging
I am a replica of ``logging.basicConfig`` which installs a
transient logging handler to stderr.
"""
fmt = "%(asctime)s [%(levelname)s] [%(name)s:%(lineno)d] %(message)s"
logging.root.setLevel(transient_level) # <--- IMPORTANT
hand = TransientStreamHandler(level=level)
hand.setFormatter(logging.Formatter(fmt))
logging.root.addHandler(hand)
|