title: CherryPy Logging: How do I configure and use the global and application level loggers?
body: <p>I'm having trouble with logging.  I'm running CherryPy 3.2 and I've been reading through the docs <a href="http://www.cherrypy.org/wiki/ConfigAPI" rel="noreferrer">here</a>, but haven't found any examples of how to configure a local log file for output and how to write to it.</p>
  
  <p>Raspberry.py: </p>
  
  <pre><code>
  import socket
  import sys
  import cherrypy
  
  app_roots = {
                  # Sean's laptop dev environment.
                  "mylaptop": "/home/src/local-mydomain.com/py",
  
                  # Hosted dev environment.  
                  "mydomain.com" : "/home/dev/src/py"
              }
  
  
  hostname = socket.gethostname()
  CherryPyLog = cherrypy.tree.mount().log
  
  if hostname not in app_roots:
      CherryPyLog("The following hostname does not have an app_root entry in raspberry.py.  Exiting early.")
      sys.exit()
  
  sys.stdout = sys.stderr
  sys.path.append(app_roots[hostname])
  
  import os
  os.chdir(app_root)
  
  # Setup for raspberry application logging.
  import datetime
  today = datetime.datetime.today()
  log.access_file = "{0}/{1}.raspberry.access.log".format(app_roots[hostname],today.strftime("%Y%m%d-%H%M"))
  log.error_file = "{0}/{1}.raspberry.error.log".format(app_roots[hostname],today.strftime("%Y%m%d-%H%M"))
  
  #Testing logger
  log("{0} -- Logger configured".format(today.strftime("%Y%m%d-%H%M%S")))
  
  import atexit
  cherrypy.config.update({'environment': 'embedded'})
  
  if cherrypy.__version__.startswith('3.0') and cherrypy.engine.state == 0:
      cherrypy.engine.start(blocking = False)
      atexit.register(cherrypy.engine.stop)
  
  from web.controllers.root import RaspberryRequestHandler
  
  application = cherrypy.Application(RaspberryRequestHandler(), script_name = None, config = None)
  </code></pre>
  
  <p>UPDATE:  Here's the code block that I ended up going with.  </p>
  
  <pre><code>
  
  app_roots = {
                  # Sean's laptop dev environment.
                  "mylaptop": "/home/src/local-plottools.com/py",
  
                  # Hosted dev environment.  
                  "myDomain" : "/home/dev/src/py"
              }
  
  import socket
  hostname = socket.gethostname()
  
  import cherrypy
  import sys
  if hostname not in app_roots:
      cherrypy.log("The hostname {0} does not have an app_root entry in {1}.  Exiting early.".format(hostname,__file__))
      sys.exit()
  
  sys.stdout = sys.stderr
  sys.path.append(app_roots[hostname])
  
  import os
  os.chdir(app_roots[hostname])
  
  from web.controllers.root import RaspberryRequestHandler
  
  cherrypy.config.update({
      'log.access_file': "{0}/cherrypy-access.log".format(app_roots[hostname]),
      'log.error_file': "{0}/cherrypy.log".format(app_roots[hostname]),
      "server.thread_pool" : 10
  })
  
  # special case, handling debug sessions when quickstart is needed.
  if __name__ == "__main__":
  
      cherrypy.config.update({
                                  'log.screen': True, 
                                  "server.socket_port": 8000
                              })
      cherrypy.quickstart(RaspberryRequestHandler())
      sys.exit()
  
  # This configuration is needed for running under mod_wsgi.  See here:  http://tools.cherrypy.org/wiki/ModWSGI    
  cherrypy.config.update({'environment': 'embedded'})
  
  applicationLogName = "{0}/raspberry.log".format(app_roots[hostname])
  
  from logging import handlers
  applicationLogFileHandler = handlers.RotatingFileHandler(applicationLogName, 'a', 10000000, 1000)
  
  import logging
  applicationLogFileHandler.setLevel(logging.DEBUG)
  
  from cherrypy import _cplogging
  applicationLogFileHandler.setFormatter(_cplogging.logfmt)
  
  cherrypy.log.error_log.addHandler(applicationLogFileHandler)
  
  application = cherrypy.Application(RaspberryRequestHandler(), None)
  </code></pre>