ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: How to disablе debug logging in release version
Submitter: Denis Barmenkov (other recipes)
Last Updated: 2006/07/12
Version no: 1.2
Category: Debugging

 

Not Rated yet


Description:

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.

Source: Text Source

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
"""

Discussion:



Add comment

Number of comments: 2

setting the logging levelI, Jürgen Hermann, 2008/06/18
What does your recipe do better than this simple call?

logging.getLogger().setLevel(logging.INFO)

Add comment

setting the logging levelI, Jürgen Hermann, 2008/06/18
What does your recipe do better than this simple call?

logging.getLogger().setLevel(logging.INFO)

Add comment



Highest rated recipes:

1. A simple XML-RPC server

2. Web service accessible ...

3. IPy Notify

4. Treat the Win32 Registry ...

5. a friendly mkdir()

6. Wrapping template engine ...

7. Assignment in expression

8. Changing return value ...

9. Implementation of sets ...

10. bag collection class




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.