1

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')
checkmate711
  • 3,301
  • 2
  • 35
  • 45
  • Calling os.remove("output.log") or logger.remove() before logger.add() would help ? You also need to check if this exists or not if you use this function to avoid errors. – gajendragarg Feb 20 '22 at 10:57

1 Answers1

3

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"))

Alex
  • 798
  • 1
  • 8
  • 21
  • 1
    The `"w"` flag can be passed to `.add()` and it should work fine actually: `logger.add("file_{time}.log", mode="w")`. – Delgan Mar 09 '22 at 14:31