annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr//bin/env python2
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2016 Jérôme Poisson (goffi@goffi.org)
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 CONF_KIVY_LEVEL = 'log_kivy_level'
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
21
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 def set_logging():
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from constants import Const as C
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from sat.core import log_config
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 log_config.satConfigure(C.LOG_BACKEND_STANDARD, C)
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
26
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 import config
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 kivy_level = config.getConfig(C.CONFIG_SECTION, CONF_KIVY_LEVEL, 'follow').upper()
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
29
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 # kivy handles its own loggers, we don't want that!
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 import logging
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 root_logger = logging.root
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 kivy_logger = logging.getLogger('kivy')
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 ori_addHandler = kivy_logger.addHandler
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 kivy_logger.addHandler = lambda dummy: None
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 ori_setLevel = kivy_logger.setLevel
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 if kivy_level == 'FOLLOW':
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 # level is following SàT level
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 kivy_logger.setLevel = lambda level: None
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 elif kivy_level == 'KIVY':
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 # level will be set by Kivy according to its own conf
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 pass
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 elif kivy_level in C.LOG_LEVELS:
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 kivy_logger.setLevel(kivy_level)
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 kivy_logger.setLevel = lambda level: None
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 else:
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 raise ValueError(u"Unknown value for {name}: {value}".format(name=CONF_KIVY_LEVEL, value=kivy_level))
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
48
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 # during import kivy set its logging stuff
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 import kivy
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 kivy # to avoid pyflakes warning
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
52
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 # we want to separate kivy logs from other logs
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 logging.root = root_logger
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
55
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 # we restore original methods
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 kivy_logger.addHandler = ori_addHandler
85649eca9f9b core (logs): integrate Kivy logs with SàT:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 kivy_logger.setLevel = ori_setLevel