Python Rotating log handler adding data in all the files


Python Rotating log handler adding data in all the files



I have the below log setup in python:


def log_setup(logger_name, log_file, level=logging.ERROR):
l = logging.getLogger(logger_name)
formatter = logging.Formatter('%(asctime)s %(message)s')
fileHandler = logging.FileHandler(log_file, mode='a')
fileHandler.setFormatter(formatter)
streamHandler = logging.StreamHandler()
streamHandler.setFormatter(formatter)

l.setLevel(level)
l.addHandler(fileHandler)
l.addHandler(streamHandler)



where log_file is the path to the file. I was using above function as :


debug_file = logs_path + 'myfile_debug.log'
log_setup('debug', debug_file) #debug_file is path to the log file
log_debug = logging.getLogger('debug')



I wanted to add RotatingFileHandler so modified the above code to below:


RotatingFileHandler


def log_setup(logger_name, log_file, level=logging.ERROR):
l = logging.getLogger(logger_name)
formatter = logging.Formatter('%(asctime)s %(message)s')
fileHandler = logging.FileHandler(log_file, mode='a')
fileHandler.setFormatter(formatter)
streamHandler = logging.StreamHandler()
streamHandler.setFormatter(formatter)

handler = RotatingFileHandler(log_file, maxBytes=20, backupCount=4) #ADDED

l.setLevel(level)
l.addHandler(fileHandler)
l.addHandler(streamHandler)
l.addHandler(handler) #ADDED



When this was executed, it created 4 log file and each log file contains 1 line of log.



So for ex, if below is the actual logs:


Connected to device id MACHINE1
Connected to Internet TRUE
Connected to hub TRUE
Publishing data FALSE



Then after modification, it was like below:



myfile_debug.log.1 contains: Connected to device id MACHINE1


Connected to device id MACHINE1



myfile_debug.log.2 contains: Connected to Internet TRUE


Connected to Internet TRUE



myfile_debug.log.3 contains: Connected to hub TRUE


Connected to hub TRUE



myfile_debug.log.4 contains: Publishing data FALSE


Publishing data FALSE



myfile_debug.log contains: {All the logs}


{All the logs}



I thought it would create a new file once the size exceed. Why is this showing this behavior? Thanks









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

PHP contact form sending but not receiving emails

PHP parse/syntax errors; and how to solve them?

iOS Top Alignment constraint based on screen (superview) height