0

I noticed that when I call another function it can still access the logger even though it wasn’t passed. For example, the following file always writes to testlogger.log:

import logging
from datetime import datetime

def printHello():
    print('Hello')
    logger.info('printed hello')
    printGoodbye()

def printGoodbye():
    print('Goodbye')
    logger.info('printed goodbye')

if __name__ == "__main__":
    logger = logging.getLogger(__name__)
    FORMAT = '%(name)s - %(levelname)s - %(message)s'
    logging.basicConfig(filename='testlogger.log', level=logging.DEBUG, format=FORMAT)
    logger.info(datetime.now().strftime('%H:%M %d-%m-%Y.log'))
    logger.info('About to call function printHello')
    printHello()
    logger.info('Now exiting')

Is this how it should be done? Also, the project I’m working on is divided into several modules, which, when completed, will call each other. Should there be a separate log file for each module? If so, is the convention just to name the log file after the script that created it?

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
Celeritas
  • 14,489
  • 36
  • 113
  • 194
  • It seems like you're looking for someone to review your code. You may have better success on http://codereview.stackexchange.com/. – Matt Habel Jul 16 '15 at 17:42

1 Answers1

1

The best way is to follow this advice from the Python logging documentation:

The logger name hierarchy is analogous to the Python package hierarchy, and identical to it if you organise your loggers on a per-module basis using the recommended construction logging.getLogger(__name__). That’s because in a module, __name__ is the module’s name in the Python package namespace.

Doing that will give you relatively fine-grained control over logging. By default (using logging.basicConfig), logging for each module will work the same way: It will be at the same level and go to the same place, but if you want you can now turn logging on or off for specific modules, or turn up logging to a high level for one module. See Python logging to multiple handlers, at different log levels? for an example of how to do that.

The reason your logger variable is accessible across functions is because when you assign to it at the top-level, a "global" variable is created (it's not really global, it has module scope). A better way is to explicitly create the logger yourself at the top of the script:

import logging
from datetime import datetime

logger = logging.getLogger(__name__)


def printHello():
    print('Hello')
    logger.info('printed hello')
    printGoodbye()

def printGoodbye():
    print('Goodbye')
    logger.info('printed goodbye')

if __name__ == "__main__":
    FORMAT = '%(name)s - %(levelname)s - %(message)s'
    logging.basicConfig(filename='testlogger.log', level=logging.DEBUG, format=FORMAT)
    logger.info(datetime.now().strftime('%H:%M %d-%m-%Y.log'))
    logger.info('About to call function printHello')
    printHello()
    logger.info('Now exiting')
Community
  • 1
  • 1
John Wiseman
  • 3,081
  • 1
  • 22
  • 31