Search code examples
pythonloggingpython-logging

Configuring the logging of a third party script


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

But I want to configure the logging which is done by the script and its libraries. The script uses the standard python logging, but does not support configuration of it.

The script uses this pattern:

import logging
logger=logging.getLogger(__name__)

Use cases:

  • I want INFO messages of file foo.py to be ignored.
  • I want to include the PID in the loggings messages.

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

The script gets called via cron.

How can I configure the logging if this script?

Important

Creating a wrapper script like in this answer is not a solution for me.

The linux process hierarchy looks like this:

Cron -> third_party_script

There should be any "glue", "wrapping" or "dirty-hack" script between cron and third_party_script.

Why obtrusive/netpicking?

I want to practice "separation of concerns". I want to be able to configure logging one time and in one place. This configuration should get used by all python code of a virtualenv. Writing a wrapper would be a work-around. I want a solution.

Update

Several months later I think a pth file would an simple answer.


Solution

  • I asked this question several months ago. Unfortunately I got no answer which satisfied me.

    The distinction between using logging and setting it up is important for me.

    This is my solution: In our context we set up logging in a method which gets called in usercustomize.py.

    This way the optional plugins can use the logging without the need to set it up.

    This almost solved all my needs.

    Up to now I found no better way than usercustomize.py. My perfect solution would be something I would call virtualenvcustomize.py: Some initialization code which gets run if the interpreter loads virtualenv. Up to now I could not find such a hook. Please let me know if you have a solution.