Commits

Michiel Overtoom committed 20690b4

Custom logging: hostname

  • Participants
  • Parent commits 6d7a6f2

Comments (0)

Files changed (2)

File cherrypy/_cplogging.py

                  'b': dict.get(outheaders, 'Content-Length', '') or "-",
                  'f': dict.get(inheaders, 'Referer', ''),
                  'a': dict.get(inheaders, 'User-Agent', ''),
+                 'o': dict.get(inheaders, 'Host', '-'),
                  }
         if py3k:
             for k, v in atoms.items():

File cherrypy/test/test_logging.py

             self.assertLog(-1, '] "GET %s/as_yield HTTP/1.1" 200 - "" ""'
                            % self.prefix())
 
+    def testCustomLogFormat(self):
+        '''Test a customized access_log_format string,
+           which is a feature of _cplogging.LogManager.access() '''
+
+        original_logformat = cherrypy._cplogging.LogManager.access_log_format
+        cherrypy._cplogging.LogManager.access_log_format = \
+          '{h} {l} {u} {t} "{r}" {s} {b} "{f}" "{a}" {o}' \
+          if py3k else \
+          '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s" %(o)s'
+
+        self.markLog()
+        self.getPage("/as_string", headers=[('Referer', 'REFERER'),
+                                            ('User-Agent', 'USERAGENT'),
+                                            ('Host', 'HOST')])
+        self.assertLog(-1, '%s - - [' % self.interface())
+        self.assertLog(-1, '] "GET /as_string HTTP/1.1" '
+                           '200 7 "REFERER" "USERAGENT" HOST')
+
+        cherrypy._cplogging.LogManager.access_log_format = original_logformat
+
     def testEscapedOutput(self):
         # Test unicode in access log pieces.
         self.markLog()