'python logging close logger
I am trying to close a logger in the sense that I Want it to be close and reopened when a new pbject is created:
import logging, logging.config
class myclass:
def __init__(self, data):
""" Initializes the main attributes of the parent class """
logging.config.dictConfig({'version': 1, 'disable_existing_loggers': True})
logging.basicConfig(filename='test.log',
format='',
filemode='w', # Overrides existing log file
level=logging.INFO) # DO NOT RAISE LEVEL
self.lgr = logging.getLogger('mylogger')
self.lgr.info(f"data is {data}")
self.data=data
a = myclass(data=1)
b = myclass(data=15)
test.log now contains:
data is 1
data is 15
I would have wanted to only contain 15, basically the file containing data is 1 would have been overwritten.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
