简体   繁体   中英

Python logging, how to prevent functions from library from logging?

I'm making a project and using a library from the requirements of the project. The library implements logging and automatically logs to a file, when I call its functions.

Now, I'm implementing logging by myself, and only want my own messages to be logged to the file and not the library messages.

One solution I thought of would be switching the logging file each time I call a function from the library and then removing that file, but that seems overly complicated and clutterly. What can I do in order to not log the messages from the library?

PS:

I'm using the logging library and I initalize it as:

logging.basicConfig(level = logging.INFO,filename = loggingFile,format = "%(message)s")

, which means, all messages from myself and from the library will get logged in loggingFile

Libraries should not directly output anything to logs - that should be done only by handlers configured by an application. A library that logs output is an anti-pattern - if a library definitely does that, I'd log a bug against that library's issue tracker.

On the other hand, it might be that the library is only outputting stuff because you have configured output via your basicConfig() call. If you need more than basic configuration, don't use basicConfig() - use the other APIs provided.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM