| 154 | | |
|---|
| | 154 | |
|---|
| | 155 | |
|---|
| | 156 | # ------------------------- WSGI handlers ------------------------- # |
|---|
| | 157 | |
|---|
| | 158 | def _set_wsgi_handler(self, log, enable): |
|---|
| | 159 | h = self._get_builtin_handler(log, "wsgi") |
|---|
| | 160 | if enable: |
|---|
| | 161 | if not h: |
|---|
| | 162 | h = WSGIErrorHandler() |
|---|
| | 163 | h.setLevel(logging.DEBUG) |
|---|
| | 164 | h.setFormatter(logfmt) |
|---|
| | 165 | h._cpbuiltin = "wsgi" |
|---|
| | 166 | log.addHandler(h) |
|---|
| | 167 | elif h: |
|---|
| | 168 | log.handlers.remove(h) |
|---|
| | 169 | |
|---|
| | 170 | def _get_wsgi(self): |
|---|
| | 171 | return bool(self._get_builtin_handler(self.error_log, "wsgi")) |
|---|
| | 172 | |
|---|
| | 173 | def _set_wsgi(self, newvalue): |
|---|
| | 174 | self._set_wsgi_handler(self.error_log, newvalue) |
|---|
| | 175 | wsgi = property(_get_wsgi, _set_wsgi, |
|---|
| | 176 | doc="If True, error messages will be sent to wsgi.errors.") |
|---|
| | 177 | |
|---|
| | 178 | |
|---|
| | 179 | class WSGIErrorHandler(logging.Handler): |
|---|
| | 180 | "A handler class which writes logging records to environ['wsgi.errors']." |
|---|
| | 181 | |
|---|
| | 182 | def flush(self): |
|---|
| | 183 | """Flushes the stream.""" |
|---|
| | 184 | try: |
|---|
| | 185 | stream = cherrypy.request.wsgi_environ.get('wsgi.errors') |
|---|
| | 186 | except AttributeError, KeyError: |
|---|
| | 187 | pass |
|---|
| | 188 | else: |
|---|
| | 189 | stream.flush() |
|---|
| | 190 | |
|---|
| | 191 | def emit(self, record): |
|---|
| | 192 | """Emit a record.""" |
|---|
| | 193 | try: |
|---|
| | 194 | stream = cherrypy.request.wsgi_environ.get('wsgi.errors') |
|---|
| | 195 | except AttributeError, KeyError: |
|---|
| | 196 | pass |
|---|
| | 197 | else: |
|---|
| | 198 | try: |
|---|
| | 199 | msg = self.format(record) |
|---|
| | 200 | fs = "%s\n" |
|---|
| | 201 | import types |
|---|
| | 202 | if not hasattr(types, "UnicodeType"): #if no unicode support... |
|---|
| | 203 | stream.write(fs % msg) |
|---|
| | 204 | else: |
|---|
| | 205 | try: |
|---|
| | 206 | stream.write(fs % msg) |
|---|
| | 207 | except UnicodeError: |
|---|
| | 208 | stream.write(fs % msg.encode("UTF-8")) |
|---|
| | 209 | self.flush() |
|---|
| | 210 | except: |
|---|
| | 211 | self.handleError(record) |
|---|