Hi All,
How to limit the file size and should start to write into a new file in logging. Much appreciated your help/suggestions on this. Thanks a lot.
Regards,
Maiya
Use a RotatingFileHandler, here an example copied and pasted from the famous
Mouse vs Python blog.
import logging
import time
from logging.handlers import RotatingFileHandler
#----------------------------------------------------------------------
def create_rotating_log(path):
"""
Creates a rotating log
"""
logger = logging.getLogger("Rotating Log")
logger.setLevel(logging.INFO)
# add a rotating handler
handler = RotatingFileHandler(path, maxBytes=20,
backupCount=5)
logger.addHandler(handler)
for i in range(10):
logger.info("This is test log line %s" % i)
time.sleep(1.5)
#----------------------------------------------------------------------
if __name__ == "__main__":
log_file = "test.log"
create_rotating_log(log_file)
logging_file = 'test.log'
logging_format = '%(asctime)s: [%(levelname)s] [{}] %(message)s'.format(self.action)
logging.basicConfig(
filename = '{}'.format(logging_file),
format = '{}'.format(logging_format),
level = logging.DEBUG
)
This is my code, here I need to limit the file size and rotate the file. How to do the same sir. Much appreciated for your help on this. Thank you.
Regards,
Maiya