Python Forum
create new log file on logging?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
create new log file on logging?
#1
Hello,
I'm using logging and I have notice that the file is not clean after reboot (program start again)
what do I need to do in order to "clean" the log file ?

this is what I have :
import logging
logging.basicConfig(filename='/home/pi/logs/App1_Log.log', level=logging.INFO, format='%(asctime)s %(message)s')
.
.
.
.

logging.info('%s:%s',  Data)
Thanks ,
Reply
#2
mode = "w" in the config command.
Reply
#3
Thanks

can I also do the following (is this "legal"?)
check the size of the file , and if it's more then a wanted size - save\delete it and then start the logging in mode ='w' ?
Reply
#4
A log file is a file. Do whatever you want.
Reply
#5
You can add a handler of type RotatingFileHandler. Check the documentation and make sure you don't set either maxBytes or backupCount to 0. You could modify your earlier logging setup like so:

import logging
from logging.handlers import RotatingFileHandler
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s %(message)s'),
    handlers=[RotatingFileHandler('/home/pi/logs/App1_log.log', maxBytes=1000000, backupCount=4),
    )
buran likes this post
Reply
#6
I would advice to use Loguru.
Usage is simpler and cleaner(no boilerplate),than the one in standard library.
from loguru import logger
import time
#logger.remove() # Only info to file
logger.add("file.log", rotation="2 day")

@logger.catch
def foo():
    return 1 / 0

foo()
[Image: kPV7uG.png]
Reply
#7
thank you both
I will try both ways

Thanks !
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can rich.logging output to file? pyfoo 1 276 Mar-21-2024, 10:30 AM
Last Post: pyfoo
  Python logging RotatingFileHandler writes to random file after the first log rotation rawatg 0 417 Feb-15-2024, 11:15 AM
Last Post: rawatg
  Create Choices from .ods file columns cspower 3 615 Dec-28-2023, 09:59 PM
Last Post: deanhystad
  Recommended way to read/create PDF file? Winfried 3 2,902 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  Use PM4PY and create working file thomaskissas33 0 678 Nov-14-2023, 06:53 AM
Last Post: thomaskissas33
  Create csv file with 4 columns for process mining thomaskissas33 3 760 Nov-06-2023, 09:36 PM
Last Post: deanhystad
  logging: change log file permission with RotatingFileHandler erg 0 1,050 Aug-09-2023, 01:24 PM
Last Post: erg
  create exe file for linux? korenron 2 985 Mar-22-2023, 01:42 PM
Last Post: korenron
  my first file won't create itself MehHz2526 2 906 Nov-27-2022, 12:58 AM
Last Post: MehHz2526
  Create multiple/single csv file for each sql records mg24 6 1,414 Sep-29-2022, 08:06 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020