0

I want logger to print INFO messages from all my code but not from 3rd party libraries. This is discussed in multiple places, but the suggested solution does not work for me. Here is my simulation of external library, extlib.py:

#!/usr/bin/env python3
from logging import info

def f():
    info("i am extlib f()")

My module:

#!/usr/bin/env python3
import logging
from logging import info
import extlib

logging.basicConfig(level=logging.INFO)
info("I am mymodule")
extlib.f()

Output:

INFO:root:I am mymodule

INFO:root:i am extlib f()

My attempt to only enable INFO for local module:

#!/usr/bin/env python3
import logging
from logging import info
import extlib

logging.getLogger(__name__).setLevel(logging.INFO)
info("I am mymodule")
extlib.f()

Output: nothing

Desired output:

INFO:root:I am mymodule

What am I doing wrong?

Muposat
  • 1,476
  • 1
  • 11
  • 24

1 Answers1

2

The problem is that they aren't using the logger class in the external library. If they were then you could filter it out. I'm not certain you can stop them from logging information since they are using the info function call. Here is a workaround though.

Suppressing output of module calling outside library

Update:

here's how you do loggers

import logging

# create logger
logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)

# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)

# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

# add formatter to ch
ch.setFormatter(formatter)

# add ch to logger
logger.addHandler(ch)

# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')
Community
  • 1
  • 1
bravosierra99
  • 1,331
  • 11
  • 23
  • 1. The question is about logging libary, while your link points to output redirection. 2. "they" are using logging library, and in my example "they" are ``extlib.py`` which clearly uses logging library. – Muposat Oct 06 '16 at 14:58
  • yes? I understand that? They are using functions from the logger library, instead of using the logger class from the logger library. That's the problem.... – bravosierra99 Oct 06 '16 at 14:59
  • another thought, did you try changing the logging level before you call their code? that might work. Don't have a chance to test it out right now. You can't recall basicConfig, but you can change the level directly – bravosierra99 Oct 06 '16 at 15:04
  • I did not realize that it can be used as an object. I tried to use it as an object: ``logger = logging.getLogger(__name__)``; ``logger.setLevel(logging.INFO)``;``logger.info("I am mymodule")`` but I still don't see any output. – Muposat Oct 06 '16 at 15:09
  • Oh, I did not get into the advanced tutorial that far. I figured basic usage was enough to get me started but ended up with full dump of SQLAlchemy logs on my hands, which, by the way, does use logging class. – Muposat Oct 06 '16 at 15:49