cwoebker cwoebker - 1 month ago 10x
Python Question

Python: logging module - globally

Hey I was wondering how to implement a global logger that could be used everywhere with your own settings:

I have

class customLogger(logging.Logger):

in a file with its formatters and other stuff.
The logger works perfectly on its own.

I import this module in my file and create an object like this:

self.log = log.customLogger(arguments)

But obviously i cannot access this object from other parts of my code.
Am i using a wrong approach? Is there a better way to do this?


Use logging.getLogger(name) to create a named global logger. ( )

import log
logger = log.setup_custom_logger('root')
logger.debug('main message')

import submodule

import logging

def setup_custom_logger(name):
    formatter = logging.Formatter(fmt='%(asctime)s - %(levelname)s - %(module)s - %(message)s')

    handler = logging.StreamHandler()

    logger = logging.getLogger(name)
    return logger

import logging

logger = logging.getLogger('root')
logger.debug('submodule message')


2011-10-01 20:08:40,049 - DEBUG - main - main message
2011-10-01 20:08:40,050 - DEBUG - submodule - submodule message