0

I have a python console script, which source I don't want to modify.

But I want to modify the logging which is done by the script and its libraries.

Examples:

  • I want messages at level ERROR to be mailed to foo@example.com
  • I want INFO messages of file foo.py to be ignored.
  • I want to include the PID in the loggings messages.

The script uses this logger:

import logging
logger=logging.getLogger(__name__)
del(logging)

How can I configure the logging, if I don't want to modify the sources of the console script?

guettli
  • 25,042
  • 81
  • 346
  • 663
  • I found a solution here: http://stackoverflow.com/questions/29962525/configuring-the-logging-of-a-third-party-script – guettli Apr 11 '16 at 12:13

2 Answers2

2

You can load it using a wrapper script. In the wrapper, set the logging configuration as desired (e.g. logging.basicConfig(), or add logging handlers as desired), and then run the script.
If the script has a main function (look for if __name__ == "__main__": in the script), you can simply import the file and run the function:

import sys
import logging
logging.basicConfig(...)
import my_console_script
sys.exit(my_console_script.main())

If it doesn't have such a function, then simply importing it will run its contents (you can omit the sys.exit() call).

micromoses
  • 6,747
  • 2
  • 20
  • 29
0

You need to get the existing lib logger, remove it and place your custom one. See also here: Configuring the logging of a third party script

TocToc
  • 4,739
  • 4
  • 29
  • 34