How can I implement fileRotation on json files. I know there exists a fileRotation handler in logging module of python but that is used to rotate and delete log files. My program is reading and writing using json module, although I can also use logging module to write in json form into the files but I don't think that is a good practice. Is there any other library I can use or will I have to write my own script to do rotation and deletion?
Asked
Active
Viewed 27 times
-1
-
What have you tried? https://stackoverflow.com/help/minimal-reproducible-example – DarkKnight Jul 10 '23 at 06:04
1 Answers
2
You can use the logging and json modules to achieve this as follows:
import logging
import json
import os
from logging.handlers import RotatingFileHandler
def rotate_json_file(file_path, max_bytes, backup_count):
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
file_handler = RotatingFileHandler(file_path, mode='a', maxBytes=max_bytes, backupCount=backup_count)
file_handler.setFormatter(logging.Formatter('%(message)s'))
logger.addHandler(file_handler)
json_data = {'key': 'value'}
logger.info(json.dumps(json_data))
logger.removeHandler(file_handler)
file_handler.close()
record = logging.makeLogRecord(json_data)
if file_handler.shouldRollover(record):
# Get the rotated file names
rotated_files = [file_path] + [file_path + '.{}'.format(i) for i in range(1, backup_count + 1)]
os.remove(rotated_files[-1])
for i in range(len(rotated_files) - 1, 0, -1):
os.rename(rotated_files[i - 1], rotated_files[i])
open(file_path, 'a').close()
If you would like it to be Time based:
import logging
import json
import os
from logging.handlers import TimedRotatingFileHandler
def rotate_json_file(file_path, when, interval, backup_count):
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
file_handler = TimedRotatingFileHandler(file_path, when=when, interval=interval, backupCount=backup_count)
file_handler.setFormatter(logging.Formatter('%(message)s'))
logger.addHandler(file_handler)
json_data = {'key': 'value'}
logger.info(json.dumps(json_data))
logger.removeHandler(file_handler)
file_handler.close()
record = logging.makeLogRecord(json_data)
if file_handler.shouldRollover(record):
# Get the rotated file names
rotated_files = [file_handler.baseFilename] + file_handler.getFilesToDelete()
os.remove(rotated_files[-1])
for i in range(len(rotated_files) - 1, 0, -1):
os.rename(rotated_files[i - 1], rotated_files[i])
open(file_handler.baseFilename, 'a').close()
Where we use the "TimedRotatingFileHandler" instead.

Lashen Dharmadasa
- 197
- 8
-
I think you are missing 'record' argument of the function RotatingFileHandler.shouldRollover() because I get the following error when I run your code ```TypeError: RotatingFileHandler.shouldRollover() missing 1 required positional argument: 'record'``` What would I pass into the shouldRollover function? Also thank you so much for your answer! – Abdullah Chaudhry Jul 10 '23 at 06:15
-
-
-
Your forgot to add logRecord object, please add the following line ```record = logging.makeLogRecord(json_data)``` – Abdullah Chaudhry Jul 10 '23 at 06:33
-
My bad, I was doing it all in my head because I have experience doing this stuff, fixed it! – Lashen Dharmadasa Jul 10 '23 at 06:38
-
Can you implement this using Time based rotation? I spent last two hours trying to figure it out but it is not working your help would be much appreciated – Abdullah Chaudhry Jul 10 '23 at 08:39
-
Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/254427/discussion-between-lashen-dharmadasa-and-abdullah-chaudhry). – Lashen Dharmadasa Jul 11 '23 at 00:42
-