comparison src/logging_setter.py @ 6:85649eca9f9b

core (logs): integrate Kivy logs with SàT: - forbid kivy to handle its own logs - added log_kivy_level option which can be put in sat.conf in [cagou] section. This option can have the following values: - follow [default]: follow SàT cagou level - kivy: follow level set in kivy own configuration - [log_level]: set this log level only for kivy messages
author Goffi <goffi@goffi.org>
date Thu, 07 Jul 2016 09:39:21 +0200
parents
children 5ebe1592a05a
comparison
equal deleted inserted replaced
5:33b619506832 6:85649eca9f9b
1 #!/usr//bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client
5 # Copyright (C) 2016 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero 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 Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 CONF_KIVY_LEVEL = 'log_kivy_level'
21
22 def set_logging():
23 from constants import Const as C
24 from sat.core import log_config
25 log_config.satConfigure(C.LOG_BACKEND_STANDARD, C)
26
27 import config
28 kivy_level = config.getConfig(C.CONFIG_SECTION, CONF_KIVY_LEVEL, 'follow').upper()
29
30 # kivy handles its own loggers, we don't want that!
31 import logging
32 root_logger = logging.root
33 kivy_logger = logging.getLogger('kivy')
34 ori_addHandler = kivy_logger.addHandler
35 kivy_logger.addHandler = lambda dummy: None
36 ori_setLevel = kivy_logger.setLevel
37 if kivy_level == 'FOLLOW':
38 # level is following SàT level
39 kivy_logger.setLevel = lambda level: None
40 elif kivy_level == 'KIVY':
41 # level will be set by Kivy according to its own conf
42 pass
43 elif kivy_level in C.LOG_LEVELS:
44 kivy_logger.setLevel(kivy_level)
45 kivy_logger.setLevel = lambda level: None
46 else:
47 raise ValueError(u"Unknown value for {name}: {value}".format(name=CONF_KIVY_LEVEL, value=kivy_level))
48
49 # during import kivy set its logging stuff
50 import kivy
51 kivy # to avoid pyflakes warning
52
53 # we want to separate kivy logs from other logs
54 logging.root = root_logger
55
56 # we restore original methods
57 kivy_logger.addHandler = ori_addHandler
58 kivy_logger.setLevel = ori_setLevel