Вопрос

(Rewritten slightly following code modifications at my end plus answers given so far. Thanks to Andrew for the sample code which gave me the starting point for demonstrating that custom error logging works and then how I can break it!)

I'm trying to get custom logging working in CherryPy. I also would like to have log file rotation so I've followed the instructions in the documentation to replace the log handlers.

script_dir is set earlier on in my code to the directory where the script is running from.

cherrypy.config.update({'server.socket_host': '0.0.0.0',
                        'server.socket_port': 1234,
                        'tools.staticdir.on': True,
                        'tools.staticdir.dir': script_dir,
                        'log.access_file': "access1.log",
                        'log.error_file': "error1.log",
                        'log.screen': False,
                        'tools.sessions.on': True,
                       })

config = {'/':
             {
                  'tools.staticdir.on': True,
                  'tools.staticdir.dir': script_dir,
                  'log.access_file': "access2.log",
                  'log.error_file': "error2.log",
                  'log.screen': False,
                  'tools.sessions.on': True,
             }
         }

application = cherrypy.tree.mount(MyApp(), "/", config)

log = application.log

# Make a new RotatingFileHandler for the error log.
fname = getattr(log, "rot_error_file", "error.log")
h = handlers.TimedRotatingFileHandler(fname, when='midnight')
h.setLevel(logging.DEBUG)
h.setFormatter(_cplogging.logfmt)
log.error_file = ""
log.error_log.addHandler(h)

# Make a new RotatingFileHandler for the access log.
fname = getattr(log, "rot_access_file", "access.log")
h = handlers.TimedRotatingFileHandler(fname, when='midnight')
h.setLevel(logging.DEBUG)
h.setFormatter(_cplogging.logfmt)
log.access_file = ""
log.access_log.addHandler(h)

With the script running, logging happens as follows:

  • Standard access logging goes to BOTH access1.log (defined at the global level) and access.log (defined at the app level)
  • Error logging only goes to error1.log (defined at the global level)
  • Nothing gets logged to *2.log (as expected)

So, it looks like CherryPy has a problem with error logging going to app-specific config. Generally that wouldn't worry me except that I do want to use the rotating log file handler but I don't know how to modify the logging sessions at the global level as I have done for the app-specific level.

Thanks.

Это было полезно?

Решение

To answer my question about how to set up the log handlers for the global log instead of the app-level log, here are the changes:

cherrypy.config.update({'server.socket_host': '0.0.0.0',
                        'server.socket_port': 1234,
                        'tools.staticdir.on': True,
                        'tools.staticdir.dir': script_dir,
                        'log.access_file': "access1.log",
                        'log.error_file': "error1.log",
                        'log.screen': True,
                        'tools.sessions.on': True,
                       })

config = {'/':
             {
             }
         }

application = cherrypy.tree.mount(HealthCheck(script_dir, service_fqdn, my_ip), "/", config)

logscope = cherrypy.log

# Make a new RotatingFileHandler for the error log.
fname = getattr(logscope, "rot_error_file", "error.log")
h = handlers.TimedRotatingFileHandler(fname, when='midnight')
h.setLevel(logging.DEBUG)
h.setFormatter(_cplogging.logfmt)
logscope.error_file = ""
logscope.error_log.addHandler(h)

# Make a new RotatingFileHandler for the access log.
fname = getattr(logscope, "rot_access_file", "access.log")
h = handlers.TimedRotatingFileHandler(fname, when='midnight')
h.setLevel(logging.DEBUG)
h.setFormatter(_cplogging.logfmt)
logscope.access_file = ""
logscope.access_log.addHandler(h)

or, in a nutshell:

  • leave the application config empty. I'm only defining it so that CherryPy starts quietly.
  • change the log scope from application.log to cherrypy.log

To be slightly more tidy, the references to access1.log and error1.log can change to access.log and error.log but I left them like that to confirm that I was overwriting the global settings.

Другие советы

Use something like this...

import cherrypy
from cherrypy import log

class MyApp(object):
    def index(self):
        log.error(msg='This is My Error ', context='HTTP', severity=20, traceback=True)
        return "Hello World!"    
    index.exposed = True


cherrypy.tree.mount(MyApp(), "/")

cherrypy.config.update({'tools.staticdir.on': True,
    'tools.staticdir.dir': 'C:\\Documents and Settings\\d\\My Documents\\Aptana Studio 3 Workspace\\ScratchPad',
    'log.access_file' : "access.log",
    'log.error_file' : "error.log",
    'log.screen' : False,
    'tools.sessions.on': True,
    })


cherrypy.engine.start()
cherrypy.engine.block()

This will log the error. I believe there's an issue loading the config in your code. I also think static dirs needs to be turned.

Hope this helps and Happy Coding!

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top