0

I want a logging module that parses the log message and sends it to a database for later analysis (I'm using the Elasticsearch database). My current implementation uses the logging module and has all the standard debug levels. To do this, I need access to the log message. Is there a method in Python's logging module that would get me access to the message?

I feel like this boils down to writing a log handler that fits within Python's logging module. Any ideas?

1

1 Answer 1

1

Python 2

class StoredLogger(logging.Logger):

    def makeRecord(self, *args, **kwargs):
        ...
        return logging.LogRecord(*args, **kwargs)

logging.setLoggerClass(StoredLogger)

Within makeRecord, you could add a call to a function like store_message, whose behavior you define.

def store_message(*args, **kwargs):
    """Write a message to the database"""
    ...

Python 3.2+

Changed in version 3.2: The creation of a LogRecord has been made more configurable by providing a factory which is used to create the record. The factory can be set using getLogRecordFactory() and setLogRecordFactory() (see this for the factory’s signature).

old_factory = logging.getLogRecordFactory()

def new_factory(*args, **kwargs):
    record = old_factory(*args, **kwargs)
    record.custom_attribute = 0xDECAFBAD
    return record

logging.setLogRecordFactory(new_factory)

You would add your call to store_message into your record_factory.

def record_factory(*args, **kwargs):
    store_message(*args, **kwargs)

    global old_factory
    return old_factory(*args, **kwargs)

Then, when you use something like logging.info('foo'), it will pass through store_message, then resume its normal behavior by writing to its out-stream. You may want to consider renaming record_factory to something more descriptive like stored_record_factory.

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

3 Comments

I suppose I should have said that I am using python 2.7.5. According to docs.python.org/3/library/logging.html: "Changed in version 3.2: The creation of a LogRecord has been made more configurable by providing a factory which is used to create the record." Is what I want to do possible under python 2.7.5? I'm still working through your code. Thanks!
@CheesePita: In Python 2, you'll need to override Logger.makeRecord in a Logger subclass. I'll add an example of that to my answer when I get a chance.
@CheesePita: I've added distinct sections for Python 2/3 to my answer.

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.