Mercurial > libervia-backend
annotate libervia/backend/core/log_config.py @ 4294:a0ed5c976bf8 default tip @
component conferences, plugin XEP-0167, XEP-0298: add stream user metadata:
A/V conference now adds user metadata about the stream it is forwarding through XEP-0298.
This is parsed and added to metadata during confirmation on client side.
rel 448
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 06 Aug 2024 23:43:11 +0200 |
parents | 0d7bb4df2343 |
children |
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 |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
23 from libervia.backend.core.constants import Const as C |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
24 from libervia.backend.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 |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4078
diff
changeset
|
34 |
3280
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
35 self.twisted_log = Logger() |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
36 |
2836
ad00f61fd9f5
core (log): add traceback when "exc_info" is set
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
37 def out(self, message, level=None, **kwargs): |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
38 """Actually log the message |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
39 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
40 @param message: formatted message |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
41 """ |
3280
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
42 self.twisted_log.emit( |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
43 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
|
44 format=message, |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
45 sat_logged=True, |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
46 **kwargs, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
47 ) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
48 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
49 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
50 class ConfigureBasic(log.ConfigureBase): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
51 def configure_colors(self, colors, force_colors, levels_taints_dict): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
52 super(ConfigureBasic, self).configure_colors( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
53 colors, force_colors, levels_taints_dict |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
54 ) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
55 if colors: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
56 import sys |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
57 |
2089
0931b5a6213c
core, quick_frontends: android compatibility hacks:
Goffi <goffi@goffi.org>
parents:
1942
diff
changeset
|
58 try: |
0931b5a6213c
core, quick_frontends: android compatibility hacks:
Goffi <goffi@goffi.org>
parents:
1942
diff
changeset
|
59 isatty = sys.stdout.isatty() |
0931b5a6213c
core, quick_frontends: android compatibility hacks:
Goffi <goffi@goffi.org>
parents:
1942
diff
changeset
|
60 except AttributeError: |
0931b5a6213c
core, quick_frontends: android compatibility hacks:
Goffi <goffi@goffi.org>
parents:
1942
diff
changeset
|
61 isatty = False |
2732
e55f871fa9db
core (log): fixed double call to ansiColors
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
62 # FIXME: isatty should be tested on each handler, not globaly |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4078
diff
changeset
|
63 if force_colors or isatty: |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
64 # we need colors |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
65 log.Logger.post_treat = lambda logger, level, message: self.ansi_colors( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
66 level, message |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
67 ) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
68 elif force_colors: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
69 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
|
70 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
71 @staticmethod |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
72 def get_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
|
73 """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
|
74 import inspect |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
75 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
76 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
|
77 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
|
78 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
|
79 if frame_data[1] != current_path: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
80 if ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
81 log.backend == C.LOG_BACKEND_STANDARD |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
82 and "/logging/__init__.py" in frame_data[1] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
83 ): |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
84 continue |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
85 break |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
86 |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
87 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
|
88 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
|
89 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
90 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
|
91 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
|
92 try: |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
93 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
94 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
|
95 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
|
96 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
97 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
|
98 except AttributeError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
99 profile = args.locals[ |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
100 "self" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
101 ].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
|
102 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
|
103 # 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
|
104 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
|
105 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
|
106 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
107 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
108 class ConfigureTwisted(ConfigureBasic): |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
109 LOGGER_CLASS = TwistedLogger |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
110 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
111 def pre_treatment(self): |
3280
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
112 from twisted import logger |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4078
diff
changeset
|
113 |
3280
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
114 global logger |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
115 self.level_map = { |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
116 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
|
117 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
|
118 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
|
119 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
|
120 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
|
121 } |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
122 self.LOGGER_CLASS.level_map = self.level_map |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
123 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
124 def configure_level(self, level): |
3280
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
125 self.level = self.level_map[level] |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
126 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
127 def configure_output(self, output): |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
128 import sys |
3280
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
129 from twisted.python import logfile |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4078
diff
changeset
|
130 |
3280
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
131 self.log_publisher = logger.LogPublisher() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
132 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
133 if output is None: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
134 output = C.LOG_OPT_OUTPUT_SEP + C.LOG_OPT_OUTPUT_DEFAULT |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
135 self.manage_outputs(output) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
136 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
137 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
|
138 if self.backend_data is None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
139 raise ValueError( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
140 "You must pass options as backend_data with Twisted backend" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
141 ) |
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
|
142 options = self.backend_data |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4078
diff
changeset
|
143 log_file = logfile.LogFile.fromFullPath(options["logfile"]) |
3280
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
144 self.log_publisher.addObserver( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4078
diff
changeset
|
145 logger.FileLogObserver(log_file, self.text_formatter) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4078
diff
changeset
|
146 ) |
3280
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
147 # 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
|
148 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
|
149 self.log_publisher.addObserver( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4078
diff
changeset
|
150 logger.FileLogObserver(sys.stdout, self.text_formatter) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4078
diff
changeset
|
151 ) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
152 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
153 if C.LOG_OPT_OUTPUT_FILE in log.handlers: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
154 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
155 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
|
156 log_file = ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
157 sys.stdout if path == "-" else logfile.LogFile.fromFullPath(path) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
158 ) |
3280
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
159 self.log_publisher.addObserver( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4078
diff
changeset
|
160 logger.FileLogObserver(log_file, self.text_formatter) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4078
diff
changeset
|
161 ) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
162 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
163 if C.LOG_OPT_OUTPUT_MEMORY in log.handlers: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
164 raise NotImplementedError( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
165 "Memory observer is not implemented in Twisted backend" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
166 ) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
167 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
168 def configure_colors(self, colors, force_colors, levels_taints_dict): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
169 super(ConfigureTwisted, self).configure_colors( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
170 colors, force_colors, levels_taints_dict |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
171 ) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
172 self.LOGGER_CLASS.colors = colors |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
173 self.LOGGER_CLASS.force_colors = force_colors |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
174 if force_colors and not colors: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
175 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
|
176 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
177 def post_treatment(self): |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
178 """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
|
179 # from twisted import logger |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
180 import sys |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4078
diff
changeset
|
181 |
3280
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
182 filtering_obs = logger.FilteringLogObserver( |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
183 observer=self.log_publisher, |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
184 predicates=[ |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
185 logger.LogLevelFilterPredicate(self.level), |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4078
diff
changeset
|
186 ], |
3280
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
187 ) |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
188 logger.globalLogBeginner.beginLoggingTo([filtering_obs]) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
189 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
190 def text_formatter(self, event): |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4078
diff
changeset
|
191 if event.get("sat_logged", False): |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4078
diff
changeset
|
192 timestamp = "".join([logger.formatTime(event.get("log_time", None)), " "]) |
3280
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
193 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
|
194 else: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4078
diff
changeset
|
195 eventText = logger.eventAsText(event, includeSystem=True) |
3280
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
196 if not eventText: |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
197 return None |
96b9b65b4368
core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
198 return eventText.replace("\n", "\n\t") + "\n" |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
199 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
200 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
201 class ConfigureStandard(ConfigureBasic): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
202 def __init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
203 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
204 level=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
205 fmt=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
206 output=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
207 logger=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
208 colors=False, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
209 levels_taints_dict=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
210 force_colors=False, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
211 backend_data=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
212 ): |
1010
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
213 if fmt is None: |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
214 fmt = C.LOG_OPT_FORMAT[1] |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
215 if output is None: |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
216 output = C.LOG_OPT_OUTPUT[1] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
217 super(ConfigureStandard, self).__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
218 level, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
219 fmt, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
220 output, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
221 logger, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
222 colors, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
223 levels_taints_dict, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
224 force_colors, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
225 backend_data, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
226 ) |
991 | 227 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
228 def pre_treatment(self): |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
229 """We use logging methods directly, instead of using Logger""" |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
230 import logging |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
231 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
232 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
|
233 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
|
234 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
|
235 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
|
236 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
|
237 log.critical = logging.critical |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
238 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
239 def configure_level(self, level): |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
240 if level is None: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
241 level = C.LOG_LVL_DEBUG |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
242 self.level = level |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
243 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
244 def configure_format(self, fmt): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
245 super(ConfigureStandard, self).configure_format(fmt) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
246 import logging |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
247 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
248 class SatFormatter(logging.Formatter): |
3028 | 249 """Formatter which manage SàT specificities""" |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4078
diff
changeset
|
250 |
1012
c8771279497e
core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents:
1010
diff
changeset
|
251 _format = fmt |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
252 _with_profile = "%(profile)s" in fmt |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
253 |
1012
c8771279497e
core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents:
1010
diff
changeset
|
254 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
|
255 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
|
256 self.can_colors = can_colors |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
257 |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
258 def format(self, record): |
1013
11409a6c16c7
core (log/standard backend): added "%(profile)s" format management
Goffi <goffi@goffi.org>
parents:
1012
diff
changeset
|
259 if self._with_profile: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
260 record.profile = ConfigureStandard.get_profile() |
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
|
261 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
|
262 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
|
263 # we copy raw formatting strings for color_* |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
264 # as formatting is handled in ansi_colors in this case |
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
|
265 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
|
266 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
|
267 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
|
268 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
269 record.color_start = record.color_end = "" |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
270 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
|
271 if do_color: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
272 s = ConfigureStandard.ansi_colors(record.levelname, s) |
3056 | 273 return s |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
274 |
1012
c8771279497e
core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents:
1010
diff
changeset
|
275 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
|
276 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
277 def configure_output(self, output): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
278 self.manage_outputs(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
|
279 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
280 def configure_logger(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
|
281 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
|
282 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
283 def configure_colors(self, colors, force_colors, levels_taints_dict): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
284 super(ConfigureStandard, self).configure_colors( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
285 colors, force_colors, levels_taints_dict |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
286 ) |
1012
c8771279497e
core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents:
1010
diff
changeset
|
287 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
|
288 self.formatterClass.force_colors = force_colors |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
289 if not colors and force_colors: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
290 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
|
291 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
292 def _add_handler(self, root_logger, hdlr, can_colors=False): |
1012
c8771279497e
core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents:
1010
diff
changeset
|
293 hdlr.setFormatter(self.formatterClass(can_colors)) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
294 root_logger.addHandler(hdlr) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
295 root_logger.setLevel(self.level) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
296 if self.name_filter is not None: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
297 hdlr.addFilter(self.name_filter) |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
298 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
299 def post_treatment(self): |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
300 import logging |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
301 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
302 root_logger = logging.getLogger() |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
303 if len(root_logger.handlers) == 0: |
3028 | 304 for handler, options in list(log.handlers.items()): |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
305 if handler == C.LOG_OPT_OUTPUT_DEFAULT: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
306 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
|
307 try: |
c8771279497e
core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents:
1010
diff
changeset
|
308 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
|
309 except AttributeError: |
c8771279497e
core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents:
1010
diff
changeset
|
310 can_colors = False |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
311 self._add_handler(root_logger, hdlr, can_colors=can_colors) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
312 elif handler == C.LOG_OPT_OUTPUT_MEMORY: |
1010
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
313 from logging.handlers import BufferingHandler |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
314 |
1010
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
315 class SatMemoryHandler(BufferingHandler): |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
316 def emit(self, record): |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
317 super(SatMemoryHandler, self).emit(self.format(record)) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
318 |
1010
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
319 hdlr = SatMemoryHandler(options) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4078
diff
changeset
|
320 log.handlers[handler] = ( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4078
diff
changeset
|
321 hdlr # we keep a reference to the handler to read the buffer later |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4078
diff
changeset
|
322 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
323 self._add_handler(root_logger, hdlr, can_colors=False) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
324 elif handler == C.LOG_OPT_OUTPUT_FILE: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
325 import os.path |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
326 |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
327 for path in options: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
328 hdlr = logging.FileHandler(os.path.expanduser(path)) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
329 self._add_handler(root_logger, hdlr, can_colors=False) |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
330 else: |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
331 raise ValueError("Unknown handler type") |
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
332 else: |
3028 | 333 root_logger.warning("Handlers already set on root logger") |
1007
a7d33c7a8277
core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents:
1006
diff
changeset
|
334 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
335 @staticmethod |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
336 def memory_get(size=None): |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
337 """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
|
338 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
339 @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
|
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 mem_handler = log.handlers[C.LOG_OPT_OUTPUT_MEMORY] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
342 return ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
343 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
|
344 ) |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
345 |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
346 |
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
347 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
|
348 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
|
349 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
|
350 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
351 |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
352 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
|
353 """Configure logging behaviour |
991 | 354 @param backend: can be: |
355 C.LOG_BACKEND_STANDARD: use standard logging module | |
356 C.LOG_BACKEND_TWISTED: use twisted logging module (with standard logging observer) | |
357 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
|
358 C.LOG_BACKEND_CUSTOM: use a given Logger subclass |
991 | 359 """ |
1021
a836b6da2c5c
core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents:
1017
diff
changeset
|
360 return log.configure(backend, **options) |
991 | 361 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
362 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
363 def _parse_options(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
|
364 """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
|
365 |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
366 @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
|
367 """ |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
368 COLORS = C.LOG_OPT_COLORS[0] |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
369 LEVEL = C.LOG_OPT_LEVEL[0] |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
370 |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
371 if COLORS in options: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
372 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
|
373 options[COLORS] = True |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
374 elif options[COLORS] == "force": |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
375 options[COLORS] = True |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
376 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
|
377 else: |
1005
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
378 options[COLORS] = False |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
379 if LEVEL in options: |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
380 level = options[LEVEL].upper() |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
381 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
|
382 level = C.LOG_LVL_INFO |
b4af31a8a4f2
core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents:
994
diff
changeset
|
383 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
|
384 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
385 |
4078
15055a00162c
refactoring: rename `sat_configure` to `libervia_configure`
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
386 def libervia_configure(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
|
387 """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
|
388 |
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
389 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
|
390 @param backend: backend to use, it can be: |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
391 - C.LOG_BACKEND_BASIC: print based backend |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
392 - C.LOG_BACKEND_TWISTED: Twisted logging backend |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
393 - C.LOG_BACKEND_STANDARD: standard logging backend |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
394 @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
|
395 """ |
1010
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
396 if const is not None: |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
397 global C |
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
398 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
|
399 log.C = const |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
400 from libervia.backend.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
|
401 import os |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
402 |
994
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
403 log_conf = {} |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
404 sat_conf = config.parse_main_conf() |
1010
73a0b7f94674
primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents:
1008
diff
changeset
|
405 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
|
406 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
407 log_conf[opt_name] = os.environ[ |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
408 "".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
|
409 ] |
994
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
410 except KeyError: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
411 log_conf[opt_name] = config.config_get( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
412 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
|
413 ) |
994
652c01ca69b1
core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents:
992
diff
changeset
|
414 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3988
diff
changeset
|
415 _parse_options(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
|
416 configure(backend, backend_data=backend_data, **log_conf) |