When you release your program to client, its a good idea to disable all the debug messages.
It is possible via custom configuring debug levels at all modules, but may be implemented using simple wrapper around logging.getLogger() function.
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 | import logging
__orig_getLogger=None
def release_getLogger(name):
'''
passing original handler with debug() method replaced to empty function
'''
def dummy(*k, **kw):
pass
global __orig_getLogger
log = __orig_getLogger(name)
setattr(log, 'debug', dummy)
return log
def install_release_loggers():
'''
save original handler, install newer
'''
global __orig_getLogger
__orig_getLogger = logging.getLogger
setattr(logging, 'getLogger', release_getLogger)
def restore_getLogger():
'''
restore original handler
'''
global __orig_getLogger
if __orig_getLogger:
setattr(logging, 'getLogger', __orig_getLogger)
# sample code
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(name)s %(levelname)s> %(message)s',
filename='./test.log',
filemode='w')
# start main program, install wrapper
install_release_loggers()
log = logging.getLogger('main')
log.info('=== start ===')
log.debug('hidden message ;)')
log.info('info')
log.error('mandatory error')
"""
Log will contain (without debug message):
main INFO> === start ===
main INFO> info
main ERROR> mandatory error
"""
|
Comments
setting the logging levelI. What does your recipe do better than this simple call?
setting the logging levelI. What does your recipe do better than this simple call?
Sign in to comment