大家好,我有一个名为“ LoggingConfig”的配置文件,其中包含所有数据。请在不喜欢此代码之前告诉我您需要的内容。我不知道如何实现配置,在Flask程序中使用。**请帮助**这是我创建的配置文件:
import logging
# create and configure logger
def logger():
logger=logging.getLogger("DATA-MANUFACTURING")
logger.setLevel(logging.DEBUG)
# create console handler and set level to debug
ch=logging.StreamHandler()
# create formatter
lOG_FORMAT= "%(levelname)s - %(name)s - %(asctime)s - %(message)s"
logging.basicConfig(filename="C:\\Users\\rahul\\Desktop\\DATA-MANUFACTURING.log", level = ch.setLevel(logging.DEBUG), format = lOG_FORMAT, filemode = "a")
为此,我想将其导入到我的烧瓶程序中,如下所示:
import logging
import LoggingConfig
import pandas as pd
import numpy as np
import random
import os
from random import randint
from flask import Flask, render_template, request, redirect, make_response
LoggingConfig.logger()
app = Flask(__name__)
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
..................................
..................................
so on.........
if __name__ == '__main__':
app.run(debug=True)
我的程序有很多东西,在这里我还没有给出我想让配置文件访问我的烧瓶程序的唯一东西,如果您想让我在这里编写整个程序,请在文本框中输入txt ...谢谢
答案 0 :(得分:1)
有不同的配置日志文件的方式: 访问此网站以获取更多信息:https://docs.python-guide.org/writing/logging/ 对于您来说,我更喜欢INI格式 进入程序,再创建一个名为“ Yourfilename.ini”的文件
[loggers]
keys=root,DATA-MANUFACTURING
[handlers]
keys=consoleHandler,fileHandler
[formatters]
keys=fileFormatter,consoleFormatter
[logger_root]
level=DEBUG
handlers=consoleHandler
[logger_DATA-MANUFACTURING]
level=DEBUG
handlers=consoleHandler,fileHandler
qualname=DATA-MANUFACTURING
propagate=0
[handler_consoleHandler]
class=StreamHandler
level=WARNING
formatter=consoleFormatter
args=(sys.stdout,)
[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=fileFormatter
args=('logfile.log', 'a')
[formatter_fileFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=
[formatter_consoleFormatter]
format=%(levelname)s - %(message)s
datefmt=
现在在您的主程序中写在顶部:
import logging.config
logging.config.fileConfig( 'logging.ini')
logger = logging.getLogger('DATA-MANUFACTURING')
.........................................
并根据需要写入任何日志记录信息
logger.info("Loading the UpoadFile page")...so on.