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
"""
|
Tags: debugging
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?
If I use some third-party modules which can call setLevel() for their logging channels, debug messages still can occurs in application log.
With my recipe loggers equipped with empty debug() methods and I dont worry about these libraries ;)
I find the advice of this recipe quite strange.
When shipping release code to the customer, the log is the only way the customer can tell me how something got wrong in the application. So I certainly don't want it off. I will usually set it to INFO and have a possibility for the customer to set it to DEBUG easily (config file, argv option, ...)
As author sometimes I dont like to show off program details to user (not a customer). Along with opensource rush to open code/logs/etc there may be third-party license restrictions, so I dont want to reveal program/data structure to end-user.