]> kaliko git repositories - mpd-sima.git/blob - sima/lib/logger.py
Fixed logger issue (error msg printed twice)
[mpd-sima.git] / sima / lib / logger.py
1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2009, 2010, 2013, 2014, 2015 Jack Kaliko <kaliko@azylum.org>
3 #
4 #  This file is part of sima
5 #
6 #  sima is free software: you can redistribute it and/or modify
7 #  it under the terms of the GNU General Public License as published by
8 #  the Free Software Foundation, either version 3 of the License, or
9 #  (at your option) any later version.
10 #
11 #  sima is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #  GNU General Public License for more details.
15 #
16 #  You should have received a copy of the GNU General Public License
17 #  along with sima.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 #
20
21 """
22 Logging facility for sima.
23 """
24
25 # standard library import
26 import logging
27 import sys
28
29 DEBUG = logging.DEBUG
30 INFO = logging.INFO
31 ERROR = logging.ERROR
32 LOG_FORMATS = {
33         DEBUG: '[{process}]{filename: >11}:{lineno: <3} {levelname: <7}: {message}',
34         INFO:  '{levelname: <7}: {message}',
35         #logging.DEBUG: '{asctime} {filename}:{lineno}({funcName}) '
36                                  #'{levelname}: {message}',
37         }
38 DATE_FMT = "%Y-%m-%d %H:%M:%S"
39
40
41 class LevelFilter(logging.Filter):
42     """
43     Enable logging between two log level by filtering everything < level.
44     """
45     def filter(self, record):
46         """Defines loglevel"""
47         return record.levelno <= ERROR
48
49
50 def set_logger(level='info', logfile=None):
51     """
52     logger:
53         level: in debug, info, warning,…
54         logfile: file to log to
55
56     """
57     name = 'sima'
58     user_log_level = getattr(logging, level.upper())
59     if user_log_level > DEBUG:
60         log_format = LOG_FORMATS.get(INFO)
61     else:
62         log_format = LOG_FORMATS.get(DEBUG)
63     logger = logging.getLogger(name)
64     formatter = logging.Formatter(log_format, DATE_FMT, '{')
65     logger.setLevel(user_log_level)
66     filehdl = False
67     if logger.handlers:
68         for hdl in logger.handlers:
69             hdl.setFormatter(formatter)
70             if isinstance(hdl, logging.FileHandler):
71                 filehdl = True
72             else:
73                 logger.removeHandler(hdl)
74
75     if logfile:
76         if filehdl:
77             logger.handlers = []
78         # Add timestamp for file handler
79         log_format = '{0} {1}'.format('{asctime}', log_format)
80         formatter = logging.Formatter(log_format, DATE_FMT, '{')
81         # create file handler
82         fileh = logging.FileHandler(logfile)
83         fileh.setFormatter(formatter)
84         logger.addHandler(fileh)
85     else:
86         if filehdl:
87             logger.info('Not changing logging handlers, only updating formatter')
88             return
89         # create console handler with a specified log level (STDOUT)
90         couth = logging.StreamHandler(sys.stdout)
91         couth.addFilter(LevelFilter())
92
93         # create console handler with warning log level (STDERR)
94         cerrh = logging.StreamHandler()
95         cerrh.setLevel(ERROR)
96
97         # add formatter to the handlers
98         cerrh.setFormatter(formatter)
99         couth.setFormatter(formatter)
100
101         # add the handlers to SIMA_LOGGER
102         logger.addHandler(couth)
103         #logger.addHandler(cerrh)  # Already added creating the handler‽ Still have to figure it out.
104
105 # VIM MODLINE
106 # vim: ai ts=4 sw=4 sts=4 expandtab