0

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?

P.S.:

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

1
  • Well, I'm not sure if we can exactly help. This might be a risky move, but you could try to replace the logging module with your own. Commented Jun 14, 2022 at 22:16

1 Answer 1

1

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 usebasicConfig() - use the other APIs provided.

Sign up to request clarification or add additional context in comments.

1 Comment

In either case, you can use the removeHandler method for the appropriate Logger instance to remove the FileHandler that writes to the given file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.