LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
log.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 #
4 # LSST Data Management System
5 # Copyright 2013 LSST Corporation.
6 #
7 # This product includes software developed by the
8 # LSST Project (http://www.lsst.org/).
9 #
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the LSST License Statement and
21 # the GNU General Public License along with this program. If not,
22 # see <http://www.lsstcorp.org/LegalNotices/>.
23 #
24 
25 from lsst.log import (configure_iface, configure_prop_iface,
26  getDefaultLoggerName_iface, pushContext_iface,
27  popContext_iface, MDC_iface, MDCRemove_iface,
28  getLevel_iface, setLevel_iface,
29  isEnabledFor_iface, forcedLog_iface)
30 import logging
31 import inspect
32 from os import path
33 
34 # logging levels (these conform to standard log4cxx levels)
35 TRACE = 5000
36 DEBUG = 10000
37 INFO = 20000
38 WARN = 30000
39 ERROR = 40000
40 FATAL = 50000
41 
42 def configure(*args):
43  if len(args) > 0:
44  configure_iface(args[0])
45  else:
47 
48 def configure_prop(properties):
49  configure_prop_iface(properties)
50 
53  return name
54 
55 def pushContext(name):
56  pushContext_iface(name)
57 
58 def popContext():
60 
61 def MDC(key, value):
62  MDC_iface(str(key), str(value))
63 
64 def MDCRemove(key):
65  MDCRemove_iface(str(key))
66 
67 def setLevel(loggername, level):
68  setLevel_iface(loggername, level)
69 
70 def getLevel(loggername):
71  return getLevel_iface(loggername)
72 
73 def isEnabledFor(loggername, level):
74  return isEnabledFor_iface(loggername, level)
75 
76 def _getFrame(depth):
77  frame = inspect.currentframe().f_back
78  for i in range(depth):
79  frame = frame.f_back
80  return frame
81 
82 def _getFuncName(depth):
83  return inspect.stack()[depth+1][3]
84 
85 def log(loggername, level, fmt, *args, **kwargs):
86  if 'depth' in kwargs:
87  depth = kwargs['depth']
88  else:
89  depth = 1
90  if isEnabledFor(loggername, level):
91  frame = _getFrame(depth)
92  forcedLog_iface(loggername, level,
93  path.split(frame.f_code.co_filename)[1],
94  _getFuncName(depth), frame.f_lineno, fmt % args)
95 
96 def trace(fmt, *args):
97  log("", TRACE, fmt, *args, depth=2)
98 
99 def debug(fmt, *args):
100  log("", DEBUG, fmt, *args, depth=2)
101 
102 def info(fmt, *args):
103  log("", INFO, fmt, *args, depth=2)
104 
105 def warn(fmt, *args):
106  log("", WARN, fmt, *args, depth=2)
107 
108 def error(fmt, *args):
109  log("", ERROR, fmt, *args, depth=2)
110 
111 def fatal(fmt, *args):
112  log("", FATAL, fmt, *args, depth=2)
113 
115 
116  def __init__(self, name=None, level=None):
117  self.name = name
118  self.level = level
119 
120  def __enter__(self):
121  self.open()
122  return self
123 
124  def __exit__(self ,type, value, traceback):
125  self.close()
126 
127  def __del__(self):
128  self.close()
129 
130  def open(self):
131  if self.name is not None:
132  pushContext(self.name)
133  if self.level is not None:
134  setLevel("", self.level)
135 
136  def close(self):
137  if self.name is not None:
138  popContext()
139  self.name = None
140 
141  def setLevel(self, level):
142  setLevel("", level)
143 
144  def getLevel(self):
145  return getLevel("")
146 
147  def isEnabledFor(self, level):
148  return isEnabledFor("", level)
149 
150 class LogHandler(logging.Handler):
151  def __init__(self, name=None, level=None):
152  self.context = LogContext(name=name, level=level)
153  self.context.open()
154  logging.Handler.__init__(self)
155 
156  def __del__(self):
157  self.close()
158 
159  def close(self):
160  if self.context is not None:
161  self.context.close()
162  self.context = None
163  logging.Handler.close(self)
164 
165  def handle(self, record):
166  if self.context.isEnabledFor(self.translateLevel(record.levelno)):
167  logging.Handler.handle(self, record)
168 
169  def emit(self, record):
170  forcedLog_iface("", self.translateLevel(record.levelno), record.filename,
171  record.funcName, record.lineno, record.msg % record.args)
172 
173  def translateLevel(self, levelno):
174  """
175  Translates from standard python logging module levels
176  to standard log4cxx levels.
177  """
178  return levelno*1000
def popContext
Definition: log.py:58
def isEnabledFor
Definition: log.py:73
void forcedLog_iface(std::string const &loggername, int level, std::string const &filename, std::string const &funcname, int lineno, std::string const &mdg)
Definition: logInterface.cc:87
def getLevel
Definition: log.py:70
def configure
Definition: log.py:42
void configure_iface(void)
Definition: logInterface.cc:43
def info
Definition: log.py:102
def _getFrame
Definition: log.py:76
def trace
Definition: log.py:96
void setLevel_iface(std::string const &loggername, int level)
Definition: logInterface.cc:75
def fatal
Definition: log.py:111
def error
Definition: log.py:108
int getLevel_iface(std::string const &loggername)
Definition: logInterface.cc:79
def log
Definition: log.py:85
def _getFuncName
Definition: log.py:82
void configure_prop_iface(std::string const &properties)
Definition: logInterface.cc:51
def warn
Definition: log.py:105
bool isEnabledFor_iface(std::string const &loggername, int level)
Definition: logInterface.cc:83
def setLevel
Definition: log.py:67
def MDC
Definition: log.py:61
void popContext_iface()
Definition: logInterface.cc:63
void MDCRemove_iface(std::string const &key)
Definition: logInterface.cc:71
std::string getDefaultLoggerName_iface(void)
Definition: logInterface.cc:55
void pushContext_iface(std::string const &name)
Definition: logInterface.cc:59
def MDCRemove
Definition: log.py:64
def configure_prop
Definition: log.py:48
void MDC_iface(std::string const &key, std::string const &value)
Definition: logInterface.cc:67
def pushContext
Definition: log.py:55
def debug
Definition: log.py:99
def getDefaultLoggerName
Definition: log.py:51