1

Here is what I have in the main code:

from mf import gameplay

....

if __name__ == "__main__":
    import logging
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.INFO)

    # create a file handler
    handler = logging.FileHandler('example.log')
    handler.setLevel(logging.INFO)

    # create a logging format
    formatter = logging.Formatter('%(asctime)s - %(worker)s - %(levelname)s - %(message)s')
    handler.setFormatter(formatter)

    # add the file handler to the logger
    logger.addHandler(handler)

    logger.info("Querying database for docs...", extra={'worker': 'id_1'})

and in a module gameplay I import in the main code I have

import logging
logger = logging.getLogger("__main__")
logger.info("test module")

but still I do not get any log output from the logger in the module. I do not see the text "test module" in the logging file "example.log".

I looked HERE, HERE, HERE and HERE and I think I followed what is described in all these articles but it is still not working for me. I am pretty sure I overlooked something totally simple.

What am I missing?

MacOS, python 3.9.6

E_net4
  • 27,810
  • 13
  • 101
  • 139
Alex
  • 41,580
  • 88
  • 260
  • 469

2 Answers2

0

Logging it kind of tricky in Python.

Try:

logger = logging.getLogger(__name__)

I usually do this:

logging.config.fileConfig("logging.conf", disable_existing_loggers=False)
LOG = logging.getLogger(__name__)
[loggers]
keys=root,app

[handlers]
keys=console_handler

[formatters]
keys=normal_formatter

[logger_root]
level=INFO
handlers=console_handler

[logger_app]
level=INFO
handlers=console_handler
qualname=app
propagate=0

[handler_console_handler]
class=StreamHandler
level=INFO
formatter=normal_formatter
args=(sys.stdout,)

[formatter_normal_formatter]
format=%(asctime)s %(levelname)s %(name)s %(message)s
datefmt=%Y-%m-%d %H:%M:%S
Istvan
  • 7,500
  • 9
  • 59
  • 109
  • Still does not seem to work. I "imported"(?) the logger in the module as `logger = logging.getLogger("__main__")` but the text does not make it into the logging file... – Alex Apr 16 '23 at 07:19
  • Ah wait you don't write to a file... – Alex Apr 16 '23 at 07:23
0

I guess the problem was that I defined the logger after I imported the other module. To solve this problem:

Define the logger at the very top of your main code before you import any of your modules!

If that is correct, the example given HERE must be incorrect!

In the main code I now have

import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

# create a file handler
handler = logging.FileHandler('gameplay.log')
handler.setLevel(logging.INFO)

# create a logging format and add that to the logger
formatter = logging.Formatter('%(asctime)s - %(filename)s:%(lineno)d - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)

import <other modules>

and in the other modules I have e.g.

import logging
logger = logging.getLogger("__main__")
logger.debug("Game module loaded")
Alex
  • 41,580
  • 88
  • 260
  • 469