如何将大请求记录为文件? [英] How to log large requests as files?
问题描述
我正在使用Flask应用程序来解决车辆路线问题.它收到大量我想记录在常规日志消息(例如 app.logger.info(..)
)之外的请求(每个请求约5MB),因为它们只会使日志变得不可读.>
我的想法是将最后一个 n 请求作为带有时间戳的json文件保存在一个单独的文件夹中.我将编写自己的函数来执行此操作.我想知道您以前是否使用过这种类型的设置以及如何实现它.我在Python的模块 logging
中找不到任何东西可以帮我完成这个任务.
我不建议使用logger模块保存大型json文件,尤其是如果您还希望格式化它们以供人类使用时.
我实际上和您过去有同样的问题,并通过编写以下函数解决了该问题:
def json_to_file(file_to_write,json_to_dump):max_files = 10file_list = sorted(glob.glob(此处为您的模式"))如果len(file_list)>max_files:os.remove(file_list [0])使用open(file_to_write,"w +")作为my_file:json.dump(json_to_dump,my_file,indent = 4)
您可以将file_to_write变量设置为unix或utc时间戳-无论如何,这就是我要做的事情.
添加了最大n个文件"部分.只需确保您使用的glob模式正确!
I am using a Flask app to solve vehicle routing problems. It receives large requests (around 5MB each) that I would like to log outside of the usual logging messages (e.g. app.logger.info(..)
) because they just render the logs unreadable.
My idea is to save the last n requests as json files with timestamps in a separate folder. I am about to write my own function to do this. I am wondering if you have used this type of setup before and how you implemented it. I couldn't find anything in Python's module logging
to do this for me.
I wouldn't recommend using the logger module to save large json files, especially if you also want them formatted for human consumption.
I actually had the same problem as you in the past, and solved it writing the following function:
def json_to_file(file_to_write, json_to_dump):
max_files = 10
file_list = sorted(glob.glob("your pattern here"))
if len(file_list) > max_files:
os.remove(file_list[0])
with open(file_to_write, "w+") as my_file:
json.dump(json_to_dump, my_file, indent=4)
You could format the file_to_write variable to be a unix or utc timestamp - that's what I do anyway.
Edit: added the "max n files" part. Just make sure you're using the right pattern with glob!
这篇关于如何将大请求记录为文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!