zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    HTML基础(一)基本语法知识
    本地方法接口
    本地方法栈
    虚拟机栈相关的问题
    栈帧的内部结构--一些附加信息
    基于角色的权限控制设计
    SpringBoot普通消息队列线程池配置
    栈帧的内部结构--动态返回地址(Return Address)
    栈帧的内部结构--动态链接 (Dynamic Linking)
    栈帧的内部结构--操作数栈(Opreand Stack)
  • 原文地址:https://www.cnblogs.com/sea-stream/p/14179332.html
Copyright © 2011-2022 走看看