irods  4.2.8
About: iRODS (the integrated Rule Oriented Data System) is a distributed data-management system for creating data grids, digital libraries, persistent archives, and real-time data systems.
  Fossies Dox: irods-4.2.8.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

log.py
Go to the documentation of this file.
1 import logging
2 import sys
3 import time
4 
5 class ColorFormatter(logging.Formatter):
6  BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
7 
8  RESET_SEQ = '\033[0m'
9  COLOR_SEQ_TEMPLATE = '\033[1;{fore_color_int}m'
10 
11  LEVELNO_TO_COLOR_INT_DICT = {
12  logging.WARNING: YELLOW,
13  logging.ERROR: RED,
14  }
15 
16  def format(self, record):
17  message = logging.Formatter.format(self, record)
18  color_seq = ''
19  if record.levelno in self.LEVELNO_TO_COLOR_INT_DICT:
20  fore_color_int = 30 + self.LEVELNO_TO_COLOR_INT_DICT[record.levelno]
21  color_seq = self.COLOR_SEQ_TEMPLATE.format(fore_color_int=fore_color_int)
22  return '{0}{1}{2}'.format(color_seq, message, self.RESET_SEQ)
23 
24 class NullHandler(logging.Handler):
25  def emit(self, record):
26  pass
27 
28 class LessThanFilter(logging.Filter, object):
29  def __init__(self, exclusive_maximum, name=""):
30  super(LessThanFilter, self).__init__(name)
31  self.max_level = exclusive_maximum
32 
33  def filter(self, record):
34  return 1 if record.levelno < self.max_level else 0
35 
36 class DeferInfoToDebugFilter(logging.Filter, object):
37  def __init__(self, name=""):
38  super(DeferInfoToDebugFilter, self).__init__(name)
39 
40  def filter(self, record):
41  if record.levelno == logging.INFO:
42  record.levelno = logging.DEBUG
43  record.levelname = 'DEBUG'
44  return 1
45 
46 def register_tty_handler(stream, minlevel, maxlevel):
47  logging_handler = logging.StreamHandler(stream)
48  logging_handler.setFormatter(ColorFormatter('%(message)s'))
49  if minlevel is not None:
50  logging_handler.setLevel(minlevel)
51  else:
52  logging_handler.setLevel(logging.NOTSET)
53  if maxlevel is not None:
54  logging_handler.addFilter(LessThanFilter(maxlevel))
55  logging.getLogger().addHandler(logging_handler)
56 
57 def register_file_handler(log_file_path, level=logging.DEBUG):
58  logging.Formatter.converter = time.gmtime
59 
60  logging_handler = logging.FileHandler(log_file_path)
61  logging_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)7s - %(filename)30s:%(lineno)4d - %(message)s'))
62  logging_handler.setLevel(logging.DEBUG)
63  logging.getLogger().addHandler(logging_handler)
irods.log.DeferInfoToDebugFilter.__init__
def __init__(self, name="")
Definition: log.py:37
irods.pyparsing.range
range
Definition: pyparsing.py:111
irods.log.ColorFormatter
Definition: log.py:5
irods.log.ColorFormatter.format
def format(self, record)
Definition: log.py:16
irods.log.ColorFormatter.COLOR_SEQ_TEMPLATE
string COLOR_SEQ_TEMPLATE
Definition: log.py:9
irods.log.LessThanFilter.__init__
def __init__(self, exclusive_maximum, name="")
Definition: log.py:29
irods.log.LessThanFilter.filter
def filter(self, record)
Definition: log.py:33
irods.log.LessThanFilter
Definition: log.py:28
irods.log.ColorFormatter.LEVELNO_TO_COLOR_INT_DICT
dictionary LEVELNO_TO_COLOR_INT_DICT
Definition: log.py:11
irods.log.DeferInfoToDebugFilter
Definition: log.py:36
irods.log.NullHandler
Definition: log.py:24
irods.log.ColorFormatter.RESET_SEQ
string RESET_SEQ
Definition: log.py:8
irods.log.register_tty_handler
def register_tty_handler(stream, minlevel, maxlevel)
Definition: log.py:46
irods.log.NullHandler.emit
def emit(self, record)
Definition: log.py:25
irods.log.register_file_handler
def register_file_handler(log_file_path, level=logging.DEBUG)
Definition: log.py:57
irods.log.DeferInfoToDebugFilter.filter
def filter(self, record)
Definition: log.py:40
irods.log.LessThanFilter.max_level
max_level
Definition: log.py:31