1

I have a script working with multiple modules that include loggers (lets assume modules A and B).

When executing, functions of A will automatically create a logger, but to keep things tidy I would like messages from module B only to be displayed in my stdout.

Is there a convenient way to disable all print statements for every function / class coming from module A, without explicitly referring to the function that prints? Im thinking about something like a line of code at the start of the script handling that.

Many thanks!

John Titor
  • 461
  • 3
  • 13

2 Answers2

0

This answer only answers the “disable all print statements” part of your question.

You can try inserting the following somewhere at the beginning of the module:

print = lambda *a, **kw: None

This way, all further uses later in the module will use the module's global print instead of the builtin print.

If you need help about redirecting the logging output, please provide the code that sets up the logger you use in the module.

tzot
  • 92,761
  • 29
  • 141
  • 204
0

There are two approaches:

  1. You can monkey patch the print function/logger object in module A's namespace and replace it with your own. This is quick and dirty, but would not require any modifications in module A. I'd highly recommend against doing this if possible, you'll just get into trouble.

  2. If you can modify module A, the proper way to do this is to ensure that module A use the python logger for all its output, and that it sets a logger name. In your logging configuration, you can then specify a LoggerFilter or LoggerAdapter to selectively suppress log entries based on logger name prefix or other attributes of the LogEntry.

Lie Ryan
  • 62,238
  • 13
  • 100
  • 144