文件轮换和删除在JSON文件上。

huangapple go评论63阅读模式
英文:

File rotation and deletion on json files

问题

如何在 JSON 文件上实现文件轮换?我知道Python的日志模块中存在一个文件轮换处理程序,但那是用于轮换和删除日志文件的。
我的程序正在使用JSON模块进行读写,尽管我也可以使用日志模块以JSON格式写入文件,但我认为这不是一个好的做法。
是否有其他库可以使用,还是我需要编写自己的脚本来进行轮换和删除操作?

英文:

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?

答案1

得分: 2

你可以使用logging和json模块来实现这个,如下所示:

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):
        # 获取旋转后的文件名
        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()

如果你想要基于时间进行旋转

```python
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):
        # 获取旋转后的文件名
        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()

在这里我们使用了"TimedRotatingFileHandler"
英文:

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.

huangapple
  • 本文由 发表于 2023年7月10日 13:46:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76650926.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定