版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ty5622022/article/details/85167191
目录
class LogFormatter(logging.Formatter):
def define_logging_options(options=None):
tornado-log
操作姿势:
logging.error('message:bug')
logging.info('message:bug')
logging.debug('message:bug')
logging.warning('sadfas')
部分源码:
/tornado/log.py
class LogFormatter(logging.Formatter):
#基于python标准库logging
access_log = logging.getLogger("tornado.access")
#基于python标准库logging
access_log = logging.getLogger("tornado.access")
app_log = logging.getLogger("tornado.application")
gen_log = logging.getLogger("tornado.general")
#日志输出形式
DEFAULT_FORMAT = \
'%(color)s[%(levelname)1.1s %(asctime)s %(module)s:%(lineno)d]%(end_color)s %(message)s'
DEFAULT_DATE_FORMAT = '%y%m%d %H:%M:%S'
#控制台各等级日志颜色
DEFAULT_COLORS = {
logging.DEBUG: 4, # Blue
logging.INFO: 2, # Green
logging.WARNING: 3, # Yellow
logging.ERROR: 1, # Red
}
/tornado/log.py
def define_logging_options(options=None):
def define_logging_options(options=None):
#options在项目初始化时设置
if options is None:
# late import to prevent cycle
import tornado.options
options = tornado.options.options
options.define("logging", default="info",
help=("Set the Python log level. If 'none', tornado won't touch the "
"logging configuration."),
metavar="debug|info|warning|error|none")
options.define("log_to_stderr", type=bool, default=None,
help=("Send log output to stderr (colorized if possible). "
"By default use stderr if --log_file_prefix is not set and "
"no other logging is configured."))
options.define("log_file_prefix", type=str, default=None, metavar="PATH",
help=("Path prefix for log files. "
"Note that if you are running multiple tornado processes, "
"log_file_prefix must be different for each of them (e.g. "
"include the port number)"))
options.define("log_file_max_size", type=int, default=100 * 1000 * 1000,
help="max size of log files before rollover")
options.define("log_file_num_backups", type=int, default=10,
help="number of log files to keep")
options.define("log_rotate_when", type=str, default='midnight',
help=("specify the type of TimedRotatingFileHandler interval "
"other options:('S', 'M', 'H', 'D', 'W0'-'W6')"))
options.define("log_rotate_interval", type=int, default=1,
help="The interval value of timed rotating")
options.define("log_rotate_mode", type=str, default='size',
help="The mode of rotating files(time or size)")
options.add_parse_callback(lambda: enable_pretty_logging(options))
RequestHandler的finish方法
->self.finish
->self._log()
def log_request(self, handler):
#log_function可自定义设置
if "log_function" in self.settings:
self.settings["log_function"](handler)
return
if handler.get_status() < 400:
log_method = access_log.info
elif handler.get_status() < 500:
log_method = access_log.warning
else:
log_method = access_log.error
request_time = 1000.0 * handler.request.request_time()
log_method("%d %s %.2fms", handler.get_status(),
handler._request_summary(), request_time)
config.settings.py
def log_func(handler):
if handler.get_status() < 400:
log_method = logging.info
elif handler.get_status() < 500:
log_method = logging.warning
else:
log_method = logging.error
request_time = 1000.0 * handler.request.request_time()
log_method("%d %s %s (%s) %s %s %.2fms",
handler.get_status(), handler.request.method,
handler.request.uri, handler.request.remote_ip,
handler.request.headers["User-Agent"],
handler.request.arguments,
request_time)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
settings = {
"git_local_path": "/Library/WebServer/Documents/python3/test",
"git_patch_dir": "gitPatch/",
"cookie_secret": "bZJc2sWbQLKos6GkHn/VB9oXwQt8S0R0kRvJ5/xJ89E=",
"debug": True,
"log_function": log_func
}
配置 在__main__文件配置日志输出形式
tornado.options.options.logging = "debug" # 日志等级 "debug|info|warning|error|none"
tornado.options.options.log_rotate_mode = "time"
tornado.options.options.log_rotate_when = "D" # 时间单位 "other options:('S', 'M', 'H', 'D', 'W0'-'W6')"
tornado.options.options.log_rotate_interval = 5 # 间隔
tornado.options.options.log_file_prefix = "%s/log/theLog" % os.path.dirname(os.path.abspath(__file__)) # 文件名
tornado.options.options.log_file_num_backups = 10 # 间隔
tornado.options.options.log_to_stderr = True # 输出到屏幕