settings up logging in console script
Question: 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?


Answer: 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).

