Changeset 1155
- Timestamp:
- 06/22/06 18:38:28
- Files:
-
- trunk/cherrypy/__init__.py (modified) (4 diffs)
- trunk/cherrypy/test/test_core.py (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/__init__.py
r1154 r1155 3 3 __version__ = '3.0.0alpha' 4 4 5 import logging 5 6 6 7 from _cperror import HTTPError, HTTPRedirect, InternalRedirect, NotFound, WrongConfigValue … … 91 92 return '%02d/%s/%04d:%02d:%02d:%02d' % ( 92 93 now.day, month, now.year, now.hour, now.minute, now.second) 94 95 _logfmt = logging.Formatter("%(message)s") 96 97 _access_log = logging.getLogger("cherrypy.access") 98 _access_log.setLevel(logging.INFO) 99 100 def _add_access_log_handler(handler): 101 if handler.level == logging.NOTSET: 102 handler.setLevel(logging.INFO) 103 if handler.formatter is None: 104 handler.setFormatter(_logfmt) 105 _access_log.addHandler(handler) 93 106 94 107 def log_access(): … … 105 118 'a': request.headers.get('user-agent', ''), 106 119 } 107 108 if config.get('log_to_screen', True): 109 print s 110 111 fname = config.get('log_access_file', '') 112 if fname: 113 f = open(fname, 'ab') 114 try: 115 f.write(s + '\n') 116 finally: 117 f.close() 118 119 _log_severity_levels = {0: "INFO", 1: "WARNING", 2: "ERROR"} 120 121 def _log_message(msg, context = '', severity = 0): 120 121 # Create handlers if needed 122 if not _access_log.handlers: 123 if config.get('server.log_to_screen'): 124 _add_access_log_handler(logging.StreamHandler(sys.stdout)) 125 fname = config.get('log_access_file', '') 126 if fname: 127 _add_access_log_handler(logging.FileHandler(fname)) 128 129 _access_log.log(logging.INFO, s) 130 131 132 _error_log = logging.getLogger("cherrypy.error") 133 _error_log.setLevel(logging.DEBUG) 134 135 def _add_error_log_handler(handler): 136 if handler.level == logging.NOTSET: 137 handler.setLevel(logging.DEBUG) 138 if handler.formatter is None: 139 handler.setFormatter(_logfmt) 140 _error_log.addHandler(handler) 141 142 def _log_message(msg, context = '', severity = logging.DEBUG): 122 143 """Default method for logging messages (error log). 123 144 … … 126 147 """ 127 148 128 level = _log_severity_levels.get(severity, "UNKNOWN") 129 130 s = ' '.join((logtime(), context, level, msg)) 131 132 if config.get('log_to_screen', True): 133 print s 134 135 fname = config.get('log_file', '') 136 #logdir = os.path.dirname(fname) 137 #if logdir and not os.path.exists(logdir): 138 # os.makedirs(logdir) 139 if fname: 140 f = open(fname, 'ab') 141 try: 142 f.write(s + '\n') 143 finally: 144 f.close() 145 146 def log(msg='', context='', severity=0, traceback=False): 149 # Create handlers if needed 150 if not _error_log.handlers: 151 if config.get('server.log_to_screen'): 152 _add_error_log_handler(logging.StreamHandler(sys.stdout)) 153 fname = config.get('log_file', '') 154 if fname: 155 _add_error_log_handler(logging.FileHandler(fname)) 156 157 _error_log.log(severity, ' '.join((logtime(), context, msg))) 158 159 def log(msg='', context='', severity=logging.DEBUG, traceback=False): 147 160 """Syntactic sugar for writing to the (error) log. 148 161 trunk/cherrypy/test/test_core.py
r1145 r1155 11 11 import os 12 12 localDir = os.path.dirname(__file__) 13 log_file = os.path.join(localDir, " error.log")13 log_file = os.path.join(localDir, "test.log") 14 14 log_access_file = os.path.join(localDir, "access.log") 15 15 favicon_path = os.path.join(os.getcwd(), localDir, "../favicon.ico") … … 68 68 class Params(Test): 69 69 70 _cp_config = {'log_file': log_file}71 72 70 def index(self, thing): 73 71 return repr(thing) … … 182 180 class Flatten(Test): 183 181 184 _cp_config = {'log_file': log_file,185 'log_access_file': log_access_file,186 }187 188 182 def as_string(self): 189 183 return "content" … … 205 199 class Error(Test): 206 200 207 _cp_config = {'log_file': log_file, 208 'tools.log_tracebacks.on': True, 201 _cp_config = {'tools.log_tracebacks.on': True, 209 202 } 210 203 … … 368 361 cherrypy.config.update({ 369 362 'log_to_screen': False, 363 'log_access_file': log_access_file, 370 364 'server.protocol_version': "HTTP/1.1", 371 365 'environment': 'production', … … 374 368 'server.max_request_header_size': 500, 375 369 }) 370 # When run via test.py, the engine is started (and the loggers created) 371 # before the above config.update, so we do it again manually. 372 import logging 373 cherrypy._add_error_log_handler(logging.FileHandler(log_file)) 374 376 375 cherrypy.tree.mount(root) 377 376 … … 437 436 438 437 def testLogging(self): 439 open(log_file, "wb").write("") 440 open(log_access_file, "wb").write("") 438 f = open(log_access_file, "wb") 439 f.write("") 440 f.close() 441 f = open(log_file, "wb") 442 f.write("") 443 f.close() 441 444 442 445 self.getPage("/flatten/as_string") … … 454 457 if k.lower() == 'content-length': 455 458 haslength = True 459 line = data[-2].strip() 456 460 if haslength: 457 self.assert_( data[0].endswith('] "GET %s/flatten/as_string HTTP/1.1" 200 7 "" ""\n'461 self.assert_(line.endswith('] "GET %s/flatten/as_string HTTP/1.1" 200 7 "" ""' 458 462 % self.prefix())) 459 463 else: 460 self.assert_( data[0].endswith('] "GET %s/flatten/as_string HTTP/1.1" 200 - "" ""\n'464 self.assert_(line.endswith('] "GET %s/flatten/as_string HTTP/1.1" 200 - "" ""' 461 465 % self.prefix())) 462 466 463 self.assertEqual(data[ 1][:15], '127.0.0.1 - - [')467 self.assertEqual(data[-1][:15], '127.0.0.1 - - [') 464 468 haslength = False 465 469 for k, v in self.headers: 466 470 if k.lower() == 'content-length': 467 471 haslength = True 472 line = data[-1].strip() 468 473 if haslength: 469 self.assert_( data[1].endswith('] "GET %s/flatten/as_yield HTTP/1.1" 200 7 "" ""\n'474 self.assert_(line.endswith('] "GET %s/flatten/as_yield HTTP/1.1" 200 7 "" ""' 470 475 % self.prefix())) 471 476 else: 472 self.assert_( data[1].endswith('] "GET %s/flatten/as_yield HTTP/1.1" 200 - "" ""\n'477 self.assert_(line.endswith('] "GET %s/flatten/as_yield HTTP/1.1" 200 - "" ""' 473 478 % self.prefix())) 474 475 data = open(log_file, "rb").readlines()476 self.assertEqual(data, [])477 479 478 480 ignore = helper.webtest.ignored_exceptions … … 483 485 self.assertInBody("raise ValueError()") 484 486 data = open(log_file, "rb").readlines() 485 self.assertEqual(data[0] [-41:], ' INFO Traceback (most recent call last):\n')486 self.assertEqual(data[-3] , ' raise ValueError()\n')487 self.assertEqual(data[0].strip().endswith('HTTP Traceback (most recent call last):'), True) 488 self.assertEqual(data[-3].strip().endswith('raise ValueError()'), True) 487 489 finally: 488 490 ignore.pop()

