Changeset 1156
- Timestamp:
- 06/25/06 01:02:33
- Files:
-
- trunk/cherrypy/__init__.py (modified) (4 diffs)
- trunk/cherrypy/_cpmodpy.py (modified) (1 diff)
- trunk/cherrypy/_cptree.py (modified) (4 diffs)
- trunk/cherrypy/config.py (modified) (3 diffs)
- trunk/cherrypy/test/test_core.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/__init__.py
r1155 r1156 92 92 return '%02d/%s/%04d:%02d:%02d:%02d' % ( 93 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)106 94 107 95 def log_access(): … … 118 106 'a': request.headers.get('user-agent', ''), 119 107 } 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) 108 try: 109 request.app.access_log.log(logging.INFO, s) 110 except: 111 log(traceback=True) 130 112 131 113 … … 133 115 _error_log.setLevel(logging.DEBUG) 134 116 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 117 def _log_message(msg, context = '', severity = logging.DEBUG): 143 118 """Default method for logging messages (error log). … … 147 122 """ 148 123 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))) 124 try: 125 log = request.app.error_log 126 except AttributeError: 127 log = _error_log 128 log.log(severity, ' '.join((logtime(), context, msg))) 158 129 159 130 def log(msg='', context='', severity=logging.DEBUG, traceback=False): trunk/cherrypy/_cpmodpy.py
r1151 r1156 14 14 func() 15 15 16 cherrypy.config.update({'global' : {' server.log_to_screen' : False}})16 cherrypy.config.update({'global' : {'log_to_screen' : False}}) 17 17 18 18 if cherrypy.engine.state == cherrypy._cpengine.STOPPED: trunk/cherrypy/_cptree.py
r1141 r1156 1 import logging 2 import sys 1 3 2 4 from cherrypy import config … … 4 6 5 7 class Application: 8 """A CherryPy Application.""" 6 9 7 10 def __init__(self, root, script_name="", conf=None): 11 self.access_log = log = logging.getLogger("cherrypy.access.%s" % id(self)) 12 log.setLevel(logging.INFO) 13 14 self.error_log = log = logging.getLogger("cherrypy.error.%s" % id(self)) 15 log.setLevel(logging.DEBUG) 16 8 17 self.root = root 9 18 self.script_name = script_name … … 13 22 14 23 def merge(self, conf): 24 """Merge the given config into self.config.""" 15 25 config.merge(self.conf, conf) 26 27 # Create log handlers as specified in config. 28 rootconf = self.conf.get("/", {}) 29 config._configure_builtin_logging(rootconf, self.access_log, "log_access_file") 30 config._configure_builtin_logging(rootconf, self.error_log) 16 31 17 32 def guess_abs_path(self): … … 36 51 37 52 class Tree: 38 """A registry of mounted applicationsat diverse points."""53 """A registry of CherryPy applications, mounted at diverse points.""" 39 54 40 55 def __init__(self): trunk/cherrypy/config.py
r1146 r1156 2 2 3 3 import ConfigParser 4 import logging 5 _logfmt = logging.Formatter("%(message)s") 4 6 import os 7 import sys 5 8 6 9 import cherrypy … … 65 68 def reset(): 66 69 globalconf.clear() 67 globalconf.update(default_conf)70 update(default_conf) 68 71 69 72 def update(conf): … … 78 81 conf = conf["global"] 79 82 globalconf.update(conf) 80 83 84 _configure_builtin_logging(globalconf, cherrypy._error_log) 85 86 def _add_builtin_screen_handler(log): 87 h = logging.StreamHandler(sys.stdout) 88 h.setLevel(logging.DEBUG) 89 h.setFormatter(_logfmt) 90 h._cpbuiltin = "screen" 91 log.addHandler(h) 92 93 def _add_builtin_file_handler(log, fname): 94 h = logging.FileHandler(fname) 95 h.setLevel(logging.DEBUG) 96 h.setFormatter(_logfmt) 97 h._cpbuiltin = "file" 98 log.addHandler(h) 99 100 def _configure_builtin_logging(conf, log, filekey="log_file"): 101 """Create/destroy builtin log handlers as needed from conf.""" 102 103 existing = dict([(getattr(x, "_cpbuiltin", None), x) 104 for x in log.handlers]) 105 h = existing.get("screen") 106 screen = conf.get('log_to_screen') 107 if screen: 108 if not h: 109 _add_builtin_screen_handler(log) 110 elif h: 111 log.handlers.remove(h) 112 113 h = existing.get("file") 114 fname = conf.get(filekey) 115 if fname: 116 if h: 117 if h.baseFilename != os.path.abspath(fname): 118 h.close() 119 log.handlers.remove(h) 120 _add_builtin_file_handler(log, fname) 121 else: 122 _add_builtin_file_handler(log, fname) 123 else: 124 if h: 125 h.close() 126 log.handlers.remove(h) 81 127 82 128 def get(key, default=None): trunk/cherrypy/test/test_core.py
r1155 r1156 361 361 cherrypy.config.update({ 362 362 'log_to_screen': False, 363 'log_ access_file': log_access_file,363 'log_file': log_file, 364 364 'server.protocol_version': "HTTP/1.1", 365 365 'environment': 'production', … … 368 368 'server.max_request_header_size': 500, 369 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 375 cherrypy.tree.mount(root) 370 cherrypy.tree.mount(root, conf={'/': {'log_access_file': log_access_file}}) 376 371 377 372

