如何解决Python Flask 记录到多个文件
我有一个 Flask 应用程序,它从 2 个不同的文件调用 2 个函数。 我已经设置日志记录到 2 个不同的文件。 但是,日志记录似乎总是附加到一个文件中(无论先到达哪个端点)
这里是文件的结构 -
- app.py
document.getElementById('add').addEventListener('click',function(e){
e.preventDefault();
// getting the list
var list=document.getElementById('playlist');
// getting the value from form
var text=document.getElementById('title').value;
// creating the new list item
var newTag =document.createElement('li');
// setting the class to list item
newTag.setAttribute('class','list-group-item');
// appending the list item value that is got from the form
newTag.appendChild(document.createTextNode(text));
// adding the list item to the list
list.appendChild(newTag)
// console.log(newTag)
});
var list=document.getElementById('playlist');
list.addEventListener('click',remove);
function remove(e){
if(e.target.classList.contains('delete')){
var li=e.target.parentElement;
list.removeChild(li)
}
}
let addbtn=document.getElementById('addtoplayer');
addbtn.addEventListener('click',addtoplayer)
function addtoplayer(){
let getUi=document.getElementById('playlist');
getUi=document.getChildNodes;
console.log(getUi)
}
- file1.py
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Js Test</title>
<link rel="STYLESHEET" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css">
</head>
<body>
<form action="" id="add-data" class="m-4">
<input type="text" id="title" class="form-control m-2">
<input type="submit" value="Add" class="btn btn-primary m-2" id="add">
</form>
<div class="container">
<div class="card shadow">
<div class="card-header text-center bg-light">
<h2><small>Playlist</small></h2>
</div>
<div class="card-body p-0">
<ul id="playlist" class="list-group">
<li class="list-group-item" data-name="song title" data-artits="artist name" data-path="path-to-song.mp3" data-pic="cover.jpg">song 1<span class="badge badge-danger float-right delete" data-id="1" id="remove">X</span></li>
<li class="list-group-item" data-name="song title" data-artits="artist name" data-path="path-to-song.mp3" data-pic="cover.jpg">song 2<span class="badge badge-danger float-right delete" data-id="2" id="remove">X</span></li>
<li class="list-group-item" data-name="song title" data-artits="artist name" data-path="path-to-song.mp3" data-pic="cover.jpg">song 3<span class="badge badge-danger float-right delete" data-id="3" id="remove">X</span></li>
<li class="list-group-item" data-name="song title" data-artits="artist name" data-path="path-to-song.mp3" data-pic="cover.jpg">song 4<span class="badge badge-danger float-right delete" data-id="4" id="remove">X</span></li>
</ul>
</div>
</div>
<button id="addtoplayer" class="btn btn-dark btn-block my-4">Add to player</button>
<div id="player"></div>
</div>
<script src="main.js"></script>
</body>
</html>
- file2.py
from file1 import fun1
from file2 import fun2
@app.route("/end_point1")
def data_1:
return fun1()
@app.route("/end_point2")
def data_2:
return fun2()
当我运行单个 python 文件 - file1.py / file2.py 时,这个记录很好(单独) 但是当我运行 API 时,日志只会附加到一个文件中。
我的日志记录有什么问题?我该如何解决?
解决方法
将此添加到 logger_setup.py
import logging
from pathlib import Path
formatter = logging.Formatter('%(asctime)s - %(levelname)s - [%(filename)s:%(lineno)d] - %(message)s')
def setup_logger( name,log_file,level=logging.DEBUG):
my_file = Path(log_file)
# print("check the if condition for the file")
# print(my_file.is_file())
if my_file.is_file():
#print(logging.getLogger(name).hasHandlers())
# if logging.getLogger(name).hasHandlers():
if len(logging.getLogger(name).handlers)>0:
return logging.getLogger(name)
else:
handler = logging.FileHandler(log_file,mode='a')
handler.setFormatter(formatter)
logger = logging.getLogger(name)
logger.setLevel(level)
logger.addHandler(handler)
logger.propagate = False
return logger
else:
handler = logging.FileHandler(log_file,mode='a')
handler.setFormatter(formatter)
logger = logging.getLogger(name)
logger.setLevel(level)
logger.addHandler(handler)
logger.propagate = False
return logger
在文件 1 和 2 中,您可以使用类似这样的内容
import logging
import logger_setup
def fun1():
log_location = 'logs'
logger = logger_setup.setup_logger(__name__,log_location+__name__+'.log')
logger.info("Logging function 1 details to to file1")
return "1"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。