Python Logging - Maxbytes not work in all files

14 Views Asked by At

My python version is 3.8.

My code looks like this

    import logging
    from logging.handlers import RotatingFileHandler
    filename = 'Code.log'
    log_fname = os.path.join(logs_path, filename)

    logging.basicConfig(filename=log_fname,
    format='{"@timestamp":"%(asctime)s","log.level":"%(levelname)-1s","message":"%(message)s"}',
    datefmt='%m/%d/%Y %H:%M:%S %p')
    logger = logging.getLogger('code')
    logger.setLevel(logging.INFO)
    # handler = logging.StreamHandler()
    handler = RotatingFileHandler(log_fname, maxBytes=500000, backupCount=3)
    handler.setLevel(logging.INFO)
    logger.addHandler(handler)
    logger.info("Notebook  Process Started !!!")
  1. The backupcount is working as expected. But some files reach the size limit of 1000kb instead of 500kb. How to restrict the size limit to 500kb always?

  2. Also at times, I encounter the following error when I try to open back up log file like Code.log.1

Bad message (badly formatted packet or protocol incompatibility).
Error code: 5
Error message from server: Bad message

Any help appreciated. Thanks.

0

There are 0 best solutions below