Search code examples
pythonloguru

Loguru - how to clear the logs


is there a way clear the log file before using it with the python log library 'loguru'?

Thanks!

from loguru import logger
#  clear the log before logging
logger.add('output.log', format="{time:YYYY-MM-DD HH:mm:ss} {level} {message}")
logger.debug('my message')

Solution

  • Loguru API does not have the ability to remove/clean up log files. Instead of that, you could either use open('output.log', 'w').close() if you want to erase all contents of the log file. Or check the ability to use log rotation in Loguru if it's suitable for your use case (something like that logger.add("file_{time}.log"))