2

I'm using python's logging module. I've initialized it as:

import logging
logger = logging.getLogger(__name__)

in every of my modules. Then, in the main file:

logging.basicConfig(level=logging.INFO,filename="log.txt")

Now, in the app I'm also using WSGIServer from gevent. The initializer takes a log argument where I can add a logger instance. Since this is an HTTP Server it's very verbose.

I would like to log all of my app's regular logs to "log.txt" and WSGIServer's logs to "http-log.txt".

I tried this:

logging.basicConfig(level=logging.INFO,filename="log.txt")
logger = logging.getLogger(__name__)

httpLogger = logging.getLogger("HTTP")
httpLogger.addHandler(logging.FileHandler("http-log.txt"))
httpLogger.addFilter(logging.Filter("HTTP"))

http_server = WSGIServer(('0.0.0.0', int(config['ApiPort'])), app, log=httpLogger)

This logs all HTTP messages into http-log.txt, but also to the main logger.

How can I send all but HTTP messages to the default logger (log.txt), and HTTP messages only to http-log.txt?

EDIT: Since people are quickly jumping to point that this Logging to two files with different settings has an answer, plese read the linked answer and you'll see they don't use basicConfig but rather initialize each logger separately. This is not how I'm using the logging module.

3
  • Duplicate of https://stackoverflow.com/questions/11232230 Commented Aug 24, 2018 at 15:28
  • None of those use basicConfig or module-level logging. Please don't be so dupe-happy and take time to read the questions. Commented Aug 24, 2018 at 15:33
  • 1
    @jonrsharpe Not sure about the duplicate. The question and answers there do not deal with two loggers along the same branch of the logging tree. Commented Aug 24, 2018 at 15:41

1 Answer 1

3

Add the following line to disable propagation:

httpLogger.propagate = False

Then, it will no longer propagate messages to its ancestors' handlers which includes the root logger for which you have set up the general log file.

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

Comments

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.