annotate src/core/log_config.py @ 2159:5734b0994cf0

core (tools/common): template renderer first draft: jinja integration is being rewritten to be used not only in Libervia, this is a first step which put the renderer in backend/frontends tools. The renderer is mainly HTML oriented, but may be used for other files. sat_templates dependency is needed to use it.
author Goffi <goffi@goffi.org>
date Tue, 21 Feb 2017 21:01:39 +0100
parents 0931b5a6213c
children 8b37a62336c3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1934
2daf7b4c6756 use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
1 #!/usr/bin/env python2
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SàT: a XMPP client
1766
d17772b0fe22 copyright update
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """High level logging functions"""
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
21 # XXX: this module use standard logging module when possible, but as SàT can work in different cases where logging is not the best choice (twisted, pyjamas, etc), it is necessary to have a dedicated module. Additional feature like environment variables and colors are also managed.
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
22
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from sat.core.constants import Const as C
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
24 from sat.core import log
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
25
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
26
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
27 class TwistedLogger(log.Logger):
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
28 colors = True
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
29 force_colors = False
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
30
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
31 def __init__(self, *args, **kwargs):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
32 super(TwistedLogger, self).__init__(*args, **kwargs)
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
33 from twisted.python import log as twisted_log
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
34 self.twisted_log = twisted_log
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
35
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
36 def out(self, message, level=None):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
37 """Actually log the message
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
38
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
39 @param message: formatted message
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
40 """
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
41 self.twisted_log.msg(message.encode('utf-8', 'ignore'), sat_logged=True, level=level)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
42
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
43
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
44 class ConfigureBasic(log.ConfigureBase):
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
45
1942
7f053e1f0b67 core (logs): taints:
Goffi <goffi@goffi.org>
parents: 1941
diff changeset
46 def configureColors(self, colors, force_colors, levels_taints_dict):
7f053e1f0b67 core (logs): taints:
Goffi <goffi@goffi.org>
parents: 1941
diff changeset
47 super(ConfigureBasic, self).configureColors(colors, force_colors, levels_taints_dict)
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
48 if colors:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
49 import sys
2089
0931b5a6213c core, quick_frontends: android compatibility hacks:
Goffi <goffi@goffi.org>
parents: 1942
diff changeset
50 try:
0931b5a6213c core, quick_frontends: android compatibility hacks:
Goffi <goffi@goffi.org>
parents: 1942
diff changeset
51 isatty = sys.stdout.isatty()
0931b5a6213c core, quick_frontends: android compatibility hacks:
Goffi <goffi@goffi.org>
parents: 1942
diff changeset
52 except AttributeError:
0931b5a6213c core, quick_frontends: android compatibility hacks:
Goffi <goffi@goffi.org>
parents: 1942
diff changeset
53 isatty = False
0931b5a6213c core, quick_frontends: android compatibility hacks:
Goffi <goffi@goffi.org>
parents: 1942
diff changeset
54 if force_colors or isatty: # FIXME: isatty should be tested on each handler, not globaly
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
55 # we need colors
1942
7f053e1f0b67 core (logs): taints:
Goffi <goffi@goffi.org>
parents: 1941
diff changeset
56 log.Logger.post_treat = lambda logger, level, message: self.ansiColors(level, message)
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
57 elif force_colors:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
58 raise ValueError("force_colors can't be used if colors is False")
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
59
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
60 @staticmethod
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
61 def getProfile():
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
62 """Try to find profile value using introspection"""
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
63 import inspect
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
64 stack = inspect.stack()
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
65 current_path = stack[0][1]
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
66 for frame_data in stack[:-1]:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
67 if frame_data[1] != current_path:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
68 if log.backend == C.LOG_BACKEND_STANDARD and "/logging/__init__.py" in frame_data[1]:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
69 continue
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
70 break
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
71
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
72 frame = frame_data[0]
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
73 args = inspect.getargvalues(frame)
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
74 try:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
75 profile = args.locals.get('profile') or args.locals['profile_key']
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
76 except (TypeError, KeyError):
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
77 try:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
78 try:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
79 profile = args.locals['self'].profile
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
80 except AttributeError:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
81 try:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
82 profile = args.locals['self'].parent.profile
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
83 except AttributeError:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
84 profile = args.locals['self'].host.profile # used in quick_frontend for single profile configuration
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
85 except Exception:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
86 # we can't find profile, we return an empty value
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
87 profile = ''
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
88 return profile
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
89
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
90
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
91 class ConfigureTwisted(ConfigureBasic):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
92 LOGGER_CLASS = TwistedLogger
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
93
1012
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
94 def changeObserver(self, observer, can_colors=False):
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
95 """Install a hook on observer to manage SàT specificities
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
96
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
97 @param observer: original observer to hook
1012
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
98 @param can_colors: True if observer can display ansi colors
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
99 """
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
100 def observer_hook(event):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
101 """redirect non SàT log to twisted_logger, and add colors when possible"""
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
102 if 'sat_logged' in event: # we only want our own logs, other are managed by twistedObserver
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
103 # we add colors if possible
1012
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
104 if (can_colors and self.LOGGER_CLASS.colors) or self.LOGGER_CLASS.force_colors:
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
105 message = event.get('message', tuple())
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
106 level = event.get('level', C.LOG_LVL_INFO)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
107 if message:
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
108 event['message'] = (self.ansiColors(level, ''.join(message)),) # must be a tuple
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
109 observer(event) # we can now call the original observer
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
110
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
111 return observer_hook
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
112
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
113 def changeFileLogObserver(self, observer):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
114 """Install SàT hook for FileLogObserver
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
115
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
116 if the output is a tty, we allow colors, else we don't
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
117 @param observer: original observer to hook
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
118 """
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
119 log_obs = observer.__self__
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
120 log_file = log_obs.write.__self__
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
121 try:
1012
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
122 can_colors = log_file.isatty()
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
123 except AttributeError:
1012
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
124 can_colors = False
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
125 return self.changeObserver(observer, can_colors=can_colors)
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
126
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
127 def installObserverHook(self, observer):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
128 """Check observer type and install SàT hook when possible
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
129
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
130 @param observer: observer to hook
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
131 @return: hooked observer or original one
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
132 """
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
133 if hasattr(observer, '__self__'):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
134 ori = observer
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
135 if isinstance(observer.__self__, self.twisted_log.FileLogObserver):
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
136 observer = self.changeFileLogObserver(observer)
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
137 elif isinstance(observer.__self__, self.twisted_log.DefaultObserver):
1012
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
138 observer = self.changeObserver(observer, can_colors=True)
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
139 else:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
140 # we use print because log system is not fully initialized
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
141 print("Unmanaged observer [%s]" % observer)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
142 return observer
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
143 self.observers[ori] = observer
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
144 return observer
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
145
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
146 def preTreatment(self):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
147 """initialise needed attributes, and install observers hooks"""
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
148 self.observers = {}
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
149 from twisted.python import log as twisted_log
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
150 self.twisted_log = twisted_log
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
151 self.log_publisher = twisted_log.msg.__self__
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
152 def addObserverObserver(self_logpub, other):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
153 """Install hook so we know when a new observer is added"""
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
154 other = self.installObserverHook(other)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
155 return self_logpub._originalAddObserver(other)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
156 def removeObserverObserver(self_logpub, ori):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
157 """removeObserver hook fix
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
158
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
159 As we wrap the original observer, the original removeObserver may want to remove the original object instead of the wrapper, this method fix this
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
160 """
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
161 if ori in self.observers:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
162 self_logpub._originalRemoveObserver(self.observers[ori])
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
163 else:
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
164 try:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
165 self_logpub._originalRemoveObserver(ori)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
166 except ValueError:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
167 try:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
168 ori in self.cleared_observers
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
169 except AttributeError:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
170 raise ValueError("Unknown observer")
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
171
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
172 # we replace addObserver/removeObserver by our own
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
173 twisted_log.LogPublisher._originalAddObserver = twisted_log.LogPublisher.addObserver
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
174 twisted_log.LogPublisher._originalRemoveObserver = twisted_log.LogPublisher.removeObserver
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
175 import types # see https://stackoverflow.com/a/4267590 (thx Chris Morgan/aaronasterling)
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
176 twisted_log.addObserver = types.MethodType(addObserverObserver, self.log_publisher, twisted_log.LogPublisher)
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
177 twisted_log.removeObserver = types.MethodType(removeObserverObserver, self.log_publisher, twisted_log.LogPublisher)
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
178
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
179 # we now change existing observers
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
180 for idx, observer in enumerate(self.log_publisher.observers):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
181 self.log_publisher.observers[idx] = self.installObserverHook(observer)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
182
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
183 def configureLevel(self, level):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
184 self.LOGGER_CLASS.level = level
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
185 super(ConfigureTwisted, self).configureLevel(level)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
186
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
187 def configureOutput(self, output):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
188 import sys
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
189 if output is None:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
190 output = C.LOG_OPT_OUTPUT_SEP + C.LOG_OPT_OUTPUT_DEFAULT
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
191 self.manageOutputs(output)
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
192 addObserver = self.twisted_log.addObserver
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
193
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
194 if C.LOG_OPT_OUTPUT_DEFAULT in log.handlers:
1116
ee450d7c88a7 core (logging): logging is added to stdout in nodaemon mode (it was already the case in debug mode)
Goffi <goffi@goffi.org>
parents: 1021
diff changeset
195 # default output is already managed, we just add output to stdout if we are in debug or nodaemon mode
1129
08f50fdac21b core (logging): new backend_data parameter can be used to transmit specific data to a backend + Twisted backend use this option to know if we are in debug or nodaemon mode
Goffi <goffi@goffi.org>
parents: 1121
diff changeset
196 if self.backend_data is None:
08f50fdac21b core (logging): new backend_data parameter can be used to transmit specific data to a backend + Twisted backend use this option to know if we are in debug or nodaemon mode
Goffi <goffi@goffi.org>
parents: 1121
diff changeset
197 raise ValueError("You must pass options as backend_data with Twisted backend")
08f50fdac21b core (logging): new backend_data parameter can be used to transmit specific data to a backend + Twisted backend use this option to know if we are in debug or nodaemon mode
Goffi <goffi@goffi.org>
parents: 1121
diff changeset
198 options = self.backend_data
08f50fdac21b core (logging): new backend_data parameter can be used to transmit specific data to a backend + Twisted backend use this option to know if we are in debug or nodaemon mode
Goffi <goffi@goffi.org>
parents: 1121
diff changeset
199 if options.get('nodaemon', False) or options.get('debug', False):
08f50fdac21b core (logging): new backend_data parameter can be used to transmit specific data to a backend + Twisted backend use this option to know if we are in debug or nodaemon mode
Goffi <goffi@goffi.org>
parents: 1121
diff changeset
200 addObserver(self.twisted_log.FileLogObserver(sys.stdout).emit)
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
201 else:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
202 # \\default is not in the output, so we remove current observers
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
203 self.cleared_observers = self.log_publisher.observers
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
204 self.observers.clear()
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
205 del self.log_publisher.observers[:]
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
206 # and we forbid twistd to add any observer
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
207 self.twisted_log.addObserver = lambda other: None
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
208
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
209 if C.LOG_OPT_OUTPUT_FILE in log.handlers:
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
210 from twisted.python import logfile
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
211 for path in log.handlers[C.LOG_OPT_OUTPUT_FILE]:
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
212 log_file = sys.stdout if path == '-' else logfile.LogFile.fromFullPath(path)
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
213 addObserver(self.twisted_log.FileLogObserver(log_file).emit)
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
214
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
215 if C.LOG_OPT_OUTPUT_MEMORY in log.handlers:
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
216 raise NotImplementedError("Memory observer is not implemented in Twisted backend")
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
217
1942
7f053e1f0b67 core (logs): taints:
Goffi <goffi@goffi.org>
parents: 1941
diff changeset
218 def configureColors(self, colors, force_colors, levels_taints_dict):
7f053e1f0b67 core (logs): taints:
Goffi <goffi@goffi.org>
parents: 1941
diff changeset
219 super(ConfigureTwisted, self).configureColors(colors, force_colors, levels_taints_dict)
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
220 self.LOGGER_CLASS.colors = colors
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
221 self.LOGGER_CLASS.force_colors = force_colors
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
222 if force_colors and not colors:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
223 raise ValueError('colors must be True if force_colors is True')
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
224
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
225 def postTreatment(self):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
226 """Install twistedObserver which manage non SàT logs"""
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
227 def twistedObserver(event):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
228 """Observer which redirect log message not produced by SàT to SàT logging system"""
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
229 if not 'sat_logged' in event:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
230 # this log was not produced by SàT
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
231 from twisted.python import log as twisted_log
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
232 text = twisted_log.textFromEventDict(event)
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
233 if text is None:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
234 return
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
235 twisted_logger = log.getLogger(C.LOG_TWISTED_LOGGER)
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
236 log_method = twisted_logger.error if event.get('isError', False) else twisted_logger.info
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
237 log_method(text.decode('utf-8'))
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
238
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
239 self.log_publisher._originalAddObserver(twistedObserver)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
240
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
241
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
242 class ConfigureStandard(ConfigureBasic):
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
243
1942
7f053e1f0b67 core (logs): taints:
Goffi <goffi@goffi.org>
parents: 1941
diff changeset
244 def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, levels_taints_dict=None, force_colors=False, backend_data=None):
1010
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
245 if fmt is None:
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
246 fmt = C.LOG_OPT_FORMAT[1]
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
247 if output is None:
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
248 output = C.LOG_OPT_OUTPUT[1]
1942
7f053e1f0b67 core (logs): taints:
Goffi <goffi@goffi.org>
parents: 1941
diff changeset
249 super(ConfigureStandard, self).__init__(level, fmt, output, logger, colors, levels_taints_dict, force_colors, backend_data)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
250
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
251 def preTreatment(self):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
252 """We use logging methods directly, instead of using Logger"""
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
253 import logging
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
254 log.getLogger = logging.getLogger
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
255 log.debug = logging.debug
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
256 log.info = logging.info
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
257 log.warning = logging.warning
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
258 log.error = logging.error
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
259 log.critical = logging.critical
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
260
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
261 def configureLevel(self, level):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
262 if level is None:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
263 level = C.LOG_LVL_DEBUG
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
264 self.level = level
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
265
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
266 def configureFormat(self, fmt):
1940
3fdacba9da68 core (logs): log color location can now be specified with %(color_start)s and %(color_end)s
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
267 super(ConfigureStandard, self).configureFormat(fmt)
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
268 import logging
2089
0931b5a6213c core, quick_frontends: android compatibility hacks:
Goffi <goffi@goffi.org>
parents: 1942
diff changeset
269 import sys
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
270
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
271 class SatFormatter(logging.Formatter):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
272 u"""Formatter which manage SàT specificities"""
1012
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
273 _format = fmt
1013
11409a6c16c7 core (log/standard backend): added "%(profile)s" format management
Goffi <goffi@goffi.org>
parents: 1012
diff changeset
274 _with_profile = '%(profile)s' in fmt
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
275
1012
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
276 def __init__(self, can_colors=False):
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
277 super(SatFormatter, self).__init__(self._format)
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
278 self.can_colors = can_colors
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
279
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
280 def format(self, record):
1013
11409a6c16c7 core (log/standard backend): added "%(profile)s" format management
Goffi <goffi@goffi.org>
parents: 1012
diff changeset
281 if self._with_profile:
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
282 record.profile = ConfigureStandard.getProfile()
1940
3fdacba9da68 core (logs): log color location can now be specified with %(color_start)s and %(color_end)s
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
283 do_color = self.with_colors and (self.can_colors or self.force_colors)
3fdacba9da68 core (logs): log color location can now be specified with %(color_start)s and %(color_end)s
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
284 if ConfigureStandard._color_location:
3fdacba9da68 core (logs): log color location can now be specified with %(color_start)s and %(color_end)s
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
285 # we copy raw formatting strings for color_*
3fdacba9da68 core (logs): log color location can now be specified with %(color_start)s and %(color_end)s
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
286 # as formatting is handled in ansiColors in this case
3fdacba9da68 core (logs): log color location can now be specified with %(color_start)s and %(color_end)s
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
287 if do_color:
3fdacba9da68 core (logs): log color location can now be specified with %(color_start)s and %(color_end)s
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
288 record.color_start = log.COLOR_START
3fdacba9da68 core (logs): log color location can now be specified with %(color_start)s and %(color_end)s
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
289 record.color_end = log.COLOR_END
3fdacba9da68 core (logs): log color location can now be specified with %(color_start)s and %(color_end)s
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
290 else:
3fdacba9da68 core (logs): log color location can now be specified with %(color_start)s and %(color_end)s
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
291 record.color_start = record.color_end = ''
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
292 s = super(SatFormatter, self).format(record)
1940
3fdacba9da68 core (logs): log color location can now be specified with %(color_start)s and %(color_end)s
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
293 if do_color:
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
294 s = ConfigureStandard.ansiColors(record.levelname, s)
2089
0931b5a6213c core, quick_frontends: android compatibility hacks:
Goffi <goffi@goffi.org>
parents: 1942
diff changeset
295 if sys.platform == "android":
0931b5a6213c core, quick_frontends: android compatibility hacks:
Goffi <goffi@goffi.org>
parents: 1942
diff changeset
296 # FIXME: dirty hack to workaround android encoding issue on log
0931b5a6213c core, quick_frontends: android compatibility hacks:
Goffi <goffi@goffi.org>
parents: 1942
diff changeset
297 # need to be fixed properly
0931b5a6213c core, quick_frontends: android compatibility hacks:
Goffi <goffi@goffi.org>
parents: 1942
diff changeset
298 return s.encode('ascii', 'ignore')
0931b5a6213c core, quick_frontends: android compatibility hacks:
Goffi <goffi@goffi.org>
parents: 1942
diff changeset
299 else:
0931b5a6213c core, quick_frontends: android compatibility hacks:
Goffi <goffi@goffi.org>
parents: 1942
diff changeset
300 return s
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
301
1012
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
302 self.formatterClass = SatFormatter
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
303
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
304 def configureOutput(self, output):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
305 self.manageOutputs(output)
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
306
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
307 def configureLogger(self, logger):
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
308 self.name_filter = log.FilterName(logger) if logger else None
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
309
1942
7f053e1f0b67 core (logs): taints:
Goffi <goffi@goffi.org>
parents: 1941
diff changeset
310 def configureColors(self, colors, force_colors, levels_taints_dict):
7f053e1f0b67 core (logs): taints:
Goffi <goffi@goffi.org>
parents: 1941
diff changeset
311 super(ConfigureStandard, self).configureColors(colors, force_colors, levels_taints_dict)
1012
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
312 self.formatterClass.with_colors = colors
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
313 self.formatterClass.force_colors = force_colors
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
314 if not colors and force_colors:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
315 raise ValueError("force_colors can't be used if colors is False")
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
316
1012
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
317 def _addHandler(self, root_logger, hdlr, can_colors=False):
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
318 hdlr.setFormatter(self.formatterClass(can_colors))
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
319 root_logger.addHandler(hdlr)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
320 root_logger.setLevel(self.level)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
321 if self.name_filter is not None:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
322 hdlr.addFilter(self.name_filter)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
323
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
324 def postTreatment(self):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
325 import logging
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
326 root_logger = logging.getLogger()
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
327 if len(root_logger.handlers) == 0:
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
328 for handler, options in log.handlers.items():
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
329 if handler == C.LOG_OPT_OUTPUT_DEFAULT:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
330 hdlr = logging.StreamHandler()
1012
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
331 try:
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
332 can_colors = hdlr.stream.isatty()
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
333 except AttributeError:
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
334 can_colors = False
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
335 self._addHandler(root_logger, hdlr, can_colors=can_colors)
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
336 elif handler == C.LOG_OPT_OUTPUT_MEMORY:
1010
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
337 from logging.handlers import BufferingHandler
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
338 class SatMemoryHandler(BufferingHandler):
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
339 def emit(self, record):
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
340 super(SatMemoryHandler, self).emit(self.format(record))
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
341 hdlr = SatMemoryHandler(options)
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
342 log.handlers[handler] = hdlr # we keep a reference to the handler to read the buffer later
1012
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
343 self._addHandler(root_logger, hdlr, can_colors=False)
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
344 elif handler == C.LOG_OPT_OUTPUT_FILE:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
345 import os.path
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
346 for path in options:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
347 hdlr = logging.FileHandler(os.path.expanduser(path))
1012
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
348 self._addHandler(root_logger, hdlr, can_colors=False)
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
349 else:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
350 raise ValueError("Unknown handler type")
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
351 else:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
352 root_logger.warning(u"Handlers already set on root logger")
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
353
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
354 @staticmethod
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
355 def memoryGet(size=None):
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
356 """Return buffered logs
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
357
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
358 @param size: number of logs to return
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
359 """
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
360 mem_handler = log.handlers[C.LOG_OPT_OUTPUT_MEMORY]
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
361 return (log_msg for log_msg in mem_handler.buffer[size if size is None else -size:])
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
362
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
363
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
364 log.configure_cls[C.LOG_BACKEND_BASIC] = ConfigureBasic
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
365 log.configure_cls[C.LOG_BACKEND_TWISTED] = ConfigureTwisted
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
366 log.configure_cls[C.LOG_BACKEND_STANDARD] = ConfigureStandard
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
367
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
368 def configure(backend, **options):
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
369 """Configure logging behaviour
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
370 @param backend: can be:
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
371 C.LOG_BACKEND_STANDARD: use standard logging module
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
372 C.LOG_BACKEND_TWISTED: use twisted logging module (with standard logging observer)
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
373 C.LOG_BACKEND_BASIC: use a basic print based logging
1017
0ea97f483464 core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents: 1016
diff changeset
374 C.LOG_BACKEND_CUSTOM: use a given Logger subclass
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
375 """
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
376 return log.configure(backend, **options)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
377
994
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
378 def _parseOptions(options):
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
379 """Parse string options as given in conf or environment variable, and return expected python value
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
380
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
381 @param options (dict): options with (key: name, value: string value)
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
382 """
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
383 COLORS = C.LOG_OPT_COLORS[0]
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
384 LEVEL = C.LOG_OPT_LEVEL[0]
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
385
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
386 if COLORS in options:
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
387 if options[COLORS].lower() in ('1', 'true'):
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
388 options[COLORS] = True
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
389 elif options[COLORS] == 'force':
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
390 options[COLORS] = True
994
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
391 options['force_colors'] = True
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
392 else:
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
393 options[COLORS] = False
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
394 if LEVEL in options:
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
395 level = options[LEVEL].upper()
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
396 if level not in C.LOG_LEVELS:
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
397 level = C.LOG_LVL_INFO
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
398 options[LEVEL] = level
994
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
399
1129
08f50fdac21b core (logging): new backend_data parameter can be used to transmit specific data to a backend + Twisted backend use this option to know if we are in debug or nodaemon mode
Goffi <goffi@goffi.org>
parents: 1121
diff changeset
400 def satConfigure(backend=C.LOG_BACKEND_STANDARD, const=None, backend_data=None):
994
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
401 """Configure logging system for SàT, can be used by frontends
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
402
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
403 logs conf is read in SàT conf, then in environment variables. It must be done before Memory init
1010
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
404 @param backend: backend to use, it can be:
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
405 - C.LOG_BACKEND_BASIC: print based backend
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
406 - C.LOG_BACKEND_TWISTED: Twisted logging backend
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
407 - C.LOG_BACKEND_STANDARD: standard logging backend
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
408 @param const: Const class to use instead of sat.core.constants.Const (mainly used to change default values)
994
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
409 """
1010
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
410 if const is not None:
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
411 global C
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
412 C = const
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
413 log.C = const
1941
befdcfc55569 core (logs): use tools.config to handle sat.conf
Goffi <goffi@goffi.org>
parents: 1940
diff changeset
414 from sat.tools import config
994
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
415 import os
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
416 log_conf = {}
1941
befdcfc55569 core (logs): use tools.config to handle sat.conf
Goffi <goffi@goffi.org>
parents: 1940
diff changeset
417 sat_conf = config.parseMainConf()
1010
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
418 for opt_name, opt_default in C.LOG_OPTIONS():
994
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
419 try:
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
420 log_conf[opt_name] = os.environ[''.join((C.ENV_PREFIX, C.LOG_OPT_PREFIX.upper(), opt_name.upper()))]
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
421 except KeyError:
1941
befdcfc55569 core (logs): use tools.config to handle sat.conf
Goffi <goffi@goffi.org>
parents: 1940
diff changeset
422 log_conf[opt_name] = config.getConfig(sat_conf, C.LOG_OPT_SECTION, C.LOG_OPT_PREFIX + opt_name, opt_default)
994
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
423
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
424 _parseOptions(log_conf)
1129
08f50fdac21b core (logging): new backend_data parameter can be used to transmit specific data to a backend + Twisted backend use this option to know if we are in debug or nodaemon mode
Goffi <goffi@goffi.org>
parents: 1121
diff changeset
425 configure(backend, backend_data=backend_data, **log_conf)