13

I set up logging throughout my python package using a logconfig.ini file.

[loggers]
keys=extracts,root

[formatters]
keys=simple,detailed

[handlers]
keys=file_handler

[formatter_simple]
format=%(module)s - %(levelname)s - %(message)s
datefmt=%Y-%m-%d %H:%M:%S

[formatter_detailed]
format=%(asctime)s %(name)s:%(lineno)s %(levelname)s %(message)s
datefmt=%Y-%m-%d %H:%M:%S

[handler_file_handler]
class=logging.handlers.RotatingFileHandler
level=DEBUG
formatter=detailed
args=('/ebs/logs/foo.log', 'a', 100000000, 3)

[logger_extracts]
level=DEBUG
handlers=file_handler
propagate=1
qualname=extracts

[logger_root]
level=NOTSET
handlers=

But whenever I run my application, I get the following warning message in prompt,

No handlers found for logger __main__

How can I fix this?

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
Zihs
  • 347
  • 2
  • 4
  • 17
  • How are you calling the logger? I mean after `import logging' how do you create a logger? Include code sample if possible. I do not think it has anything to do with the logconfig. – helloV Dec 10 '14 at 22:53
  • 1
    LOG = logging.getLogger(__name__) – Zihs Dec 10 '14 at 22:55

2 Answers2

16

You have to call logging.basicConfig() first:

Logging HOWTO

The call to basicConfig() should come before any calls to debug(), info() etc. As it’s intended as a one-off simple configuration facility, only the first call will actually do anything: subsequent calls are effectively no-ops.

Or all logging.info('Starting logger for...') which will call logging.basicConfig() automatically. So something like:

import logging
logging.info('Starting logger for...') # or call logging.basicConfig()
LOG = logging.getLogger(name)

The module author's reason for this behavior is here

helloV
  • 50,176
  • 7
  • 137
  • 145
  • I call logging.config.fileConfig(logfile) in my __init__.py file – Zihs Dec 10 '14 at 23:09
  • I don't want to call BasicConfig. I think it may have to do with my logconfig.ini file but i can't figure out where – Zihs Dec 10 '14 at 23:32
2

I found my error. It turns out the the root logger is used for main. I just need to attach a handler to the root logger as so,

[logger_root]
level=NOTSET
handlers=file_handler
Zihs
  • 347
  • 2
  • 4
  • 17