annotate libervia/backend/core/log.py @ 4332:71c939e34ca6

XEP-0373 (OX): Adjust to gpgme updates: generate with explicit algorithm and subkeys
author Syndace <me@syndace.dev>
date Sat, 13 Jul 2024 18:28:28 +0200
parents 0d7bb4df2343
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2836
diff changeset
1 #!/usr/bin/env python3
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3280
diff changeset
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """High level logging functions"""
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
21 # XXX: this module use standard logging module when possible, but as SàT can work in different cases where logging is not the best choice (twisted, pyjamas, etc), it is necessary to have a dedicated module. Additional feature like environment variables and colors are also managed.
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
22 # TODO: change formatting from "%s" style to "{}" when moved to Python 3
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
23
3997
1b7c6ee080b9 core (log): type hints
Goffi <goffi@goffi.org>
parents: 3988
diff changeset
24 from typing import TYPE_CHECKING, Any, Optional, Dict
3911
8289ac1b34f4 plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents: 3480
diff changeset
25
8289ac1b34f4 plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents: 3480
diff changeset
26 if TYPE_CHECKING:
8289ac1b34f4 plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents: 3480
diff changeset
27 from logging import _ExcInfoType
8289ac1b34f4 plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents: 3480
diff changeset
28 else:
8289ac1b34f4 plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents: 3480
diff changeset
29 _ExcInfoType = Any
8289ac1b34f4 plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents: 3480
diff changeset
30
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
31 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
32 from libervia.backend.tools.common.ansi import ANSI as A
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
33 from libervia.backend.core import exceptions
2836
ad00f61fd9f5 core (log): add traceback when "exc_info" is set
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
34 import traceback
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
35
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
36 backend = None
3997
1b7c6ee080b9 core (log): type hints
Goffi <goffi@goffi.org>
parents: 3988
diff changeset
37 _loggers: Dict[str, "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
38 handlers = {}
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
39 COLOR_START = "%(color_start)s"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
40 COLOR_END = "%(color_end)s"
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
41
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
42
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
43 class Filtered(Exception):
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
44 pass
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
45
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
46
3280
96b9b65b4368 core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
47 class Logger:
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
48 """High level logging class"""
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
49
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
50 fmt = None # format option as given by user (e.g. SAT_LOG_LOGGER)
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
51 filter_name = None # filter to call
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
52 post_treat = None
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
53
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 def __init__(self, name):
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
55 if isinstance(name, Logger):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
56 self.copy(name)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
57 else:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
58 self._name = name
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
59
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
60 def copy(self, other):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
61 """Copy values from other Logger"""
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
62 self.fmt = other.fmt
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
63 self.Filter_name = other.fmt
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
64 self.post_treat = other.post_treat
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
65 self._name = other._name
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
66
3988
760f563b1243 core (log): `log.exception` is now usable
Goffi <goffi@goffi.org>
parents: 3911
diff changeset
67 def add_traceback(self, message):
2836
ad00f61fd9f5 core (log): add traceback when "exc_info" is set
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
68 tb = traceback.format_exc()
ad00f61fd9f5 core (log): add traceback when "exc_info" is set
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
69 return message + "\n==== traceback ====\n" + tb
ad00f61fd9f5 core (log): add traceback when "exc_info" is set
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
70
3988
760f563b1243 core (log): `log.exception` is now usable
Goffi <goffi@goffi.org>
parents: 3911
diff changeset
71 def out(
760f563b1243 core (log): `log.exception` is now usable
Goffi <goffi@goffi.org>
parents: 3911
diff changeset
72 self,
760f563b1243 core (log): `log.exception` is now usable
Goffi <goffi@goffi.org>
parents: 3911
diff changeset
73 message: object,
760f563b1243 core (log): `log.exception` is now usable
Goffi <goffi@goffi.org>
parents: 3911
diff changeset
74 level: Optional[str] = None,
760f563b1243 core (log): `log.exception` is now usable
Goffi <goffi@goffi.org>
parents: 3911
diff changeset
75 exc_info: _ExcInfoType = False,
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
76 **kwargs,
3988
760f563b1243 core (log): `log.exception` is now usable
Goffi <goffi@goffi.org>
parents: 3911
diff changeset
77 ) -> None:
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
78 """Actually log the message
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
79
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
80 @param message: formatted message
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
81 """
3988
760f563b1243 core (log): `log.exception` is now usable
Goffi <goffi@goffi.org>
parents: 3911
diff changeset
82 if exc_info:
760f563b1243 core (log): `log.exception` is now usable
Goffi <goffi@goffi.org>
parents: 3911
diff changeset
83 message = self.add_traceback(message)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2836
diff changeset
84 print(message)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
85
3988
760f563b1243 core (log): `log.exception` is now usable
Goffi <goffi@goffi.org>
parents: 3911
diff changeset
86 def log(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
87 self, level: str, message: object, exc_info: _ExcInfoType = False, **kwargs
3988
760f563b1243 core (log): `log.exception` is now usable
Goffi <goffi@goffi.org>
parents: 3911
diff changeset
88 ) -> None:
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
89 """Print message
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
90
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
91 @param level: one of C.LOG_LEVELS
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
92 @param message: message to format and print
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
93 """
3988
760f563b1243 core (log): `log.exception` is now usable
Goffi <goffi@goffi.org>
parents: 3911
diff changeset
94 if exc_info:
760f563b1243 core (log): `log.exception` is now usable
Goffi <goffi@goffi.org>
parents: 3911
diff changeset
95 message = self.add_traceback(message)
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
96 try:
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
97 formatted = self.format(level, message)
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
98 if self.post_treat is None:
2836
ad00f61fd9f5 core (log): add traceback when "exc_info" is set
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
99 self.out(formatted, level, **kwargs)
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
100 else:
2836
ad00f61fd9f5 core (log): add traceback when "exc_info" is set
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
101 self.out(self.post_treat(level, formatted), level, **kwargs)
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
102 except Filtered:
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
103 pass
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
104
3911
8289ac1b34f4 plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents: 3480
diff changeset
105 def format(self, level: str, message: object) -> object:
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
106 """Format message according to Logger.fmt
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
107
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
108 @param level: one of C.LOG_LEVELS
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
109 @param message: message to format
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
110 @return: formatted message
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
111
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
112 @raise: Filtered when the message must not be logged
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
113 """
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
114 if self.fmt is None and self.filter_name is None:
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
115 return message
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
116 record = {
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
117 "name": self._name,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
118 "message": message,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
119 "levelname": level,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
120 }
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
121 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3997
diff changeset
122 if not self.filter_name.dict_filter(record):
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
123 raise Filtered
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
124 except (
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
125 AttributeError,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
126 TypeError,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
127 ): # XXX: TypeError is here because of a pyjamas bug which need to be fixed (TypeError is raised instead of AttributeError)
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
128 if self.filter_name is not None:
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
129 raise ValueError("Bad filter: filters must have a .filter method")
1008
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
130 try:
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
131 return self.fmt % record
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
132 except TypeError:
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
133 return message
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
134 except KeyError as e:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
135 if e.args[0] == "profile":
1008
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
136 # XXX: %(profile)s use some magic with introspection, for debugging purpose only *DO NOT* use in production
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
137 record["profile"] = configure_cls[backend].get_profile()
1008
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
138 return self.fmt % record
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
139 else:
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
140 raise e
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
141
3988
760f563b1243 core (log): `log.exception` is now usable
Goffi <goffi@goffi.org>
parents: 3911
diff changeset
142 def debug(self, msg: object, **kwargs) -> None:
2836
ad00f61fd9f5 core (log): add traceback when "exc_info" is set
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
143 self.log(C.LOG_LVL_DEBUG, msg, **kwargs)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
144
3988
760f563b1243 core (log): `log.exception` is now usable
Goffi <goffi@goffi.org>
parents: 3911
diff changeset
145 def info(self, msg: object, **kwargs) -> None:
2836
ad00f61fd9f5 core (log): add traceback when "exc_info" is set
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
146 self.log(C.LOG_LVL_INFO, msg, **kwargs)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
147
3988
760f563b1243 core (log): `log.exception` is now usable
Goffi <goffi@goffi.org>
parents: 3911
diff changeset
148 def warning(self, msg: object, **kwargs) -> None:
2836
ad00f61fd9f5 core (log): add traceback when "exc_info" is set
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
149 self.log(C.LOG_LVL_WARNING, msg, **kwargs)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
150
3988
760f563b1243 core (log): `log.exception` is now usable
Goffi <goffi@goffi.org>
parents: 3911
diff changeset
151 def error(self, msg: object, **kwargs) -> None:
2836
ad00f61fd9f5 core (log): add traceback when "exc_info" is set
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
152 self.log(C.LOG_LVL_ERROR, msg, **kwargs)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
153
3988
760f563b1243 core (log): `log.exception` is now usable
Goffi <goffi@goffi.org>
parents: 3911
diff changeset
154 def critical(self, msg: object, **kwargs) -> None:
2836
ad00f61fd9f5 core (log): add traceback when "exc_info" is set
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
155 self.log(C.LOG_LVL_CRITICAL, msg, **kwargs)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
156
3988
760f563b1243 core (log): `log.exception` is now usable
Goffi <goffi@goffi.org>
parents: 3911
diff changeset
157 def exception(self, msg: object, exc_info=True, **kwargs) -> None:
760f563b1243 core (log): `log.exception` is now usable
Goffi <goffi@goffi.org>
parents: 3911
diff changeset
158 self.log(C.LOG_LVL_ERROR, msg, exc_info=exc_info, **kwargs)
760f563b1243 core (log): `log.exception` is now usable
Goffi <goffi@goffi.org>
parents: 3911
diff changeset
159
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
160
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
161 class FilterName(object):
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
162 """Filter on logger name according to a regex"""
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
163
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
164 def __init__(self, name_re):
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
165 """Initialise name filter
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
166
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
167 @param name_re: regular expression used to filter names (using search and not match)
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
168 """
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
169 assert name_re
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
170 import re
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
171
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
172 self.name_re = re.compile(name_re)
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
173
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
174 def filter(self, record):
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
175 if self.name_re.search(record.name) is not None:
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
176 return 1
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
177 return 0
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
178
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3997
diff changeset
179 def dict_filter(self, dict_record):
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
180 """Filter using a dictionary record
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
181
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
182 @param dict_record: dictionary with at list a key "name" with logger name
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
183 @return: True if message should be logged
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
184 """
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
185
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
186 class LogRecord(object):
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
187 pass
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
188
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
189 log_record = LogRecord()
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
190 log_record.name = dict_record["name"]
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
191 return self.filter(log_record) == 1
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
192
1010
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
193
3280
96b9b65b4368 core (log): logging with Twisted now uses the new twisted.logger
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
194 class ConfigureBase:
1017
0ea97f483464 core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents: 1016
diff changeset
195 LOGGER_CLASS = Logger
2671
0fa217fafabf tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
196 # True if color location is specified in fmt (with COLOR_START)
0fa217fafabf tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
197 _color_location = False
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
198
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
199 def __init__(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
200 self,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
201 level=None,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
202 fmt=None,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
203 output=None,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
204 logger=None,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
205 colors=False,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
206 levels_taints_dict=None,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
207 force_colors=False,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
208 backend_data=None,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
209 ):
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
210 """Configure a backend
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
211
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
212 @param level: one of C.LOG_LEVELS
2671
0fa217fafabf tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
213 @param fmt: format string, pretty much as in std logging.
0fa217fafabf tools (common/template), jp: refactoring to handle multiple sites:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
214 Accept the following keywords (maybe more depending on backend):
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
215 - "message"
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
216 - "levelname"
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
217 - "name" (logger name)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
218 @param logger: if set, use it as a regular expression to filter on logger name.
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
219 Use search to match expression, so ^ or $ can be necessary.
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
220 @param colors: if True use ANSI colors to show log levels
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
221 @param force_colors: if True ANSI colors are used even if stdout is not a tty
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
222 """
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: 1021
diff changeset
223 self.backend_data = backend_data
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3997
diff changeset
224 self.pre_treatment()
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3997
diff changeset
225 self.configure_level(level)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3997
diff changeset
226 self.configure_format(fmt)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3997
diff changeset
227 self.configure_output(output)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3997
diff changeset
228 self.configure_logger(logger)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3997
diff changeset
229 self.configure_colors(colors, force_colors, levels_taints_dict)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3997
diff changeset
230 self.post_treatment()
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3997
diff changeset
231 self.update_current_logger()
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
232
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3997
diff changeset
233 def update_current_logger(self):
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
234 """update existing logger to the class needed for this backend"""
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
235 if self.LOGGER_CLASS is None:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
236 return
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2836
diff changeset
237 for name, logger in list(_loggers.items()):
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
238 _loggers[name] = self.LOGGER_CLASS(logger)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
239
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3997
diff changeset
240 def pre_treatment(self):
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
241 pass
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
242
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3997
diff changeset
243 def configure_level(self, level):
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
244 if level is not None:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
245 # we deactivate methods below level
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
246 level_idx = C.LOG_LEVELS.index(level)
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
247
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
248 def dev_null(self, msg):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
249 pass
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
250
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
251 for _level in C.LOG_LEVELS[:level_idx]:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
252 setattr(Logger, _level.lower(), dev_null)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
253
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3997
diff changeset
254 def configure_format(self, fmt):
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
255 if fmt is not None:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
256 if fmt != "%(message)s": # %(message)s is the same as None
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
257 Logger.fmt = 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
258 if COLOR_START in fmt:
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 ConfigureBase._color_location = True
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
260 if fmt.find(COLOR_END, fmt.rfind(COLOR_START)) < 0:
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
261 # color_start not followed by an end, we add it
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
262 Logger.fmt += COLOR_END
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
263
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3997
diff changeset
264 def configure_output(self, output):
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
265 if output is not None:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
266 if 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
267 # TODO: manage other outputs
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
268 raise NotImplementedError("Basic backend only manage default output yet")
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
269
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3997
diff changeset
270 def configure_logger(self, logger):
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
271 if logger:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
272 Logger.filter_name = FilterName(logger)
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
273
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3997
diff changeset
274 def configure_colors(self, colors, force_colors, levels_taints_dict):
1942
7f053e1f0b67 core (logs): taints:
Goffi <goffi@goffi.org>
parents: 1940
diff changeset
275 if colors:
7f053e1f0b67 core (logs): taints:
Goffi <goffi@goffi.org>
parents: 1940
diff changeset
276 # if color are used, we need to handle levels_taints_dict
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2836
diff changeset
277 for level in list(levels_taints_dict.keys()):
1942
7f053e1f0b67 core (logs): taints:
Goffi <goffi@goffi.org>
parents: 1940
diff changeset
278 # we wants levels in uppercase to correspond to contstants
7f053e1f0b67 core (logs): taints:
Goffi <goffi@goffi.org>
parents: 1940
diff changeset
279 levels_taints_dict[level.upper()] = levels_taints_dict[level]
7f053e1f0b67 core (logs): taints:
Goffi <goffi@goffi.org>
parents: 1940
diff changeset
280 taints = self.__class__.taints = {}
7f053e1f0b67 core (logs): taints:
Goffi <goffi@goffi.org>
parents: 1940
diff changeset
281 for level in C.LOG_LEVELS:
7f053e1f0b67 core (logs): taints:
Goffi <goffi@goffi.org>
parents: 1940
diff changeset
282 # we want use values and use constant value as default
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
283 taint_list = levels_taints_dict.get(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
284 level, C.LOG_OPT_TAINTS_DICT[1][level]
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
285 )
1942
7f053e1f0b67 core (logs): taints:
Goffi <goffi@goffi.org>
parents: 1940
diff changeset
286 ansi_list = []
7f053e1f0b67 core (logs): taints:
Goffi <goffi@goffi.org>
parents: 1940
diff changeset
287 for elt in taint_list:
7f053e1f0b67 core (logs): taints:
Goffi <goffi@goffi.org>
parents: 1940
diff changeset
288 elt = elt.upper()
7f053e1f0b67 core (logs): taints:
Goffi <goffi@goffi.org>
parents: 1940
diff changeset
289 try:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
290 ansi = getattr(A, "FG_{}".format(elt))
1942
7f053e1f0b67 core (logs): taints:
Goffi <goffi@goffi.org>
parents: 1940
diff changeset
291 except AttributeError:
7f053e1f0b67 core (logs): taints:
Goffi <goffi@goffi.org>
parents: 1940
diff changeset
292 try:
2154
7cbffd754b4a core (constants), tools (common/ansi): moved ANSI escape codes to a dedicated class, with helper methods
Goffi <goffi@goffi.org>
parents: 1942
diff changeset
293 ansi = getattr(A, elt)
1942
7f053e1f0b67 core (logs): taints:
Goffi <goffi@goffi.org>
parents: 1940
diff changeset
294 except AttributeError:
7f053e1f0b67 core (logs): taints:
Goffi <goffi@goffi.org>
parents: 1940
diff changeset
295 # we use raw string if element is unknown
7f053e1f0b67 core (logs): taints:
Goffi <goffi@goffi.org>
parents: 1940
diff changeset
296 ansi = elt
7f053e1f0b67 core (logs): taints:
Goffi <goffi@goffi.org>
parents: 1940
diff changeset
297 ansi_list.append(ansi)
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
298 taints[level] = "".join(ansi_list)
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
299
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3997
diff changeset
300 def post_treatment(self):
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
301 pass
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
302
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3997
diff changeset
303 def manage_outputs(self, outputs_raw):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
304 """Parse output option in a backend agnostic way, and fill handlers consequently
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
305
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
306 @param outputs_raw: output option as enterred in environment variable or in configuration
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
307 """
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
308 if not outputs_raw:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
309 return
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
310 outputs = outputs_raw.split(C.LOG_OPT_OUTPUT_SEP)
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
311 global handlers
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
312 if len(outputs) == 1:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
313 handlers[C.LOG_OPT_OUTPUT_FILE] = [outputs.pop()]
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
314
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
315 for output in outputs:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
316 if not output:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
317 continue
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
318 if output[-1] == ")":
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
319 # we have options
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
320 opt_begin = output.rfind("(")
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
321 options = output[opt_begin + 1 : -1]
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
322 output = output[:opt_begin]
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
323 else:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
324 options = None
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
325
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
326 if output not in (
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
327 C.LOG_OPT_OUTPUT_DEFAULT,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
328 C.LOG_OPT_OUTPUT_FILE,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
329 C.LOG_OPT_OUTPUT_MEMORY,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
330 ):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2836
diff changeset
331 raise ValueError("Invalid output [%s]" % output)
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
332
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
333 if output == C.LOG_OPT_OUTPUT_DEFAULT:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
334 # no option for defaut handler
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
335 handlers[output] = None
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
336 elif output == C.LOG_OPT_OUTPUT_FILE:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
337 if not options:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
338 ValueError(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
339 "{handler} output need a path as option".format(handle=output)
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
340 )
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
341 handlers.setdefault(output, []).append(options)
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
342 options = None # option are parsed, we can empty them
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
343 elif output == C.LOG_OPT_OUTPUT_MEMORY:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
344 # we have memory handler, option can be the len limit or None
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
345 try:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
346 limit = int(options)
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
347 options = None # option are parsed, we can empty them
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
348 except (TypeError, ValueError):
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
349 limit = C.LOG_OPT_OUTPUT_MEMORY_LIMIT
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
350 handlers[output] = limit
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
351
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
352 if options: # we should not have unparsed options
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
353 raise ValueError(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
354 "options [{options}] are not supported for {handler} output".format(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
355 options=options, handler=output
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
356 )
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
357 )
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
358
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
359 @staticmethod
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3997
diff changeset
360 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
361 """Return buffered logs
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
362
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
363 @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
364 """
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
365 raise NotImplementedError
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
366
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
367 @classmethod
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3997
diff changeset
368 def ansi_colors(cls, level, message):
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
369 """Colorise message depending on level for terminals
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
370
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
371 @param level: one of C.LOG_LEVELS
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
372 @param message: formatted message to log
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
373 @return: message with ANSI escape codes for coloration
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
374 """
1942
7f053e1f0b67 core (logs): taints:
Goffi <goffi@goffi.org>
parents: 1940
diff changeset
375
7f053e1f0b67 core (logs): taints:
Goffi <goffi@goffi.org>
parents: 1940
diff changeset
376 try:
7f053e1f0b67 core (logs): taints:
Goffi <goffi@goffi.org>
parents: 1940
diff changeset
377 start = cls.taints[level]
7f053e1f0b67 core (logs): taints:
Goffi <goffi@goffi.org>
parents: 1940
diff changeset
378 except KeyError:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
379 start = ""
1942
7f053e1f0b67 core (logs): taints:
Goffi <goffi@goffi.org>
parents: 1940
diff changeset
380
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
381 if cls._color_location:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
382 return message % {"color_start": start, "color_end": A.RESET}
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
383 else:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
384 return "%s%s%s" % (start, message, A.RESET)
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
385
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
386 @staticmethod
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3997
diff changeset
387 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
388 """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
389 raise NotImplementedError
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
390
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
391
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
392 class ConfigureCustom(ConfigureBase):
1017
0ea97f483464 core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents: 1016
diff changeset
393 LOGGER_CLASS = None
0ea97f483464 core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents: 1016
diff changeset
394
0ea97f483464 core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents: 1016
diff changeset
395 def __init__(self, logger_class, *args, **kwargs):
0ea97f483464 core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents: 1016
diff changeset
396 ConfigureCustom.LOGGER_CLASS = logger_class
0ea97f483464 core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents: 1016
diff changeset
397
0ea97f483464 core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents: 1016
diff changeset
398
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
399 configure_cls = {
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
400 None: ConfigureBase,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
401 C.LOG_BACKEND_CUSTOM: ConfigureCustom,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
402 } # XXX: (key: backend, value: Configure subclass) must be filled when new backend are added
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
403
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
404
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
405 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
406 """Configure logging behaviour
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
407 @param backend: can be:
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
408 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
409 C.LOG_BACKEND_CUSTOM: use a given Logger subclass
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
410 """
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
411 global backend
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
412 if backend is not None:
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
413 raise exceptions.InternalError("Logging can only be configured once")
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
414 backend = backend_
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
415
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
416 try:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
417 configure_class = configure_cls[backend]
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
418 except KeyError:
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
419 raise ValueError("unknown backend [{}]".format(backend))
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
420 if backend == C.LOG_BACKEND_CUSTOM:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
421 logger_class = options.pop("logger_class")
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
422 configure_class(logger_class, **options)
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
423 else:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
424 configure_class(**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
425
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
426
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3997
diff changeset
427 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
428 if not C.LOG_OPT_OUTPUT_MEMORY in handlers:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
429 raise ValueError("memory output is not used")
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3997
diff changeset
430 return configure_cls[backend].memory_get(size)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
431
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
432
3997
1b7c6ee080b9 core (log): type hints
Goffi <goffi@goffi.org>
parents: 3988
diff changeset
433 def getLogger(name=C.LOG_BASE_LOGGER) -> 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
434 try:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
435 logger_class = configure_cls[backend].LOGGER_CLASS
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
436 except KeyError:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
437 raise ValueError(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
438 "This method should not be called with backend [{}]".format(backend)
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
439 )
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
440 return _loggers.setdefault(name, logger_class(name))
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
441
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
442
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
443 _root_logger = getLogger()
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
444
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
445
2836
ad00f61fd9f5 core (log): add traceback when "exc_info" is set
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
446 def debug(msg, **kwargs):
ad00f61fd9f5 core (log): add traceback when "exc_info" is set
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
447 _root_logger.debug(msg, **kwargs)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
448
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
449
2836
ad00f61fd9f5 core (log): add traceback when "exc_info" is set
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
450 def info(msg, **kwargs):
ad00f61fd9f5 core (log): add traceback when "exc_info" is set
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
451 _root_logger.info(msg, **kwargs)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
452
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
453
2836
ad00f61fd9f5 core (log): add traceback when "exc_info" is set
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
454 def warning(msg, **kwargs):
ad00f61fd9f5 core (log): add traceback when "exc_info" is set
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
455 _root_logger.warning(msg, **kwargs)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
456
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
457
2836
ad00f61fd9f5 core (log): add traceback when "exc_info" is set
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
458 def error(msg, **kwargs):
ad00f61fd9f5 core (log): add traceback when "exc_info" is set
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
459 _root_logger.error(msg, **kwargs)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
460
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
461
2836
ad00f61fd9f5 core (log): add traceback when "exc_info" is set
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
462 def critical(msg, **kwargs):
ad00f61fd9f5 core (log): add traceback when "exc_info" is set
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
463 _root_logger.critical(msg, **kwargs)