logging in multiple classes with module name in log
Question: I want to use the logging module instead of printing for debug information and
documentation. The goal is to print on the console with DEBUG level and log to
a file with INFO level.

I read through a lot of documentation, the cookbook and other tutorials on the
logging module but couldn't figure out, how I can use it the way I want it.
(I'm on python25)

I want to have the names of the modules in which the logs are written in my
logfile.

The documentation says I should use `logger = logging.getLogger(__name__)` but
how do I declare the loggers used in classes in other modules / packages, so
they use the same handlers like the main logger? To recognize the 'parent' I
can use `logger = logging.getLogger(parent.child)` but where do I know, who
has called the class/method?`

The example below shows my problem, if I run this, the output will only have
the `__main__` logs in and ignore the logs in `Class`

This is my **Mainfile:**

    
    
    # main.py
    
    import logging
    from module import Class
    
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)
    
    # create file handler which logs info messages
    fh = logging.FileHandler('foo.log', 'w', 'utf-8')
    fh.setLevel(logging.INFO)
    
    # create console handler with a debug log level
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    
    # creating a formatter
    formatter = logging.Formatter('- %(name)s - %(levelname)-8s: %(message)s')
    
    # setting handler format
    fh.setFormatter(formatter)
    ch.setFormatter(formatter)
    
    # add the handlers to the logger
    logger.addHandler(fh)
    logger.addHandler(ch)
    
    if __name__ == '__main__':
        logger.info('Script starts')
        logger.info('calling class Class')
        c = Class()
        logger.info('calling c.do_something()')
        c.do_something()
        logger.info('calling c.try_something()')
        c.try_something()
    

**Module:**

    
    
    # module.py
    
    imnport logging
    
    class Class:
        def __init__(self):
            self.logger = logging.getLogger(__name__) # What do I have to enter here?
            self.logger.info('creating an instance of Class')
            self.dict = {'a':'A'}
        def do_something(self):
            self.logger.debug('doing something')
            a = 1 + 1
            self.logger.debug('done doing something')
        def try_something(self):
            try:
                logging.debug(self.dict['b'])
            except KeyError, e:
                logging.exception(e)
    

Output in **console:**

    
    
    - __main__ - INFO    : Script starts
    - __main__ - INFO    : calling class Class
    - __main__ - INFO    : calling c.do_something()
    - __main__ - INFO    : calling c.try_something()
    No handlers could be found for logger "module"
    

Besides: Is there a way to get the module names were the logs ocurred in my
logfile, without declaring a new logger in each class like above? Also like
this way I have to go for `self.logger.info()` each time I want to log
something. I would prefer to use `logging.info()` or `logger.info()` in my
whole code.

Is a global logger perhaps the right answer for this? But then I won't get the
modules where the errors occur in the logs...

And my last question: Is this pythonic? Or is there a better recommendation to
do such things right.


Answer: You should have one global logger with your handlers:

    
    
    logger= logging.getLogger("myapp")
    #configure handlers
    

Then, on each module:

`logger= logging.getLogger("myapp."+__name__)`

Whether you need per-class loggers is up to you - most projects I've seen have
(at most) one logger per module. My advice is, if you don't need different
handlers for different classes, stick with one logger per module, or even one
logger per project - if the project is small.

If you need more context on your logs, note you can print the current function
name using `%(funcName)s` on the formatter

