s4-rpc_server/drsuapi: Improve debugging of invalid DNs
[Samba.git] / python / samba / logger.py
blob667c6487a51a9acec883ef182611e56886025a7a
1 # Samba common functions
3 # Copyright (C) Joe Guo <joeg@catalyst.net.nz>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 import sys
20 import logging
21 from samba.colour import GREY, YELLOW, GREEN, RED, DARK_RED, C_NORMAL
23 LEVEL_COLORS = {
24 logging.CRITICAL: DARK_RED,
25 logging.ERROR: RED,
26 logging.WARNING: YELLOW,
27 logging.INFO: GREEN,
28 logging.DEBUG: GREY,
32 class ColoredFormatter(logging.Formatter):
33 """Add color to log according to level"""
35 def format(self, record):
36 log = super(ColoredFormatter, self).format(record)
37 color = LEVEL_COLORS.get(record.levelno, GREY)
38 return color + log + C_NORMAL
41 def get_samba_logger(
42 name='samba', stream=sys.stderr,
43 level=None, verbose=False, quiet=False,
44 fmt=('%(levelname)s %(asctime)s pid:%(process)d '
45 '%(pathname)s #%(lineno)d: %(message)s'),
46 datefmt=None):
47 """
48 Get a logger instance and config it.
49 """
50 logger = logging.getLogger(name)
52 if not level:
53 # if level not specified, map options to level
54 level = ((verbose and logging.DEBUG) or
55 (quiet and logging.WARNING) or logging.INFO)
57 logger.setLevel(level)
59 if (hasattr(stream, 'isatty') and stream.isatty()):
60 Formatter = ColoredFormatter
61 else:
62 Formatter = logging.Formatter
63 formatter = Formatter(fmt=fmt, datefmt=datefmt)
65 handler = logging.StreamHandler(stream=stream)
66 handler.setFormatter(formatter)
67 logger.addHandler(handler)
69 return logger