1 # Copyright (c) 2011 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.
5 ''' Utility functions and objects for logging.
11 class StdoutStderrHandler(logging
.Handler
):
12 ''' Subclass of logging.Handler which outputs to either stdout or stderr
13 based on a threshold level.
16 def __init__(self
, threshold
=logging
.WARNING
, err
=sys
.stderr
, out
=sys
.stdout
):
18 threshold: below this logging level messages are sent to stdout,
19 otherwise they are sent to stderr
20 err: a stream object that error messages are sent to, defaults to
22 out: a stream object that non-error messages are sent to, defaults to
25 logging
.Handler
.__init
__(self
)
26 self
._err
= logging
.StreamHandler(err
)
27 self
._out
= logging
.StreamHandler(out
)
28 self
._threshold
= threshold
29 self
._last
_was
_err
= False
31 def setLevel(self
, lvl
):
32 logging
.Handler
.setLevel(self
, lvl
)
33 self
._err
.setLevel(lvl
)
34 self
._out
.setLevel(lvl
)
36 def setFormatter(self
, formatter
):
37 logging
.Handler
.setFormatter(self
, formatter
)
38 self
._err
.setFormatter(formatter
)
39 self
._out
.setFormatter(formatter
)
41 def emit(self
, record
):
42 if record
.levelno
< self
._threshold
:
43 self
._out
.emit(record
)
44 self
._last
_was
_err
= False
46 self
._err
.emit(record
)
47 self
._last
_was
_err
= False
50 # preserve order on the flushing, the stalest stream gets flushed first
51 if self
._last
_was
_err
:
59 FORMAT
= "%(asctime)s %(filename)s [%(levelname)s] %(message)s"
62 def config_root(level
=logging
.INFO
, threshold
=logging
.WARNING
, format
=FORMAT
,
64 ''' Configure the root logger to use a StdoutStderrHandler and some default
67 level: messages below this level are ignored
68 threshold: below this logging level messages are sent to stdout,
69 otherwise they are sent to stderr
70 format: format for log messages, see logger.Format
71 datefmt: format for date in log messages
74 # to set the handler of the root logging object, we need to do setup
75 # manually rather than using basicConfig
76 root
= logging
.getLogger()
78 formatter
= logging
.Formatter(format
, datefmt
)
79 handler
= StdoutStderrHandler(threshold
=threshold
)
80 handler
.setLevel(level
)
81 handler
.setFormatter(formatter
)
82 root
.addHandler(handler
)