'How to Add Unique Identifier to Logging Format Per Function Call
I'm trying to add a unique id to logger calls. This is where I've got to:
import logging
def logging_test(identifier):
logging.basicConfig(format=f'%(asctime)s - %(name)s - %(levelname)s - %(message)s - [{identifier}]')
logging.error('hello')
logging_test('one')
logging_test('two')
This gives output:
2022-02-10 14:01:35,011 - root - ERROR - hello - [one]
2022-02-10 14:01:35,011 - root - ERROR - hello - [one]
But my desired output is:
2022-02-10 14:01:35,011 - root - ERROR - hello - [one]
2022-02-10 14:01:35,011 - root - ERROR - hello - [two]
The function I'm working on is very long, so I'm trying to avoid doing something like this since it would require writing dozens of lines of logger.error(message + identifier)
import logging
def logging_test(identifier):
logging.error(f'hello {identifier}')
logging_test('one')
logging_test('two')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
