1

I have put the following in my config.py:

import time
import logging
#logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', level=logging.INFO)
logFormatter = logging.Formatter('%(asctime)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
rootLogger = logging.getLogger()
rootLogger.setLevel(logging.INFO)

fileHandler = logging.FileHandler("{0}.log".format(time.strftime('%Y%m%d%H%M%S')))
fileHandler.setFormatter(logFormatter)
rootLogger.addHandler(fileHandler)

consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(logFormatter)
rootLogger.addHandler(consoleHandler)

and then I am doing

from config import *

in all of my scripts and imported files.

Unfortunately, this causes multiple log files created.

How to fix this? I wan't centralized config.py with logging configured both to console and file.

6
  • You can manually log them to a txt file using with open('log.txt','a') as f: f.write(logging part) and do this for all the scripts with the same log.txt file. And for console, you can use print statements. Commented Jul 10, 2017 at 14:17
  • I would like to print timestamp, with print it will require repetitive coding. Commented Jul 10, 2017 at 14:20
  • @Dims make it a function? Commented Jul 10, 2017 at 14:22
  • you can use datetime.datetime.now() as one of the parameters in the print. So, since you have to print the log anyway, you can print it with the timestamp. Commented Jul 10, 2017 at 14:24
  • How does the overall application look like? Is there kind of a "main" file that runs all modules? Or put differently: is this one application consisting of different parts (scripts), or are these (besides logging) independent scripts that run independently? Commented Jul 10, 2017 at 14:29

1 Answer 1

3

Case 1: Independent Scripts / Programs

In case we are talking about multiple, independent scripts, that should have logging set up in the same way: I would say, each independent application should have its own log. If you definitively do not want this, you would have to

  • make sure that all applications have the same log file name (e.g. create a function in config.py, with a parameter "timestamp", which is provided by your script(s)
  • specify the append filemode for the fileHandler
  • make sure that config.py is not called twice somewhere, as you would add the log handlers twice, which would result in each log message being printed twice.

Case 2: One big application consisting of modules

In case we are talking about one big application, consisting of modules, you could adopt a structure like the following:

config.py:

def set_up_logging():
    # your logging setup code

module example (some_module.py):

import logging

def some_function():
    logger = logging.getLogger(__name__)

    [...]

    logger.info('sample log')

    [...]

main example (main.py)

import logging
from config import set_up_logging
from some_module import some_function

def main():
    set_up_logging()

    logger = logging.getLogger(__name__)
    logger.info('Executing some function')

    some_function()

    logger.info('Finished')

if __name__ == '__main__':
    main()

Explanation:

  • With the call to set_up_logging() in main() you configure your applications root logger
  • each module is called from main(), and get its logger via logger = logging.getLogger(__name__). As the modules logger are in the hierarchy below the root logger, those loggings get "propagated up" to the root logger and handled by the handlers of the root logger.

For more information see Pythons logging module doc and/or the logging cookbook

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.