]> git.ipfire.org Git - people/stevee/pakfire.git/blame - src/pakfire/logger.py
Use autotools.
[people/stevee/pakfire.git] / src / pakfire / logger.py
CommitLineData
47a4cb89 1#!/usr/bin/python
b792d887
MT
2###############################################################################
3# #
4# Pakfire - The IPFire package management system #
5# Copyright (C) 2011 Pakfire development team #
6# #
7# This program 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# This program 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 this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
47a4cb89 21
9eac53ba 22import time
47a4cb89 23
8b6bc023 24import logging
a6bd96bc
MT
25import logging.handlers
26
8b6bc023
MT
27log = logging.getLogger("pakfire")
28
60845a36 29def setup_logging(config=None):
47a4cb89
MT
30 """
31 This function initialized the logger that is enabled immediately.
32 """
8b6bc023
MT
33 l = logging.getLogger("pakfire")
34 l.propagate = 0
47a4cb89 35
6174d54a 36 # Remove all previous defined handlers.
47a4cb89
MT
37 for handler in l.handlers:
38 l.removeHandler(handler)
a6bd96bc 39 l.handlers = []
47a4cb89 40
6174d54a
MT
41 # Set level of logger always to DEBUG.
42 l.setLevel(logging.DEBUG)
47a4cb89 43
a6bd96bc 44 # Add output to console (but never dump debugging stuff there).
47a4cb89 45 handler = logging.StreamHandler()
a6bd96bc 46 handler.setLevel(logging.INFO)
47a4cb89
MT
47 l.addHandler(handler)
48
6174d54a 49 # The configuration file always logs all messages.
60845a36 50 if config:
a6bd96bc
MT
51 file = config.get("logger", "file", None)
52 if not file:
53 return
54
55 level = logging.INFO
56 if config.get("logger", "level") == "debug":
57 level = logging.DEBUG
58
59 mode = config.get("logger", "mode", "normal")
60 if mode == "rotate":
61 threshold = config.get("logger", "rotation_threshold", 0)
62 try:
63 threshold = int(threshold)
64 except ValueError:
65 threshold = 0
66
67 handler = logging.handlers.RotatingFileHandler(file,
68 maxBytes=threshold, backupCount=9)
69 else:
70 handler = logging.FileHandler(file)
71
72 handler.setLevel(level)
60845a36 73 l.addHandler(handler)
9eac53ba
MT
74
75
76class BuildFormatter(logging.Formatter):
77 def __init__(self):
78 self._fmt = "[%(asctime)s] %(message)s"
79 self.datefmt = None
80
81 self.starttime = time.time()
82
83 def converter(self, recordtime):
84 """
85 This returns a timestamp relatively to the time when we started
86 the build.
87 """
88 recordtime -= self.starttime
89
90 return time.gmtime(recordtime)
91
92 def formatTime(self, record, datefmt=None):
93 ct = self.converter(record.created)
94 t = time.strftime("%H:%M:%S", ct)
95 s = "%s,%03d" % (t, record.msecs)
96 return s