search by tags

for the user

adventures into the land of the command line

using python logging in multiple modules

project be like…

myapp/
myapp/__main__.py
myapp/something.py

In __main__.py

import logging

# logging
logger = logging.getLogger("myapp")
logger.setLevel(logging.INFO)
handler = logging.FileHandler(logfile)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)


logger.info('hi there from myapp')

In something.py

import logging

logger = logging.getLogger("myapp.something")

class Else:
    def __init__(self):
        self.logger = logging.getLogger("myapp.something.Else")
        .
        .

    def booya(self):
        .
        .
        self.logger.info('hi there from something')

Output will be like so:

OUTPUT:

2017-05-12 12:06:35,865 - myapp - INFO - 'hi there from myapp'
2017-05-12 12:06:35,865 - myapp.something.Else - INFO - 'hi there from something'

FROM TWO FILES!