在我的 Flask 应用程序中,我使用该库实现了一个日志系统logging。它当前在以下函数中运行:
if __name__ == "__main__":
"""[Runs the webserver.
Finally block is used for some logging management. It will first shut down
logging, to ensure no files are open, then renames the file to 'log_'
+ the current date, and finally moves the file to the /logs archive
directory]
"""
try:
session_management.clean_uploads_on_start(UPLOAD_FOLDER)
app.run(debug=False)
finally:
try:
logging.shutdown()
new_log_file_name = log_management.rename_log(app.config['DEFAULT_LOG_NAME'])
log_management.move_log(new_log_file_name)
except FileNotFoundError:
logging.warning("Current log file not found")
except PermissionError:
logging.warning("Permissions lacking to rename or move log.")
我发现如果强制关闭 cmd 提示符或服务器崩溃,则文件不会重命名和移动。我认为在服务器启动之前将重命名并移入函数的初始“try”块可能会更好,但我遇到了问题,因为我有一个配置文件(在此脚本中导入)具有以下代码:
logging.basicConfig(filename='current_log.log', level=logging.INFO,
filemode='a',
format='%(asctime)s:%(levelname)s:%(message)s')
我试过做类似下面的事情,但我仍然遇到权限错误,但我认为我仍然遇到错误,因为 log_management 脚本也导入了配置。此外,我找不到启动日志系统的函数,类似于在系统结束时使用的 logging.shutdown() ,否则我会关闭它,移动文件(如果存在)并重新启动它。
try:
session_management.clean_uploads_on_start(UPLOAD_FOLDER)
log_management.check_log_on_startup(app.config['DEFAULT_LOG_NAME'])
import config
app.run(debug=False)
finally:
try:
logging.shutdown()
new_log_file_name = log_management.rename_log(app.config['DEFAULT_LOG_NAME'])
log_management.move_log(new_log_file_name)
except FileNotFoundError:
logging.warning("Current log file not found")
except PermissionError:
logging.warning("Permissions lacking to rename or move log.")
# (in another script)
def check_log_on_startup(file_name):
if os.path.exists(file_name):
move_log(rename_log(file_name))
非常欢迎任何建议,因为我觉得我在一堵砖墙上!
繁花如伊
相关分类