A library isn't supposed to configure logging - that's up to the application developer. Inbar Rose's answer isn't quite right. If the module you're referring to is called foo
, then the reference to __name__
in its getLogger
call will be passing in foo
. So in your configuration code, you would need to do the equivalent of
logging.getLogger('foo').setLevel(logging.WARNING)
To include the PID in the logs, just ensure that you use an appropriate format string for your Formatters, i.e. one which includes %(process)d
. A simple example would be:
logging.basicConfig(format='%(process)d %(message)s')
Note that you can't write to the same log file from multiple processes concurrently - you may need to consider an alternative approach if you want to do this.
Update: An application developer is someone who writes Python code which is not the library, but is invoked by e.g. a user or another script via a command line or other means of creating a Python process.
To use the code I posted above, there is no need to wrap or modify the third-party code, as long as it's a library. For example, in the main script which invokes the third-party library:
if __name__ == '__main__':
# configure logging here
# sets the third party's logger to do WARNING or greater
# replace 'foo' with whatever the top-level package name your
# third party package uses
logging.getLogger('foo').setLevel(logging.WARNING)
# set any other loggers to use INFO or greater,
# unless otherwise configured explicitly
logging.basicConfig(level=logging.INFO, format='%(process)d %(message)s')
# now call the main function (or else inline code here)
main()
If the third party code runs via cron, it's not library code - it's an application, and you are probably out of luck.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…