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 3 # found in the LICENSE file. 4 5 import logging 6 7 from appengine_wrappers import logservice 8 9 10 class CustomLogger(object): 11 '''Wraps logging methods to include a prefix and flush immediately. 12 The flushing is important because logging is often done from jobs 13 which may time out, thus losing unflushed logs. 14 ''' 15 def __init__(self, prefix): 16 self._prefix = prefix 17 18 def info(self, msg, *args): self._log(logging.info, 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) 21 22 def _log(self, logfn, msg, args): 23 try: 24 logfn('%s: %s' % (self._prefix, msg), *args) 25 finally: 26 logservice.flush() 27