0

I have a simple custom filter that simply change the log level of an error to a warning. The code is

import logging

LOG = logging.getLogger(__name__)

class MyErrorFilter(logging.Filter):
    def filter(self, record):
        if record.levelname == 'ERROR':
            print 'get record', str(record), record.levelname, record.levelno
            LOG.warn('check something')
            record.levelname = 'WARNING'
            record.levelno = logging.WARNING
    return 1

The filter worked well except that the LOG.warn(...) inside my filter did not. I could see the printed message and see the changed log level except the warning message check something. Is this an expected behavior or i made mistakes here? Thanks!

1 Answer 1

1

Don't log from inside logging code, which is what a Filter essentially is - it's called from inside the logging machinery. Logging is designed for logging from applications and libraries, but not for logging what's happening inside logging. I'm not saying it won't ever work, but it's not good practice. There's not enough information to be able to answer your question - for example, your logging configuration is not given, so there's no way of telling why your LOG.xxx doesn't produce any output.

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.