1. 程式人生 > 實用技巧 >cherrypy 列印日誌

cherrypy 列印日誌

code

cherrypy.log("hello there")

test.py

import logging
import logging.config


import cherrypy


logger = logging.getLogger()
db_logger = logging.getLogger('db')


LOG_CONF = {
    'version': 1,


    'formatters': {
        'void': {
            'format': ''
        },
        'standard': {
            
'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s' }, }, 'handlers': { 'default': { 'level':'INFO', 'class':'logging.StreamHandler', 'formatter': 'standard', 'stream': 'ext://sys.stdout' }, 'cherrypy_console
': { 'level':'INFO', 'class':'logging.StreamHandler', 'formatter': 'void', 'stream': 'ext://sys.stdout' }, 'cherrypy_access': { 'level':'INFO', 'class': 'logging.handlers.RotatingFileHandler', 'formatter
': 'void', 'filename': 'access.log', 'maxBytes': 10485760, 'backupCount': 20, 'encoding': 'utf8' }, 'cherrypy_error': { 'level':'INFO', 'class': 'logging.handlers.RotatingFileHandler', 'formatter': 'void', 'filename': 'errors.log', 'maxBytes': 10485760, 'backupCount': 20, 'encoding': 'utf8' }, }, 'loggers': { '': { 'handlers': ['default'], 'level': 'INFO' }, 'db': { 'handlers': ['default'], 'level': 'INFO' , 'propagate': False }, 'cherrypy.access': { 'handlers': ['cherrypy_access'], 'level': 'INFO', 'propagate': False }, 'cherrypy.error': { 'handlers': ['cherrypy_console', 'cherrypy_error'], 'level': 'INFO', 'propagate': False }, } } class Root(object): @cherrypy.expose def index(self): logger.info("boom") db_logger.info("bam") cherrypy.log("bang") return "hello world" if __name__ == '__main__': cherrypy.config.update({'log.screen': False, 'log.access_file': '', 'log.error_file': ''}) cherrypy.engine.unsubscribe('graceful', cherrypy.log.reopen_files) logging.config.dictConfig(LOG_CONF) cherrypy.quickstart(Root())

output

macname@MacdeMacBook-Pro cherry % python3 test.py
[21/Jan/2020:01:04:15] ENGINE Listening for SIGTERM.
[21/Jan/2020:01:04:15] ENGINE Listening for SIGHUP.
[21/Jan/2020:01:04:15] ENGINE Listening for SIGUSR1.
[21/Jan/2020:01:04:15] ENGINE Bus STARTING
CherryPy Checker:
The Application mounted at '' has an empty config.

[21/Jan/2020:01:04:15] ENGINE Started monitor thread 'Autoreloader'.
[21/Jan/2020:01:04:15] ENGINE Serving on http://127.0.0.1:8080
[21/Jan/2020:01:04:15] ENGINE Bus STARTED
2020-01-21 01:05:28,432 [INFO] root: boom
2020-01-21 01:05:28,432 [INFO] db: bam
[21/Jan/2020:01:05:28]  bang