]> kaliko git repositories - mpd-sima.git/blob - sima/lib/logger.py
b27d4f07f15ca15e689c944a8a80fb83dfcc5468
[mpd-sima.git] / sima / lib / logger.py
1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2009, 2010, 2013, 2014 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
30 LOG_FORMATS = {
31         logging.DEBUG: '[{process}]{filename: >11}:{lineno: <3} {levelname: <7}: {message}',
32         logging.INFO:  '{levelname: <7}: {message}',
33         #logging.DEBUG: '{asctime} {filename}:{lineno}({funcName}) '
34                                  #'{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):
54     """
55     logger:
56         level: in debug, info, warning,…
57         logfile: file to log to
58
59     """
60     name = 'sima'
61     user_log_level = getattr(logging, level.upper())
62     if user_log_level > logging.DEBUG:
63         log_format = LOG_FORMATS.get(logging.INFO)
64     else:
65         log_format = LOG_FORMATS.get(logging.DEBUG)
66     logg = logging.getLogger(name)
67     formatter = logging.Formatter(log_format, DATE_FMT, '{')
68     logg.setLevel(user_log_level)
69     filehdl = False
70     if logg.handlers:
71         for hdl in logg.handlers:
72             hdl.setFormatter(formatter)
73             if isinstance(hdl, logging.FileHandler):
74                 filehdl = True
75             else:
76                 logg.removeHandler(hdl)
77
78     if logfile:
79         if filehdl:
80             logg.handlers = []
81         # Add timestamp for file handler
82         log_format = '{0} {1}'.format('{asctime}', log_format)
83         formatter = logging.Formatter(log_format, DATE_FMT, '{')
84         # create file handler
85         fileh = logging.FileHandler(logfile)
86         #fileh.setLevel(user_log_level)
87         fileh.setFormatter(formatter)
88         logg.addHandler(fileh)
89     else:
90         if filehdl:
91             logg.info('Not changing logging handlers, only updating formatter')
92             return
93         # create console handler with a specified log level (STDOUT)
94         couth = logging.StreamHandler(sys.stdout)
95         #couth.setLevel(user_log_level)
96         couth.addFilter(LevelFilter(logging.WARNING))
97
98         # create console handler with warning log level (STDERR)
99         cerrh = logging.StreamHandler(sys.stderr)
100         #cerrh.setLevel(logging.WARNING)
101         cerrh.setLevel(logging.ERROR)
102
103         # add formatter to the handlers
104         cerrh.setFormatter(formatter)
105         couth.setFormatter(formatter)
106
107         # add the handlers to SIMA_LOGGER
108         logg.addHandler(couth)
109         logg.addHandler(cerrh)
110
111 # VIM MODLINE
112 # vim: ai ts=4 sw=4 sts=4 expandtab