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