diff --git a/app.py b/app.py index ffdf53f..c1039f4 100644 --- a/app.py +++ b/app.py @@ -11,9 +11,9 @@ import tornado.ioloop import tornado.options import os from tornado.options import define, options - from libs import db_session, engine # import the engine to bind #from libs.memcache import cache + if os.getenv("ENV")=='dev': from dev_settings import url_handlers, settings else: @@ -36,6 +36,16 @@ application = Application(url_handlers, **settings) if __name__ == "__main__": tornado.options.parse_command_line() + + 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"%os.path.dirname(os.path.abspath(__file__)) # 文件名 + #tornado.options.options.log_file_prefix = "%s/logs/theLog" % os.path.dirname(os.path.abspath(__file__)) # 文件名 + tornado.options.options.log_file_num_backups = 10 # 间隔 + tornado.options.options.log_to_stderr = True # 输出到屏幕 + http_server = tornado.httpserver.HTTPServer(application) http_server.listen(options.port) tornado.ioloop.IOLoop.instance().start() diff --git a/config.py b/config.py index f21ec4b..286737d 100644 --- a/config.py +++ b/config.py @@ -48,12 +48,9 @@ DATABASE = { } } -# TODO: the reids database settings -REDIS = { - -} +REDIS = {'dev': {"host": '192.168.100.30', "port": 6379, "db": 0}, + 'prod': {"host": '192.168.100.30', "port": 6379, "db": 0}} # TODO: the log settings -# TODO: memcahce usage diff --git a/libs/db/database.py b/libs/db/database.py index 8675ff7..9f405e6 100644 --- a/libs/db/database.py +++ b/libs/db/database.py @@ -18,12 +18,10 @@ Solution: http://docs.sqlalchemy.org/en/rel_0_7/core/pooling.html#setting-pool-r """ # engine = create_engine('mysql+pymysql://miles:aspect@192.168.100.30/test', pool_recycle=3600, encoding="utf-8", echo=True) #会乱码 env = os.getenv("ENV") or "dev" -print(f"env={env}") mysql_conn = "{driven}://{user}:{pswd}@{host}/test".format(driven = DATABASE[env]['driven'], user = DATABASE[env]['user'], pswd = DATABASE[env]['password'], host = DATABASE[env]['host']) -print(f"mysql_conn={mysql_conn}") engine = create_engine(mysql_conn, pool_recycle = 60, connect_args = {"charset": "utf8"}, echo = True) Base = declarative_base() diff --git a/logging.yaml b/logging.yaml new file mode 100644 index 0000000..85a2daa --- /dev/null +++ b/logging.yaml @@ -0,0 +1,47 @@ +version: 1 + +loggers: + root: + level: DEBUG + handlers: [console] + tornado: + level: DEBUG + handlers: [console,log] + propagate: no + tornado.access: + level: DEBUG + handlers: [console, access] + propagate: no + log: + level: DEBUG + handlers: [console,log] + propagate: no + +formatters: + simple: + format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s' + timedRotating: + format: '%(asctime)s %(name)-12s %(levelname)-8s - %(message)s' + +handlers: + console: + class: logging.StreamHandler + level: DEBUG + formatter: simple + access: + class: logging.handlers.TimedRotatingFileHandler + level: DEBUG + formatter: simple + filename: 'logs/access.log' + when: 'midnight' + interval: 1 + backupCount: 180 + log: + class: logging.handlers.TimedRotatingFileHandler + level: DEBUG + formatter: timedRotating + filename: 'logs/log.log' + when: 'midnight' + interval: 1 + backupCount: 180 + encoding: 'utf8' \ No newline at end of file diff --git a/settings.py b/settings.py index 8094398..6f6ce57 100644 --- a/settings.py +++ b/settings.py @@ -3,17 +3,29 @@ from os import path from urls import urls_pattern as url_handlers +from tornado.log import access_log +#import logging DEBUG = False -# the application settings -settings = { - 'debug': DEBUG, - 'cookie_secret': 'test', # TODO: get the real secret - 'login_url': '/admin/login', - 'xsrf_cookies': True, - 'static_path': path.join(path.dirname(__file__), 'static_v1'), - 'template_path': path.join(path.dirname(__file__), 'templates'), - #'ui_modules': '' # TODO: the ui modules file -} +def log_func(handler): + 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 %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) + + +# the application settings +settings = {'debug': DEBUG, 'cookie_secret': 'test', # TODO: get the real secret + 'login_url': '/admin/login', 'xsrf_cookies': True, + 'static_path': path.join(path.dirname(__file__), 'static_v1'), + 'template_path': path.join(path.dirname(__file__), 'templates'), "log_function": log_func + # 'ui_modules': '' # TODO: the ui modules file + }