summaryrefslogtreecommitdiffstats
path: root/chrome/common/extensions/docs/server2/custom_logger.py
blob: d651612b00a0aa15bf466896d51a3e5adf8278f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import logging

from environment import IsAppEngine


class CustomLogger(object):
  '''Wraps logging methods to include a prefix and flush immediately.
  The flushing is important because logging is often done from jobs
  which may time out, thus losing unflushed logs.
  '''
  def __init__(self, prefix):
    self._prefix = prefix

  def info(self, msg, *args):    self._log(logging.info, msg, args)
  def warning(self, msg, *args): self._log(logging.warning, msg, args)
  def error(self, msg, *args):   self._log(logging.error, msg, args)

  def _log(self, logfn, msg, args):
    try:
      logfn('%s: %s' % (self._prefix, msg), *args)
    finally:
      self.flush()

  if IsAppEngine():
    from google.appengine.api.logservice import logservice
    def flush(self):
      logservice.flush()
  else:
    def flush(self):
      pass