python: use python3 style super statements
[samba.git] / python / samba / logger.py
bloba35ef2a358f69d61cba62706fe01717be84198d0
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
22 from samba.colour import is_colour_wanted
24 LEVEL_COLORS = {
25 logging.CRITICAL: DARK_RED,
26 logging.ERROR: RED,
27 logging.WARNING: YELLOW,
28 logging.INFO: GREEN,
29 logging.DEBUG: GREY,
33 class ColoredFormatter(logging.Formatter):
34 """Add color to log according to level"""
36 def format(self, record):
37 log = super().format(record)
38 color = LEVEL_COLORS.get(record.levelno, GREY)
39 return color + log + C_NORMAL
42 def get_samba_logger(
43 name='samba', stream=sys.stderr,
44 level=None, verbose=False, quiet=False,
45 fmt=('%(levelname)s %(asctime)s pid:%(process)d '
46 '%(pathname)s #%(lineno)d: %(message)s'),
47 datefmt=None):
48 """
49 Get a logger instance and config it.
50 """
51 logger = logging.getLogger(name)
53 if not level:
54 # if level not specified, map options to level
55 level = ((verbose and logging.DEBUG) or
56 (quiet and logging.WARNING) or logging.INFO)
58 logger.setLevel(level)
59 if is_colour_wanted(stream):
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