-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMainServer.py
More file actions
110 lines (83 loc) · 3.26 KB
/
MainServer.py
File metadata and controls
110 lines (83 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# -*- coding: utf-8 -*-
# @Time : 2018/6/7 上午10:57
# @Author : waitWalker
# @Email : waitwalker@163.com
# @File : MainServer.py
# @Software: PyCharm
import tornado.web,tornado.options,tornado.ioloop,tornado.httpserver
from tornado.options import define,options
from urls import urls
import pymysql
import redis
import tornado.log
import logging
import os
import logging.handlers
# 宏定义
# 端口
define('port', type=int, default=8000, help='listen port')
# console输出格式
class MTTLogFormatter(tornado.log.LogFormatter):
def __init__(self):
super(MTTLogFormatter, self).__init__(
fmt='%(color)s[%(asctime)s %(filename)s:%(funcName)s:%(lineno)d %(levelname)s]%(end_color)s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
# application
class MTTApplication(tornado.web.Application):
def __init__(self, *args, **kwargs):
super(MTTApplication, self).__init__(*args, **kwargs)
self.database = pymysql.connect(host='localhost',
user='root',password='etiantian',
db='pet_database',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor
)
# redis 连接池
pool = redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)
self.redis = redis.Redis(connection_pool=pool)
# 入口函数
def main():
# if "log_file_prefix" in options._options:
# # print("log_file_prefix have define")
# else:
# tornado.options.define('log_file_prefix', default=get_logger_file_path())
# # print("options._options", options._options)
tornado.options.parse_command_line()
app = MTTApplication(
urls.handlers,
)
# [i.setFormatter(MTTLogFormatter()) for i in logging.getLogger().handlers]
# 自定义logger
# set_logger()
http_server = tornado.httpserver.HTTPServer(app, xheaders=True)
http_server.listen(options.port)
tornado.ioloop.IOLoop.current().start()
# 获取文件绝对路径
def get_logger_file_path():
current_path = os.path.abspath(__file__)
father_path = os.path.abspath(os.path.dirname(current_path))
file_path = os.path.join(os.path.abspath(os.path.dirname(current_path) + "/loggers/tornado_main.log"))
# print("current_path:", current_path)
# print("father_path:", father_path)
# print("file_path:", file_path)
return file_path
# 自定义logger
def set_logger():
log_file = get_logger_file_path()
handler = logging.handlers.RotatingFileHandler(filename=log_file, maxBytes=1024 * 1024 * 1024,backupCount=5)
formatter = MTTLogFormatter()
handler.setFormatter(formatter)
# print("handler.baseFilename", handler.baseFilename)
logger = logging.getLogger('tornado_main')
# print("logger.handlers", logger.handlers)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
logger.info('abc')
def define(name, default=None, type=None, help=None, metavar=None,
multiple=False, group=None, callback=None):
if name not in options._options:
return define(name, default, type, help, metavar,
multiple, group, callback)
if __name__ == '__main__':
main()