Mercurial > libervia-backend
annotate src/core/log_config.py @ 2043:f3940f6a3222
quick frontend (widgets): a deletion with onDelete can now be cancelled if onDelete return False
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 22 Aug 2016 20:56:51 +0200 |
parents | 7f053e1f0b67 |
children | 0931b5a6213c |
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 | 2 # -*- coding: utf-8 -*- |
3 | |
4 # SàT: a XMPP client | |
1766 | 5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org) |
991 | 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 """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 | 22 |
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 | 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 | 46 def configureColors(self, colors, force_colors, levels_taints_dict): |
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 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
50 if force_colors or sys.stdout.isatty(): # FIXME: isatty should be tested on each handler, not globaly |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
51 # we need colors |
1942 | 52 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
|
53 elif force_colors: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
54 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
|
55 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
56 @staticmethod |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
57 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
|
58 """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
|
59 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
|
60 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
|
61 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
|
62 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
|
63 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
|
64 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
|
65 continue |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
66 break |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
67 |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
68 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
|
69 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
|
70 try: |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
71 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
|
72 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
|
73 try: |
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['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
|
76 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
|
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 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
|
79 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
|
80 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
|
81 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
|
82 # 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
|
83 profile = '' |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
84 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
|
85 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
86 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
87 class ConfigureTwisted(ConfigureBasic): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
88 LOGGER_CLASS = TwistedLogger |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
89 |
1012
c8771279497e
core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents:
1010
diff
changeset
|
90 def changeObserver(self, observer, can_colors=False): |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
91 """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
|
92 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
93 @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
|
94 @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
|
95 """ |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
96 def observer_hook(event): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
97 """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
|
98 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
|
99 # 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
|
100 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
|
101 message = event.get('message', tuple()) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
102 level = event.get('level', C.LOG_LVL_INFO) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
103 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
|
104 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
|
105 observer(event) # we can now call the original observer |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
106 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
107 return observer_hook |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
108 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
109 def changeFileLogObserver(self, observer): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
110 """Install SàT hook for FileLogObserver |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
111 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
112 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
|
113 @param observer: original observer to hook |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
114 """ |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
115 log_obs = observer.__self__ |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
116 log_file = log_obs.write.__self__ |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
117 try: |
1012
c8771279497e
core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents:
1010
diff
changeset
|
118 can_colors = log_file.isatty() |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
119 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
|
120 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
|
121 return self.changeObserver(observer, can_colors=can_colors) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
122 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
123 def installObserverHook(self, observer): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
124 """Check observer type and install SàT hook when possible |
991 | 125 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
126 @param observer: observer to hook |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
127 @return: hooked observer or original one |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
128 """ |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
129 if hasattr(observer, '__self__'): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
130 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
|
131 if isinstance(observer.__self__, self.twisted_log.FileLogObserver): |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
132 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
|
133 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
|
134 observer = self.changeObserver(observer, can_colors=True) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
135 else: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
136 # we use print because log system is not fully initialized |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
137 print("Unmanaged observer [%s]" % observer) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
138 return observer |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
139 self.observers[ori] = observer |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
140 return observer |
991 | 141 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
142 def preTreatment(self): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
143 """initialise needed attributes, and install observers hooks""" |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
144 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
|
145 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
|
146 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
|
147 self.log_publisher = twisted_log.msg.__self__ |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
148 def addObserverObserver(self_logpub, other): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
149 """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
|
150 other = self.installObserverHook(other) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
151 return self_logpub._originalAddObserver(other) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
152 def removeObserverObserver(self_logpub, ori): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
153 """removeObserver hook fix |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
154 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
155 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
|
156 """ |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
157 if ori in self.observers: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
158 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
|
159 else: |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
160 try: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
161 self_logpub._originalRemoveObserver(ori) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
162 except ValueError: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
163 try: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
164 ori in self.cleared_observers |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
165 except AttributeError: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
166 raise ValueError("Unknown observer") |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
167 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
168 # 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
|
169 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
|
170 twisted_log.LogPublisher._originalRemoveObserver = twisted_log.LogPublisher.removeObserver |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
171 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
|
172 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
|
173 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
|
174 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
175 # we now change existing observers |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
176 for idx, observer in enumerate(self.log_publisher.observers): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
177 self.log_publisher.observers[idx] = self.installObserverHook(observer) |
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 def configureLevel(self, level): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
180 self.LOGGER_CLASS.level = level |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
181 super(ConfigureTwisted, self).configureLevel(level) |
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 configureOutput(self, output): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
184 import sys |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
185 if output is None: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
186 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
|
187 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
|
188 addObserver = self.twisted_log.addObserver |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
189 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
190 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
|
191 # 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
|
192 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
|
193 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
|
194 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
|
195 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
|
196 addObserver(self.twisted_log.FileLogObserver(sys.stdout).emit) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
197 else: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
198 # \\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
|
199 self.cleared_observers = self.log_publisher.observers |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
200 self.observers.clear() |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
201 del self.log_publisher.observers[:] |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
202 # 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
|
203 self.twisted_log.addObserver = lambda other: None |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
204 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
205 if C.LOG_OPT_OUTPUT_FILE in log.handlers: |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
206 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
|
207 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
|
208 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
|
209 addObserver(self.twisted_log.FileLogObserver(log_file).emit) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
210 |
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 if C.LOG_OPT_OUTPUT_MEMORY in log.handlers: |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
212 raise NotImplementedError("Memory observer is not implemented in Twisted backend") |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
213 |
1942 | 214 def configureColors(self, colors, force_colors, levels_taints_dict): |
215 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
|
216 self.LOGGER_CLASS.colors = colors |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
217 self.LOGGER_CLASS.force_colors = force_colors |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
218 if force_colors and not colors: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
219 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
|
220 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
221 def postTreatment(self): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
222 """Install twistedObserver which manage non SàT logs""" |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
223 def twistedObserver(event): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
224 """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
|
225 if not 'sat_logged' in event: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
226 # 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
|
227 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
|
228 text = twisted_log.textFromEventDict(event) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
229 if text is None: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
230 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
|
231 twisted_logger = log.getLogger(C.LOG_TWISTED_LOGGER) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
232 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
|
233 log_method(text.decode('utf-8')) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
234 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
235 self.log_publisher._originalAddObserver(twistedObserver) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
236 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
237 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
238 class ConfigureStandard(ConfigureBasic): |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
239 |
1942 | 240 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
|
241 if fmt is None: |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
242 fmt = C.LOG_OPT_FORMAT[1] |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
243 if output is None: |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
244 output = C.LOG_OPT_OUTPUT[1] |
1942 | 245 super(ConfigureStandard, self).__init__(level, fmt, output, logger, colors, levels_taints_dict, force_colors, backend_data) |
991 | 246 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
247 def preTreatment(self): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
248 """We use logging methods directly, instead of using Logger""" |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
249 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
|
250 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
|
251 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
|
252 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
|
253 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
|
254 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
|
255 log.critical = logging.critical |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
256 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
257 def configureLevel(self, level): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
258 if level is None: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
259 level = C.LOG_LVL_DEBUG |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
260 self.level = level |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
261 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
262 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
|
263 super(ConfigureStandard, self).configureFormat(fmt) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
264 import logging |
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 class SatFormatter(logging.Formatter): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
267 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
|
268 _format = fmt |
1013
11409a6c16c7
core (log/standard backend): added "%(profile)s" format management
Goffi <goffi@goffi.org>
parents:
1012
diff
changeset
|
269 _with_profile = '%(profile)s' in fmt |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
270 |
1012
c8771279497e
core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents:
1010
diff
changeset
|
271 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
|
272 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
|
273 self.can_colors = can_colors |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
274 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
275 def format(self, record): |
1013
11409a6c16c7
core (log/standard backend): added "%(profile)s" format management
Goffi <goffi@goffi.org>
parents:
1012
diff
changeset
|
276 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
|
277 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
|
278 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
|
279 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
|
280 # 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
|
281 # 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
|
282 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
|
283 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
|
284 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
|
285 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
|
286 record.color_start = record.color_end = '' |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
287 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
|
288 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
|
289 s = ConfigureStandard.ansiColors(record.levelname, s) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
290 return s |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
291 |
1012
c8771279497e
core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents:
1010
diff
changeset
|
292 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
|
293 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
294 def configureOutput(self, output): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
295 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
|
296 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
297 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
|
298 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
|
299 |
1942 | 300 def configureColors(self, colors, force_colors, levels_taints_dict): |
301 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
|
302 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
|
303 self.formatterClass.force_colors = force_colors |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
304 if not colors and force_colors: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
305 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
|
306 |
1012
c8771279497e
core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents:
1010
diff
changeset
|
307 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
|
308 hdlr.setFormatter(self.formatterClass(can_colors)) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
309 root_logger.addHandler(hdlr) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
310 root_logger.setLevel(self.level) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
311 if self.name_filter is not None: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
312 hdlr.addFilter(self.name_filter) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
313 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
314 def postTreatment(self): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
315 import logging |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
316 root_logger = logging.getLogger() |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
317 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
|
318 for handler, options in log.handlers.items(): |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
319 if handler == C.LOG_OPT_OUTPUT_DEFAULT: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
320 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
|
321 try: |
c8771279497e
core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents:
1010
diff
changeset
|
322 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
|
323 except AttributeError: |
c8771279497e
core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents:
1010
diff
changeset
|
324 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
|
325 self._addHandler(root_logger, hdlr, can_colors=can_colors) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
326 elif handler == C.LOG_OPT_OUTPUT_MEMORY: |
1010
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
327 from logging.handlers import BufferingHandler |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
328 class SatMemoryHandler(BufferingHandler): |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
329 def emit(self, record): |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
330 super(SatMemoryHandler, self).emit(self.format(record)) |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
331 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
|
332 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
|
333 self._addHandler(root_logger, hdlr, can_colors=False) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
334 elif handler == C.LOG_OPT_OUTPUT_FILE: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
335 import os.path |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
336 for path in options: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
337 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
|
338 self._addHandler(root_logger, hdlr, can_colors=False) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
339 else: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
340 raise ValueError("Unknown handler type") |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
341 else: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
342 root_logger.warning(u"Handlers already set on root logger") |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
343 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
344 @staticmethod |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
345 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
|
346 """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
|
347 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
348 @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
|
349 """ |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
350 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
|
351 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
|
352 |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
353 |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
354 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
|
355 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
|
356 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
|
357 |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
358 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
|
359 """Configure logging behaviour |
991 | 360 @param backend: can be: |
361 C.LOG_BACKEND_STANDARD: use standard logging module | |
362 C.LOG_BACKEND_TWISTED: use twisted logging module (with standard logging observer) | |
363 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
|
364 C.LOG_BACKEND_CUSTOM: use a given Logger subclass |
991 | 365 """ |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
366 return log.configure(backend, **options) |
991 | 367 |
994
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
368 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
|
369 """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
|
370 |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
371 @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
|
372 """ |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
373 COLORS = C.LOG_OPT_COLORS[0] |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
374 LEVEL = C.LOG_OPT_LEVEL[0] |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
375 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
376 if COLORS in options: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
377 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
|
378 options[COLORS] = True |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
379 elif options[COLORS] == 'force': |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
380 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
|
381 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
|
382 else: |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
383 options[COLORS] = False |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
384 if LEVEL in options: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
385 level = options[LEVEL].upper() |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
386 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
|
387 level = C.LOG_LVL_INFO |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
388 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
|
389 |
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
|
390 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
|
391 """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
|
392 |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
393 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
|
394 @param backend: backend to use, it can be: |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
395 - C.LOG_BACKEND_BASIC: print based backend |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
396 - C.LOG_BACKEND_TWISTED: Twisted logging backend |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
397 - C.LOG_BACKEND_STANDARD: standard logging backend |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
398 @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
|
399 """ |
1010
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
400 if const is not None: |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
401 global C |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
402 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
|
403 log.C = const |
1941
befdcfc55569
core (logs): use tools.config to handle sat.conf
Goffi <goffi@goffi.org>
parents:
1940
diff
changeset
|
404 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
|
405 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
|
406 log_conf = {} |
1941
befdcfc55569
core (logs): use tools.config to handle sat.conf
Goffi <goffi@goffi.org>
parents:
1940
diff
changeset
|
407 sat_conf = config.parseMainConf() |
1010
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
408 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
|
409 try: |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
410 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
|
411 except KeyError: |
1941
befdcfc55569
core (logs): use tools.config to handle sat.conf
Goffi <goffi@goffi.org>
parents:
1940
diff
changeset
|
412 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
|
413 |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
414 _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
|
415 configure(backend, backend_data=backend_data, **log_conf) |