2

I have a module name acms and inside that have number of python files.The main.py has calls to other python files.I have added logs in those files, which are displayed on console but i also want to write these logs in a file called all.log, i tried with setting log levels and logger in a file called log.py but didnt get the expected format,since am new to python am getting difficulty in handling logs

tintin
  • 35
  • 2
  • 12

1 Answers1

0

Use the logging module and use logger = logging.getLogger(__name__). Then it will use the correct logger with the options that you have set up.

See the thinkpad-scripts project for its logging. Also the logging cookbook has a section for logging to multiple locations.

We use the following to log to the console and the syslog:

    kwargs = {}
    dev_log = '/dev/log'
    if os.path.exists(dev_log):
        kwargs['address'] = dev_log
    syslog = logging.handlers.SysLogHandler(**kwargs)
    syslog.setLevel(logging.DEBUG)
    formatter = logging.Formatter(syslog_format)
    syslog.setFormatter(formatter)
    logging.getLogger('').addHandler(syslog)
Martin Ueding
  • 8,245
  • 6
  • 46
  • 92