日志保存到文件

This commit is contained in:
John Smith 2020-09-03 20:01:54 +08:00
parent 4d58245ad9
commit ca55f9917d
2 changed files with 11 additions and 3 deletions

0
log/.gitkeep Normal file
View File

14
main.py
View File

@ -2,6 +2,7 @@
import argparse import argparse
import logging import logging
import logging.handlers
import os import os
import webbrowser import webbrowser
@ -18,7 +19,9 @@ import update
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
WEB_ROOT = os.path.join(os.path.dirname(__file__), 'frontend', 'dist') BASE_PATH = os.path.dirname(os.path.realpath(__file__))
WEB_ROOT = os.path.join(BASE_PATH, 'frontend', 'dist')
LOG_FILE_NAME = os.path.join(BASE_PATH, 'log', 'blivechat.log')
routes = [ routes = [
(r'/api/server_info', api.main.ServerInfoHandler), (r'/api/server_info', api.main.ServerInfoHandler),
@ -47,7 +50,7 @@ def main():
def parse_args(): def parse_args():
parser = argparse.ArgumentParser(description='用于OBS的仿YouTube风格的bilibili直播聊天层') parser = argparse.ArgumentParser(description='用于OBS的仿YouTube风格的bilibili直播评论栏')
parser.add_argument('--host', help='服务器host默认为127.0.0.1', default='127.0.0.1') parser.add_argument('--host', help='服务器host默认为127.0.0.1', default='127.0.0.1')
parser.add_argument('--port', help='服务器端口默认为12450', type=int, default=12450) parser.add_argument('--port', help='服务器端口默认为12450', type=int, default=12450)
parser.add_argument('--debug', help='调试模式', action='store_true') parser.add_argument('--debug', help='调试模式', action='store_true')
@ -55,11 +58,16 @@ def parse_args():
def init_logging(debug): def init_logging(debug):
stream_handler = logging.StreamHandler()
file_handler = logging.handlers.TimedRotatingFileHandler(
LOG_FILE_NAME, encoding='utf-8', when='midnight', backupCount=7, delay=True
)
logging.basicConfig( logging.basicConfig(
format='{asctime} {levelname} [{name}]: {message}', format='{asctime} {levelname} [{name}]: {message}',
datefmt='%Y-%m-%d %H:%M:%S', datefmt='%Y-%m-%d %H:%M:%S',
style='{', style='{',
level=logging.INFO if not debug else logging.DEBUG level=logging.INFO if not debug else logging.DEBUG,
handlers=[stream_handler, file_handler]
) )