With the python log library 'loguru', is there a way to change the log file name after declaring it?
Thanks!
from loguru import logger
logger.add('dummy.log', format="{time:YYYY-MM-DD HH:mm:ss} {level} {message}")
.....
# How to change log filename?
logger.debug('my message')
In loguru, the add method returns a logger id, which can be used to remove a logger that is no longer useful (with the remove method). This is mentioned here in the documentation.
This translates as:
from loguru import logger
if __name__ == "__main__":
dummy_logger_id: int = logger.add("dummy.log", format="{time:DD-MM-YYYY HH:mm:ss} {level} {message}")
logger.info("Only dummy")
test_logger_id: int = logger.add("test.log", format="{time:DD-MM-YYYY HH:mm:ss} {level} {message}")
logger.info("Both")
logger.remove(dummy_logger_id)
logger.info("Only test")
This results in two files, dummy.log and test.log:
dummy.log
15-07-2022 08:15:12 INFO Only dummy
15-07-2022 08:15:12 INFO Both
test.log
15-07-2022 08:15:12 INFO Both
15-07-2022 08:15:12 INFO Only test
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With