]> kaliko git repositories - mpd-sima.git/blob - sima/lib/logger.py
Add SimaDB and deal with history
[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 import logging
27 import sys
28
29 LOG_FORMATS = {
30         logging.DEBUG: '{asctime} {filename}:{lineno}({funcName}) '
31                                  '{levelname}: {message}',
32         logging.INFO:  '{asctime} {levelname}: {message}'
33         }
34 DATE_FMT = "%Y-%m-%d %H:%M:%S"
35
36
37 class LevelFilter(logging.Filter):# Logging facility
38     """
39     Enable logging between two log level by filtering everything < level.
40     """
41
42     def __init__(self, filt_level):
43         logging.Filter.__init__(self)
44         self.level = filt_level
45
46     def filter(self, record):
47         """Defines loglevel"""
48         return record.levelno <= self.level
49
50
51 def set_logger(level='info', logfile=None, name='sima'):
52     """
53     logger:
54         level: in debug, info, warning,…
55         file: provides to log to file
56
57     """
58     user_log_level = getattr(logging, level.upper())
59     if user_log_level > logging.DEBUG:
60         log_format = LOG_FORMATS.get(logging.INFO)
61     else:
62         log_format = LOG_FORMATS.get(logging.DEBUG)
63     logg = logging.getLogger(name)
64     formatter = logging.Formatter(log_format, DATE_FMT, '{')
65     logg.setLevel(user_log_level)
66     if logfile:
67         # create file handler
68         fileh = logging.FileHandler(logfile)
69         #fileh.setLevel(user_log_level)
70         fileh.setFormatter(formatter)
71         logg.addHandler(fileh)
72     else:
73         # create console handler with a specified log level (STDOUT)
74         couth = logging.StreamHandler(sys.stdout)
75         #couth.setLevel(user_log_level)
76         couth.addFilter(LevelFilter(logging.WARNING))
77
78         # create console handler with warning log level (STDERR)
79         cerrh = logging.StreamHandler(sys.stderr)
80         #cerrh.setLevel(logging.WARNING)
81         cerrh.setLevel(logging.ERROR)
82
83         # add formatter to the handlers
84         cerrh.setFormatter(formatter)
85         couth.setFormatter(formatter)
86
87         # add the handlers to SIMA_LOGGER
88         logg.addHandler(couth)
89         logg.addHandler(cerrh)
90
91 # VIM MODLINE
92 # vim: ai ts=4 sw=4 sts=4 expandtab