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
275
276
277
278
279
280
281
282
|
# Software Management Providers
#
# Copyright (C) 2012-2013 Red Hat, Inc. All rights reserved.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# Authors: Michal Minar <miminar@redhat.com>
#
"""
Module devoted to logging facilities.
"""
import inspect
from itertools import chain
import logging
import logging.config
import os
from lmi.providers import cmpi_logging
DEFAULT_LOGGING_CONFIG = {
"version" : 1,
'disable_existing_loggers' : True,
"formatters": {
# this is a message format for logging function/method calls
# it's manually set up in YumWorker's init method
"default": {
"()": "lmi.software.util.cmpi_logging.DispatchingFormatter",
"formatters" : {
"lmi.software.util.cmpi_logging.trace_function_or_method":
"%(levelname)s:%(message)s"
},
"default" : "%(levelname)s:%(module)s:"
"%(funcName)s:%(lineno)d - %(message)s"
},
},
"handlers": {
"stderr" : {
"class" : "logging.StreamHandler",
"level" : "ERROR",
"formatter": "default",
},
"cmpi" : {
"()" : "lmi.providers.cmpi_logging.CMPILogHandler",
# this must be filled with broker env logger
"cmpi_logger" : None,
"level" : "ERROR",
"formatter" : "default"
},
},
"root": {
"level": "ERROR",
"handlers" : ["cmpi"],
},
}
LOGGING_LEVELS = {
"critical" : logging.CRITICAL,
"error" : logging.ERROR,
"warning" : logging.WARNING,
"warn" : logging.WARNING,
"info" : logging.INFO,
"trace_warning" : cmpi_logging.TRACE_WARNING,
"trace_info" : cmpi_logging.TRACE_INFO,
"trace_verbose" : cmpi_logging.TRACE_VERBOSE,
"debug" : logging.DEBUG
}
class DispatchingFormatter:
"""
Formatter class for logging module. It allows to predefine different
format string for paricular module names.
There is no way, how to setup this formatter in configuration file.
"""
def __init__(self, formatters, default):
"""
*format* in parameters description can be either ``string`` or
another formatter object.
:param formatters (``dict``) Mapping of module names to *format*.
:param default Default *format*.
"""
for k, formatter in formatters.items():
if isinstance(formatter, basestring):
formatters[k] = logging.Formatter(formatter)
self._formatters = formatters
if isinstance(default, basestring):
default = logging.Formatter(default)
self._default_formatter = default
def format(self, record):
"""
Interface for logging module.
"""
formatter = self._formatters.get(record.name, self._default_formatter)
return formatter.format(record)
# *****************************************************************************
# Decorators
# *****************************************************************************
def trace_function_or_method(is_method=False, frame_level=1):
"""
Factory for function and method decorators. Generated decorators
log every calls and exits of decorated functions or methods.
Logged information contain the caller's module and line together with
called function's module, function name and line number.
:param is_method: (``bool``) Whether returned decorator is targeted
for use upon a method of a class. It modified logged function by
prepending owning class name.
:param frame_level: (``int``) Number of nested frames to skip when
searching for called function scope by inspecting stack upwards.
When the result of this function is applied directly on the definition
of function, it's value should be 1. When used from inside of some
other factory, it must be increased by 1.
"""
assert frame_level >= 1
def _decorator(func):
"""
Decorator for logging entries and exits of function or method.
"""
if not inspect.ismethod(func) and not inspect.isfunction(func):
raise TypeError("func must be a function")
def _print_value(val):
"""
Used here for printing function arguments. Shortens the output
string, if that would be too long.
"""
if isinstance(val, list):
if len(val) < 2:
return str(val)
else:
return "[%s, ...]" % _print_value(val[0])
return str(val)
logger = logging.getLogger(__name__+'.trace_function_or_method')
module = func.__module__.split('.')[-1]
frm = inspect.currentframe()
for i in range(frame_level):
frm = frm.f_back
lineno = frm.f_lineno
del frm
classname = inspect.getouterframes(
inspect.currentframe())[frame_level][3]
def _wrapper(*args, **kwargs):
"""
Wrapper for function or method, that does the logging.
"""
if logger.isEnabledFor(logging.DEBUG):
frm = inspect.currentframe()
logargs = {
"caller_file" : os.path.basename(os.path.splitext(
frm.f_back.f_code.co_filename)[0]),
"caller_lineno" : frm.f_back.f_lineno,
"module" : module,
"func" : classname + "." + func.__name__
if is_method else func.__name__,
"lineno" : lineno,
"action" : "entering",
"args" : ", ".join(chain(
(_print_value(a) for a in args),
( "%s=%s"%(k, _print_value(v))
for k, v in kwargs.items())))
}
if not logargs["args"]:
logargs["args"] = ""
else:
logargs["args"] = " with args=(%s)" % logargs["args"]
logger.debug("%(caller_file)s:%(caller_lineno)d - %(action)s"
" %(module)s:%(func)s:%(lineno)d%(args)s" , logargs)
try:
result = func(*args, **kwargs)
if logger.isEnabledFor(logging.DEBUG):
logargs["action"] = "exiting"
logger.debug("%(caller_file)s:%(caller_lineno)d"
" - %(action)s %(module)s:%(func)s:%(lineno)d",
logargs)
except Exception as exc:
if logger.isEnabledFor(logging.DEBUG):
logargs['action'] = 'exiting'
logargs['error'] = str(exc)
logger.debug("%(caller_file)s:%(caller_lineno)d"
" - %(action)s %(module)s:%(func)s:%(lineno)d"
" with error: %(error)s", logargs)
raise
return result
return _wrapper
return _decorator
def trace_function(func, frame_level=1):
""" Convenience function used for decorating simple functions. """
return trace_function_or_method(frame_level=frame_level + 1)(func)
def trace_method(func, frame_level=1):
""" Convenience function used for decorating methods. """
return trace_function_or_method(True, frame_level + 1)(func)
def setup(env, config):
"""
Set up the logging with options given by SoftwareConfiguration instance.
This should be called at process's startup before any message is sent to
log.
:param env: (``ProviderEnvironment``) Provider environment, taken
from CIMOM callback (e.g. ``get_providers()``).
:param config: (``BaseConfiguration``) Configuration with Log section
containing settings for logging.
"""
cp = config.config
logging_setup = False
try:
path = config.file_path('Log', 'FileConfig')
if not os.path.exists(path):
logging.getLogger(__name__).error('given FileConfig "%s" does'
' not exist', path)
else:
logging.config.fileConfig(path)
logging_setup = True
except Exception:
if cp.has_option('Log', 'FileConfig'):
logging.getLogger(__name__).exception(
'failed to setup logging from FileConfig')
if logging_setup is False:
defaults = DEFAULT_LOGGING_CONFIG.copy()
defaults["handlers"]["cmpi"]["cmpi_logger"] = env.get_logger()
if config.stderr:
defaults["root"]["handlers"] = ["stderr"]
level = config.logging_level
if not level in LOGGING_LEVELS:
logging.getLogger(__name__).error(
'level name "%s" not supported', level)
else:
level = LOGGING_LEVELS[level]
for handler in defaults["handlers"].values():
handler["level"] = level
defaults["root"]["level"] = level
logging.config.dictConfig(defaults)
# needed for shared code under lmi.common
cmpi_logging.logger = logging.getLogger('lmi.providers')
def get_logger(module_name):
"""
Convenience function for getting callable returning logger for particular
module name. It's supposed to be used at module's level to assign its
result to global variable like this:
LOG = cmpi_logging.get_logger(__name__)
This can be used in module's functions and classes like this:
def module_function(param):
LOG().debug("this is debug statement logging param: %s", param)
Thanks to ``LOG`` being a callable, it always returns valid logger object
with current configuration, which may change overtime.
"""
def _logger():
""" Callable used to obtain current logger object. """
return logging.getLogger(module_name)
return _logger
|