0

Python newbie. Now I have been stuck around this for a while. When I try to write logs in a file using ini configuration nothing is captured in the file. I tried to debug the problem but couldn't get any clue. Writing logs without using ini file works perfectly fine.

Below is the code and ini file

import logging

from logging.config import fileConfig

def info(message):


    fileConfig('logging_config.ini')
    logger=logging.getLogger("simple logger")

    logger.warning('Something is not right')
    logger.warning(message)

logging_config.ini

[loggers]
keys=root

[handlers]
keys=file_handler

[logger_root]
level=WARNING
handlers=file_handler

[formatters]
keys=formatter

[formatter_formatter]
format='%(message)s'

[handler_file_handler]
class=FileHandler
level=WARNING
formatter=formatter
args=('dummyoutput.log','w')

I checked the logger object also to see if I can get any clue from its properties. Below is the object

{'disabled': 0,
 'filters': [],
 'handlers': [<logging.FileHandler object at 0x7ff03358ce90>],
 'level': 30,
 'name': 'root',
 'parent': None,
 'propagate': 1}

Not sure if its helpful but I noticed the property disabled was earlier shown TRUE but now it is 0 every time.

Has anyone got any clue on this?

Update: The issue was due to multiple calls to logging.config.fileConfig() for the same configuration file. But I couldn't really understand why nothing was written when that function was called for the last time. Any idea on that??

qwerty
  • 2,392
  • 3
  • 30
  • 55

2 Answers2

2

There is a naming convention for loggers, and the name "simple logger" is not valid because of the space.

You should replace the space by a period. We usually use Python package names for loggers.

Here is how you can fix that:

import logging

from logging.config import fileConfig


def info(message):

    fileConfig('logging_config.ini')
    logger = logging.getLogger("simple.logger")

    logger.warning('Something is not right')
    logger.warning(message)


if __name__ == "__main__":
    info("hello")

It woks perfectly.

'Something is not right'
'hello'

Quoting Logger Objects:

The name is potentially a period-separated hierarchical value, like foo.bar.baz (though it could also be just plain foo, for example). Loggers that are further down in the hierarchical list are children of loggers higher up in the list. For example, given a logger with a name of foo, loggers with names of foo.bar, foo.bar.baz, and foo.bam are all descendants of foo. 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.

Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103
  • Oh yes...you're right. But with that also its not working. Also, I tried earlier mostly without giving logger any name `logger = logging.getLogger()` but that also didn't worked. No idea whats going wrong there. – qwerty May 27 '17 at 07:06
  • 1
    Can you explain exactly what's not working (because I have tested it). Note: you should use a full path in your config INI. Have you found your log file? – Laurent LAPORTE May 27 '17 at 10:36
  • The log file was already being generated. The code you shared is perfectly fine. Actually the problem was that the `info(message)` was getting called more than once and so was `fileConfig()` for the same configuration file as it was inside info function *[please check my answer below for details]*. But I couldn't really understand why nothing was written when that function was called for the last time. Any idea on that?? – qwerty May 27 '17 at 10:48
0

Figured out what the (stupid) mistake was. Actually the info(message) was getting called more than once and so was fileConfig() for the same configuration file as it was inside info function. Hence, the issue. Modified code as below and it worked.

import logging
from logging.config import fileConfig

def info(message):

    logger.warning('Something is not right')
    logger.warning(message)

fileConfig('logging_config.ini')
logger=logging.getLogger("simple.logger")

Update: Even if we don't follow the logger naming convention for eg. as I gave simple logger instead of simple.logger it works fine.

qwerty
  • 2,392
  • 3
  • 30
  • 55