Loggy
Loggy

Reputation: 11

How can I share a logger across a large application?

I have an application that consists of many distinct modules, yet everything is still part of a single application. How can I properly share a logger, so that everything writes to the same file. Do I need to pass a logger around? I'd prefer not to have to do this.

Example project layout:

/
    __init__.py
    main_application.py
    functions_group1.py
    functions_group2.py
    functions_group3.py

I want to be able to define a logger in main_application.py like so:

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
file_log = logging.FileHandler('logs/%s.log' % (file_name), 'a', encoding='UTF-8')
file_log.setLevel(file_level)
formatter = logging.Formatter('%(asctime)s - %(levelname)-8s - %(name)-12s - %(message)s')
file_log.setFormatter(formatter)
logger.addHandler(file_log)

Then be able to use logger in functions_group1, functions_group1, functions_group3 which are imported like this in main_application:

import functions_group1
import functions_group2
import functions_group3

Each of these files has only a list of functions (grouped by similar functionality)

functions_group1

def function1_dothing():
    # Want to log in here!
    return ...

def function1_dothing2():
    # Want to log in here!
    return ...

def function1_dothing3():
    # Want to log in here!
    return ...

How can I share the logger across the entire application?

Upvotes: 1

Views: 1039

Answers (1)

theorifice
theorifice

Reputation: 690

I think the point that you are missing is that by default, Python loggers are hierarchical. In your main application you simply create a logger with a fixed name (you can use the name of the main script). For example:

mainapp.py:

import logging

root_logger = logging.getLogger(appname())
# do any logger setup 

where appname() is defined as:

def appname():
    return os.path.splitext(os.path.basename(sys.argv[0]))[0]

In any of your modules you can either get the root logger or get a child of the root logger.

moduleX.py:

import logging
module_logger = logging.getLogger("%s.moduleX" % (appname()))

Any logging that module_logger performs will be handled by the root logger. There's much more you can accomplish with the logging module. Maybe another read of https://docs.python.org/2/howto/logging.html with a different perspective will be valuable.

Upvotes: 5

Related Questions