Mercurial > libervia-backend
annotate sat/core/log_config.py @ 4012:d538b07cddf3
plugin AP gateway: fix exception on HEAD requests
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 18 Mar 2023 15:01:55 +0100 |
parents | 760f563b1243 |
children | 524856bd7b19 |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
991 | 3 |
3480
7550ae9cfbac
Renamed the project from "Salut à Toi" to "Libervia":
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
4 # Libervia: an XMPP client |
3479 | 5 # Copyright (C) 2009-2021 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): |
3280
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
32 super().__init__(*args, **kwargs) |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
33 from twisted.logger import Logger |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
34 self.twisted_log = Logger() |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
35 |
2836
ad00f61fd9f5
core (log): add traceback when "exc_info" is set
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
36 def out(self, message, level=None, **kwargs): |
1007
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 """ |
3280
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
41 self.twisted_log.emit( |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
42 level=self.level_map[level], |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
43 format=message, |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
44 sat_logged=True, |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
45 **kwargs, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
46 ) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
47 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
48 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
49 class ConfigureBasic(log.ConfigureBase): |
1942 | 50 def configureColors(self, colors, force_colors, levels_taints_dict): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
51 super(ConfigureBasic, self).configureColors( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
52 colors, force_colors, levels_taints_dict |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
53 ) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
54 if colors: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
55 import sys |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
56 |
2089
0931b5a6213c
core, quick_frontends: android compatibility hacks:
Goffi <goffi@goffi.org>
parents:
1942
diff
changeset
|
57 try: |
0931b5a6213c
core, quick_frontends: android compatibility hacks:
Goffi <goffi@goffi.org>
parents:
1942
diff
changeset
|
58 isatty = sys.stdout.isatty() |
0931b5a6213c
core, quick_frontends: android compatibility hacks:
Goffi <goffi@goffi.org>
parents:
1942
diff
changeset
|
59 except AttributeError: |
0931b5a6213c
core, quick_frontends: android compatibility hacks:
Goffi <goffi@goffi.org>
parents:
1942
diff
changeset
|
60 isatty = False |
2732
e55f871fa9db
core (log): fixed double call to ansiColors
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
61 # FIXME: isatty should be tested on each handler, not globaly |
e55f871fa9db
core (log): fixed double call to ansiColors
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
62 if (force_colors or isatty): |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
63 # we need colors |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
64 log.Logger.post_treat = lambda logger, level, message: self.ansiColors( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
65 level, message |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
66 ) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
67 elif force_colors: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
68 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
|
69 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
70 @staticmethod |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
71 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
|
72 """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
|
73 import inspect |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
74 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
75 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
|
76 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
|
77 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
|
78 if frame_data[1] != current_path: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
79 if ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
80 log.backend == C.LOG_BACKEND_STANDARD |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
81 and "/logging/__init__.py" in frame_data[1] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
82 ): |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
83 continue |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
84 break |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
85 |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
86 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
|
87 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
|
88 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
89 profile = args.locals.get("profile") or args.locals["profile_key"] |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
90 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
|
91 try: |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
92 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
93 profile = args.locals["self"].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
|
94 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
|
95 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
96 profile = args.locals["self"].parent.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
|
97 except AttributeError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
98 profile = args.locals[ |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
99 "self" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
100 ].host.profile # used in quick_frontend for single profile configuration |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
101 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
|
102 # we can't find profile, we return an empty value |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
103 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
|
104 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
|
105 |
1007
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 class ConfigureTwisted(ConfigureBasic): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
108 LOGGER_CLASS = TwistedLogger |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
109 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
110 def preTreatment(self): |
3280
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
111 from twisted import logger |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
112 global logger |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
113 self.level_map = { |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
114 C.LOG_LVL_DEBUG: logger.LogLevel.debug, |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
115 C.LOG_LVL_INFO: logger.LogLevel.info, |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
116 C.LOG_LVL_WARNING: logger.LogLevel.warn, |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
117 C.LOG_LVL_ERROR: logger.LogLevel.error, |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
118 C.LOG_LVL_CRITICAL: logger.LogLevel.critical, |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
119 } |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
120 self.LOGGER_CLASS.level_map = self.level_map |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
121 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
122 def configureLevel(self, level): |
3280
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
123 self.level = self.level_map[level] |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
124 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
125 def configureOutput(self, output): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
126 import sys |
3280
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
127 from twisted.python import logfile |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
128 self.log_publisher = logger.LogPublisher() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
129 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
130 if output is None: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
131 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
|
132 self.manageOutputs(output) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
133 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
134 if C.LOG_OPT_OUTPUT_DEFAULT in log.handlers: |
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
|
135 if self.backend_data is None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
136 raise ValueError( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
137 "You must pass options as backend_data with Twisted backend" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
138 ) |
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
|
139 options = self.backend_data |
3280
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
140 log_file = logfile.LogFile.fromFullPath(options['logfile']) |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
141 self.log_publisher.addObserver( |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
142 logger.FileLogObserver(log_file, self.textFormatter)) |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
143 # we also want output to stdout if we are in debug or nodaemon mode |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
144 if options.get("nodaemon", False) or options.get("debug", False): |
3280
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
145 self.log_publisher.addObserver( |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
146 logger.FileLogObserver(sys.stdout, self.textFormatter)) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
147 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
148 if C.LOG_OPT_OUTPUT_FILE in log.handlers: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
149 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
150 for path in log.handlers[C.LOG_OPT_OUTPUT_FILE]: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
151 log_file = ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
152 sys.stdout if path == "-" else logfile.LogFile.fromFullPath(path) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
153 ) |
3280
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
154 self.log_publisher.addObserver( |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
155 logger.FileLogObserver(log_file, self.textFormatter)) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
156 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
157 if C.LOG_OPT_OUTPUT_MEMORY in log.handlers: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
158 raise NotImplementedError( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
159 "Memory observer is not implemented in Twisted backend" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
160 ) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
161 |
1942 | 162 def configureColors(self, colors, force_colors, levels_taints_dict): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
163 super(ConfigureTwisted, self).configureColors( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
164 colors, force_colors, levels_taints_dict |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
165 ) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
166 self.LOGGER_CLASS.colors = colors |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
167 self.LOGGER_CLASS.force_colors = force_colors |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
168 if force_colors and not colors: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
169 raise ValueError("colors must be True if force_colors is True") |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
170 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
171 def postTreatment(self): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
172 """Install twistedObserver which manage non SàT logs""" |
3280
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
173 # from twisted import logger |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
174 import sys |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
175 filtering_obs = logger.FilteringLogObserver( |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
176 observer=self.log_publisher, |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
177 predicates=[ |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
178 logger.LogLevelFilterPredicate(self.level), |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
179 ] |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
180 ) |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
181 logger.globalLogBeginner.beginLoggingTo([filtering_obs]) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
182 |
3280
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
183 def textFormatter(self, event): |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
184 if event.get('sat_logged', False): |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
185 timestamp = ''.join([logger.formatTime(event.get("log_time", None)), " "]) |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
186 return f"{timestamp}{event.get('log_format', '')}\n" |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
187 else: |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
188 eventText = logger.eventAsText( |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
189 event, includeSystem=True) |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
190 if not eventText: |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
191 return None |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
192 return eventText.replace("\n", "\n\t") + "\n" |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
193 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
194 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
195 class ConfigureStandard(ConfigureBasic): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
196 def __init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
197 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
198 level=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
199 fmt=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
200 output=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
201 logger=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
202 colors=False, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
203 levels_taints_dict=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
204 force_colors=False, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
205 backend_data=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
206 ): |
1010
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
207 if fmt is None: |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
208 fmt = C.LOG_OPT_FORMAT[1] |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
209 if output is None: |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
210 output = C.LOG_OPT_OUTPUT[1] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
211 super(ConfigureStandard, self).__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
212 level, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
213 fmt, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
214 output, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
215 logger, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
216 colors, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
217 levels_taints_dict, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
218 force_colors, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
219 backend_data, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
220 ) |
991 | 221 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
222 def preTreatment(self): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
223 """We use logging methods directly, instead of using Logger""" |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
224 import logging |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
225 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
226 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
|
227 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
|
228 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
|
229 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
|
230 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
|
231 log.critical = logging.critical |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
232 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
233 def configureLevel(self, level): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
234 if level is None: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
235 level = C.LOG_LVL_DEBUG |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
236 self.level = level |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
237 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
238 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
|
239 super(ConfigureStandard, self).configureFormat(fmt) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
240 import logging |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
241 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
242 class SatFormatter(logging.Formatter): |
3028 | 243 """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
|
244 _format = fmt |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
245 _with_profile = "%(profile)s" in fmt |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
246 |
1012
c8771279497e
core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents:
1010
diff
changeset
|
247 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
|
248 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
|
249 self.can_colors = can_colors |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
250 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
251 def format(self, record): |
1013
11409a6c16c7
core (log/standard backend): added "%(profile)s" format management
Goffi <goffi@goffi.org>
parents:
1012
diff
changeset
|
252 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
|
253 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
|
254 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
|
255 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
|
256 # 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
|
257 # 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
|
258 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
|
259 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
|
260 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
|
261 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
262 record.color_start = record.color_end = "" |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
263 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
|
264 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
|
265 s = ConfigureStandard.ansiColors(record.levelname, s) |
3056 | 266 return s |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
267 |
1012
c8771279497e
core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents:
1010
diff
changeset
|
268 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
|
269 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
270 def configureOutput(self, output): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
271 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
|
272 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
273 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
|
274 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
|
275 |
1942 | 276 def configureColors(self, colors, force_colors, levels_taints_dict): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
277 super(ConfigureStandard, self).configureColors( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
278 colors, force_colors, levels_taints_dict |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
279 ) |
1012
c8771279497e
core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents:
1010
diff
changeset
|
280 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
|
281 self.formatterClass.force_colors = force_colors |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
282 if not colors and force_colors: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
283 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
|
284 |
1012
c8771279497e
core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents:
1010
diff
changeset
|
285 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
|
286 hdlr.setFormatter(self.formatterClass(can_colors)) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
287 root_logger.addHandler(hdlr) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
288 root_logger.setLevel(self.level) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
289 if self.name_filter is not None: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
290 hdlr.addFilter(self.name_filter) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
291 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
292 def postTreatment(self): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
293 import logging |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
294 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
295 root_logger = logging.getLogger() |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
296 if len(root_logger.handlers) == 0: |
3028 | 297 for handler, options in list(log.handlers.items()): |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
298 if handler == C.LOG_OPT_OUTPUT_DEFAULT: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
299 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
|
300 try: |
c8771279497e
core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents:
1010
diff
changeset
|
301 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
|
302 except AttributeError: |
c8771279497e
core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents:
1010
diff
changeset
|
303 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
|
304 self._addHandler(root_logger, hdlr, can_colors=can_colors) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
305 elif handler == C.LOG_OPT_OUTPUT_MEMORY: |
1010
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
306 from logging.handlers import BufferingHandler |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
307 |
1010
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
308 class SatMemoryHandler(BufferingHandler): |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
309 def emit(self, record): |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
310 super(SatMemoryHandler, self).emit(self.format(record)) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
311 |
1010
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
312 hdlr = SatMemoryHandler(options) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
313 log.handlers[ |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
314 handler |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
315 ] = ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
316 hdlr |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
317 ) # 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
|
318 self._addHandler(root_logger, hdlr, can_colors=False) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
319 elif handler == C.LOG_OPT_OUTPUT_FILE: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
320 import os.path |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
321 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
322 for path in options: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
323 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
|
324 self._addHandler(root_logger, hdlr, can_colors=False) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
325 else: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
326 raise ValueError("Unknown handler type") |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
327 else: |
3028 | 328 root_logger.warning("Handlers already set on root logger") |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
329 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
330 @staticmethod |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
331 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
|
332 """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
|
333 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
334 @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
|
335 """ |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
336 mem_handler = log.handlers[C.LOG_OPT_OUTPUT_MEMORY] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
337 return ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
338 log_msg for log_msg in mem_handler.buffer[size if size is None else -size :] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
339 ) |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
340 |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
341 |
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.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
|
343 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
|
344 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
|
345 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
346 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
347 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
|
348 """Configure logging behaviour |
991 | 349 @param backend: can be: |
350 C.LOG_BACKEND_STANDARD: use standard logging module | |
351 C.LOG_BACKEND_TWISTED: use twisted logging module (with standard logging observer) | |
352 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
|
353 C.LOG_BACKEND_CUSTOM: use a given Logger subclass |
991 | 354 """ |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
355 return log.configure(backend, **options) |
991 | 356 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
357 |
994
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
358 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
|
359 """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
|
360 |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
361 @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
|
362 """ |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
363 COLORS = C.LOG_OPT_COLORS[0] |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
364 LEVEL = C.LOG_OPT_LEVEL[0] |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
365 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
366 if COLORS in options: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
367 if options[COLORS].lower() in ("1", "true"): |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
368 options[COLORS] = True |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
369 elif options[COLORS] == "force": |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
370 options[COLORS] = True |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
371 options["force_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
|
372 else: |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
373 options[COLORS] = False |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
374 if LEVEL in options: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
375 level = options[LEVEL].upper() |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
376 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
|
377 level = C.LOG_LVL_INFO |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
378 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
|
379 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
380 |
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
|
381 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
|
382 """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
|
383 |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
384 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
|
385 @param backend: backend to use, it can be: |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
386 - C.LOG_BACKEND_BASIC: print based backend |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
387 - C.LOG_BACKEND_TWISTED: Twisted logging backend |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
388 - C.LOG_BACKEND_STANDARD: standard logging backend |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
389 @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
|
390 """ |
1010
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
391 if const is not None: |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
392 global C |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
393 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
|
394 log.C = const |
1941
befdcfc55569
core (logs): use tools.config to handle sat.conf
Goffi <goffi@goffi.org>
parents:
1940
diff
changeset
|
395 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
|
396 import os |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
397 |
994
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
398 log_conf = {} |
1941
befdcfc55569
core (logs): use tools.config to handle sat.conf
Goffi <goffi@goffi.org>
parents:
1940
diff
changeset
|
399 sat_conf = config.parseMainConf() |
1010
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
400 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
|
401 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
402 log_conf[opt_name] = os.environ[ |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
403 "".join((C.ENV_PREFIX, C.LOG_OPT_PREFIX.upper(), opt_name.upper())) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
404 ] |
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 except KeyError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
406 log_conf[opt_name] = config.getConfig( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
407 sat_conf, C.LOG_OPT_SECTION, C.LOG_OPT_PREFIX + opt_name, opt_default |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
408 ) |
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 |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
410 _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
|
411 configure(backend, backend_data=backend_data, **log_conf) |