Sanic沒有像Flask那麼優秀的集成了logging。可是項目中日誌功能又必不可少。因此就來學習下python
from sanic import Sanic from sanic.config import LOGGING # The default logging handlers are ['accessStream', 'errorStream'] # but we change it to use other handlers here for demo purpose LOGGING['loggers']['network']['handlers'] = [ 'accessSysLog', 'errorSysLog'] app = Sanic('test') @app.route('/') async def test(request): return response.text('Hello World!') if __name__ == "__main__": app.run(log_config=LOGGING) ``` ### 能夠看出,sanic有關於對logging的配置。可是若是像Flask中直接使用的話,還須要改造一下。 ```python from sanic.response import json from sanic import Sanic from sanic.config import LOGGING import logging class sanic(Sanic): def __init__(self,*args,**kwargs): super(sanic,self).__init__(*args,**kwargs) self.web_app = Sanic(__name__) self.web_app.config.LOGO = None self.web_app.log_config = LOGGING self.web_app.logger = self.logger() def logger(self): return logging.getLogger('sanic') def create_app(self): return self.web_app @app.route('/') async def get(request): current_app = request.app current_app.logger.debug('111111111111111111111111111111111111111111111111111111111111111111') return json({1: 2}) if __name__ == '__main__': app = sanic().create_app() app.run(debug=True) ``` #### 繼承一個sanic的類,而後給他一個logging的屬性。這樣就能夠像FLask同樣的使用了