Robert Brewer avatar Robert Brewer committed e2428a6

mod_python: send bus messages to the apache log, plus cherrypy.setup arg now can take a bare module to import (without having to call a ::function).

Comments (0)

Files changed (1)


 Then restart apache2 and access
+import logging
 import StringIO
 import cherrypy
 # ------------------------------ Request-handling
 def setup(req):
+    from mod_python import apache
     # Run any setup function defined by a "PythonOption cherrypy.setup" directive.
     options = req.get_options()
     if 'cherrypy.setup' in options:
-        modname, fname = options['cherrypy.setup'].split('::')
-        mod = __import__(modname, globals(), locals(), [fname])
-        func = getattr(mod, fname)
-        func()
+        atoms = options['cherrypy.setup'].split('::', 1)
+        if len(atoms) == 1:
+            mod = __import__(atoms[0], globals(), locals())
+        else:
+            modname, fname = atoms
+            mod = __import__(modname, globals(), locals(), [fname])
+            func = getattr(mod, fname)
+            func()
     cherrypy.config.update({'log.screen': False,
                             "tools.ignore_headers.on": True,
                             "tools.ignore_headers.headers": ['Range'],
+    cherrypy._console_control_handler.unsubscribe()
+    cherrypy.engine.autoreload.unsubscribe()
+    def _log(msg, level):
+        newlevel = apache.APLOG_ERR
+        if logging.DEBUG >= level:
+            newlevel = apache.APLOG_DEBUG
+        elif logging.INFO >= level:
+            newlevel = apache.APLOG_INFO
+        elif logging.WARNING >= level:
+            newlevel = apache.APLOG_WARNING
+        # On Windows, req.server is required or the msg will vanish. See
+        #
+        # Also, "When server is not specified...LogLevel does not apply..."
+        apache.log_error(msg, newlevel, req.server)
+    cherrypy.engine.subscribe('log', _log)
     def cherrypy_cleanup(data):
-        from mod_python import apache
         # apache.register_cleanup wasn't available until 3.1.4.
     except AttributeError:
         tb = format_exc()
-        cherrypy.log(tb)
+        cherrypy.log(tb, 'MOD_PYTHON', severity=logging.ERROR)
         s, h, b = bare_error()
         send_response(req, s, h, b)
     return apache.OK
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.