加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
manager.py 4.08 KB
一键复制 编辑 原始数据 按行查看 历史
sincher 提交于 2022-07-28 16:24 . flask重新打包
# coding=utf-8
# from flask import Flask
# from redis import StrictRedis
from werkzeug.routing import BaseConverter
import os
import sys
from flask_session import Session
BASE_DIR=os.path.dirname(os.path.abspath(__file__))
class HTMLConverter(BaseConverter):
regex = '.*'
# 路由正则
class RegexConverter(BaseConverter):
def __init__(self, url_map, *items):
super(RegexConverter, self).__init__(url_map)
self.regex = items[0]
#log配置,实现日志自动按日期生成日志文件
def make_log():
import logging
from logging.handlers import RotatingFileHandler
import time
from configparser import ConfigParser
cfg = ConfigParser()
cfg.read('config/config.ini', encoding='utf-8')
make_dir_path = cfg.get('log','path')
path = make_dir_path.strip()
if not os.path.exists(path):
os.makedirs(path)
log_file_name = 'logger-' + time.strftime('%Y-%m-%d', time.localtime(time.time())) + '.log'
log_file_folder = path + os.sep + log_file_name
# log_file_folder = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)) + os.sep + log_dir_name
# make_dir(log_file_folder)
logging.basicConfig(level=logging.DEBUG)
# log_file_str = log_file_folder + os.sep + log_file_name
# log_level = logging.DEBUG
handler = RotatingFileHandler(os.path.join(BASE_DIR, log_file_folder), maxBytes=1024 * 1024 * 100,backupCount=10)
# handler = logging.FileHandler(log_file_str, encoding='UTF-8')
logging_format = logging.Formatter(
'%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(lineno)s - %(message)s')
handler.setFormatter(logging_format)
logging.getLogger().addHandler(handler)
def console_out(logFilename):
''' Output log to file and console '''
# Define a Handler and set a format which output to file
import logging
from logging.handlers import RotatingFileHandler
# logging.basicConfig(
# level = logging.DEBUG, # 定义输出到文件的log级别,
# format = '%(asctime)s %(filename)s : %(levelname)s %(message)s', # 定义输出log的格式
# datefmt = '%Y-%m-%d %A %H:%M:%S', # 时间
# filename = logFilename, # log文件名
# filemode = 'w') # 写入模式“w”或“a”
# Define a Handler and set a format which output to console
logging.basicConfig(level=logging.DEBUG)
console = RotatingFileHandler(os.path.join(BASE_DIR, logFilename), maxBytes=1024 * 1024 * 100,backupCount=10)
# console = logging.StreamHandler() # 定义console handler
# console.setLevel(logging.INFO) # 定义该handler级别
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(lineno)s - %(message)s') #定义该handler格式
console.setFormatter(formatter)
# Create an instance
logging.getLogger().addHandler(console) # 实例化添加handler
def create_app(app,config):
# app=Flask(__name__)
app.config.from_object(config)
app.url_map.converters['html']=HTMLConverter
app.url_map.converters['regex'] = RegexConverter
#session初始化,将session存储到redis中
# Session(app)
# 创建redis存储对象
# redis_store = StrictRedis(host=config.REDIS_HOST, port=config.REDIS_PORT)
# app.redis = redis_store
# 日志处理
# import logging
# from logging.handlers import RotatingFileHandler
# logging.basicConfig(level=logging.DEBUG)
# file_log_handler = RotatingFileHandler(os.path.join(BASE_DIR, "logs/myblog.log"), maxBytes=1024 * 1024 * 100,backupCount=10)
# formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(lineno)s - %(message)s')
# file_log_handler.setFormatter(formatter)
# logging.getLogger().addHandler(file_log_handler)
# console_out('logs/logging.log')
make_log()
return app
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化