OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import logging | 5 import logging |
6 | 6 |
7 from appengine_wrappers import logservice | 7 from environment import IsAppEngine |
8 | 8 |
9 | 9 |
10 class CustomLogger(object): | 10 class CustomLogger(object): |
11 '''Wraps logging methods to include a prefix and flush immediately. | 11 '''Wraps logging methods to include a prefix and flush immediately. |
12 The flushing is important because logging is often done from jobs | 12 The flushing is important because logging is often done from jobs |
13 which may time out, thus losing unflushed logs. | 13 which may time out, thus losing unflushed logs. |
14 ''' | 14 ''' |
15 def __init__(self, prefix): | 15 def __init__(self, prefix): |
16 self._prefix = prefix | 16 self._prefix = prefix |
17 | 17 |
18 def info(self, msg, *args): self._log(logging.info, msg, args) | 18 def info(self, msg, *args): self._log(logging.info, msg, args) |
19 def warning(self, msg, *args): self._log(logging.warning, msg, args) | 19 def warning(self, msg, *args): self._log(logging.warning, msg, args) |
20 def error(self, msg, *args): self._log(logging.error, msg, args) | 20 def error(self, msg, *args): self._log(logging.error, msg, args) |
21 | 21 |
22 def _log(self, logfn, msg, args): | 22 def _log(self, logfn, msg, args): |
23 try: | 23 try: |
24 logfn('%s: %s' % (self._prefix, msg), *args) | 24 logfn('%s: %s' % (self._prefix, msg), *args) |
25 finally: | 25 finally: |
| 26 self.flush() |
| 27 |
| 28 if IsAppEngine(): |
| 29 from google.appengine.api.logservice import logservice |
| 30 def flush(self): |
26 logservice.flush() | 31 logservice.flush() |
| 32 else: |
| 33 def flush(self): |
| 34 pass |
OLD | NEW |