annotate src/core/log.py @ 1848:446a1539c207

quick_frontend (blog): fixes main item update (comment's data were lost)
author souliane <souliane@mailoo.org>
date Mon, 22 Feb 2016 13:15:02 +0100
parents d17772b0fe22
children 2daf7b4c6756
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SàT: a XMPP client
1766
d17772b0fe22 copyright update
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """High level logging functions"""
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
21 # XXX: this module use standard logging module when possible, but as SàT can work in different cases where logging is not the best choice (twisted, pyjamas, etc), it is necessary to have a dedicated module. Additional feature like environment variables and colors are also managed.
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
22
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from sat.core.constants import Const as C
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from sat.core import exceptions
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
25
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
26 backend = None
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 _loggers = {}
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
28 handlers = {}
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
29
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
30
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
31 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
32 pass
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
33
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
34
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 class Logger(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
36 """High level logging class"""
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
37 fmt = None # format option as given by user (e.g. SAT_LOG_LOGGER)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
38 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
39 post_treat = None
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 def __init__(self, name):
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
42 if isinstance(name, Logger):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
43 self.copy(name)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
44 else:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
45 self._name = name
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
46
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
47 def copy(self, other):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
48 """Copy values from other Logger"""
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
49 self.fmt = other.fmt
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
50 self.Filter_name = other.fmt
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
51 self.post_treat = other.post_treat
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
52 self._name = other._name
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
53
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
54 def out(self, message, level=None):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
55 """Actually log the message
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
56
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
57 @param message: formatted message
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
58 """
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
59 print message
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
60
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
61 def log(self, 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
62 """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
63
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
64 @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
65 @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
66 """
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
67 try:
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
68 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
69 if self.post_treat is None:
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
70 self.out(formatted, level)
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
71 else:
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
72 self.out(self.post_treat(level, formatted), level)
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
73 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
74 pass
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
75
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
76 def format(self, 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
77 """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
78
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
79 @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
80 @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
81 @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
82
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
83 @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
84 """
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
85 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
86 return message
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
87 record = {'name': self._name,
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
88 'message': message,
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
89 'levelname': level,
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 try:
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
92 if not self.filter_name.dictFilter(record):
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
93 raise Filtered
1016
0c361fdc76af core (logs): workaround for pyjamas bug
Goffi <goffi@goffi.org>
parents: 1013
diff changeset
94 except (AttributeError, TypeError): # 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
95 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
96 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
97 try:
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
98 return self.fmt % record
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
99 except TypeError:
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
100 return message
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
101 except KeyError as e:
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
102 if e.args[0] == 'profile':
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
103 # XXX: %(profile)s use some magic with introspection, for debugging purpose only *DO NOT* use in production
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
104 record['profile'] = configure_cls[backend].getProfile()
1008
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
105 return self.fmt % record
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
106 else:
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
107 raise e
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
108
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 def debug(self, msg):
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
110 self.log(C.LOG_LVL_DEBUG, msg)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
111
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 def info(self, msg):
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 self.log(C.LOG_LVL_INFO, msg)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
114
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 def warning(self, msg):
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
116 self.log(C.LOG_LVL_WARNING, msg)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
117
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 def error(self, msg):
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
119 self.log(C.LOG_LVL_ERROR, msg)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
120
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 def critical(self, msg):
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
122 self.log(C.LOG_LVL_CRITICAL, msg)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
123
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
124
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
125 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
126 """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
127
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
128 def __init__(self, name_re):
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
129 """Initialise name filter
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
130
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
131 @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
132 """
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
133 assert name_re
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
134 import re
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
135 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
136
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
137 def filter(self, record):
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
138 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
139 return 1
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
140 return 0
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
141
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
142 def dictFilter(self, dict_record):
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
143 """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
144
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
145 @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
146 @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
147 """
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
148 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
149 pass
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
150 log_record = LogRecord()
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
151 log_record.name = dict_record['name']
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
152 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
153
1010
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
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 class ConfigureBase(object):
1017
0ea97f483464 core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents: 1016
diff changeset
156 LOGGER_CLASS = Logger
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
157
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
158 def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, force_colors=False, backend_data=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
159 """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
160
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
161 @param level: one of C.LOG_LEVELS
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
162 @param fmt: format string, pretty much as in std logging. Accept the following keywords (maybe more depending on backend):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
163 - "message"
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
164 - "levelname"
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
165 - "name" (logger name)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
166 @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
167 Use search to match expression, so ^ or $ can be necessary.
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
168 @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
169 @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
170 """
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
171 self.backend_data = backend_data
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
172 self.preTreatment()
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
173 self.configureLevel(level)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
174 self.configureFormat(fmt)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
175 self.configureOutput(output)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
176 self.configureLogger(logger)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
177 self.configureColors(colors, force_colors)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
178 self.postTreatment()
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
179 self.updateCurrentLogger()
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
180
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
181 def updateCurrentLogger(self):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
182 """update existing logger to the class needed for this backend"""
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
183 if self.LOGGER_CLASS is None:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
184 return
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
185 for name, logger in _loggers.items():
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
186 _loggers[name] = self.LOGGER_CLASS(logger)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
187
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
188 def preTreatment(self):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
189 pass
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
190
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
191 def configureLevel(self, level):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
192 if level is not None:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
193 # we deactivate methods below level
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
194 level_idx = C.LOG_LEVELS.index(level)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
195 def dev_null(self, msg):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
196 pass
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
197 for _level in C.LOG_LEVELS[:level_idx]:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
198 setattr(Logger, _level.lower(), dev_null)
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 def configureFormat(self, fmt):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
201 if fmt is not None:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
202 if fmt != '%(message)s': # %(message)s is the same as None
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
203 Logger.fmt = fmt
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
204
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
205 def configureOutput(self, output):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
206 if output is not None:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
207 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
208 # TODO: manage other outputs
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
209 raise NotImplementedError("Basic backend only manage default output yet")
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
210
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
211 def configureLogger(self, logger):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
212 if logger:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
213 Logger.filter_name = FilterName(logger)
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
214
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
215 def configureColors(self, colors, force_colors):
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
216 pass
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
217
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
218 def postTreatment(self):
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
219 pass
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
220
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
221 def manageOutputs(self, 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
222 """ Parse output option in a backend agnostic way, and fill handlers consequently
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
223
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
224 @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
225 """
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
226 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
227 return
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
228 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
229 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
230 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
231 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
232
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
233 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
234 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
235 continue
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
236 if output[-1] == ')':
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
237 # we have options
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
238 opt_begin = output.rfind('(')
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
239 options = output[opt_begin+1:-1]
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
240 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
241 else:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
242 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
243
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
244 if output not in (C.LOG_OPT_OUTPUT_DEFAULT, C.LOG_OPT_OUTPUT_FILE, 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
245 raise ValueError(u"Invalid output [%s]" % output)
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
246
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
247 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
248 # 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
249 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
250 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
251 if not options:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
252 ValueError("%(handler)s output need a path as option" % {'handler': output})
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
253 handlers.setdefault(output, []).append(options)
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
254 options = None # option are parsed, we can empty them
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
255 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
256 # 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
257 try:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
258 limit = int(options)
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
259 options = None # option are parsed, we can empty them
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
260 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
261 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
262 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
263
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
264 if options: # we should not have unparsed options
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
265 raise ValueError(u"options [%(options)s] are not supported for %(handler)s output" % {'options': options, 'handler': output})
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
266
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
267 @staticmethod
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
268 def memoryGet(size=None):
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
269 """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
270
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
271 @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
272 """
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
273 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
274
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
275 @staticmethod
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
276 def ansiColors(level, message):
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
277 """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
278
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
279 @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
280 @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
281 @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
282 """
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
283 if level == C.LOG_LVL_DEBUG:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
284 out = (C.ANSI_FG_CYAN, message, C.ANSI_RESET)
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
285 elif level == C.LOG_LVL_WARNING:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
286 out = (C.ANSI_FG_YELLOW, message, C.ANSI_RESET)
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
287 elif level == C.LOG_LVL_ERROR:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
288 out = (C.ANSI_FG_RED,
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
289 C.ANSI_BLINK,
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
290 r'/!\ ',
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
291 C.ANSI_BLINK_OFF,
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
292 message,
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
293 C.ANSI_RESET)
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
294 elif level == C.LOG_LVL_CRITICAL:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
295 out = (C.ANSI_BOLD,
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
296 C.ANSI_FG_RED,
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
297 'Guru Meditation ',
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
298 C.ANSI_NORMAL_WEIGHT,
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
299 message,
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
300 C.ANSI_RESET)
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
301 else:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
302 out = message
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
303 return ''.join(out)
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
304
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
305 @staticmethod
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
306 def getProfile():
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
307 """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
308 raise NotImplementedError
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
309
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
310
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
311 class ConfigureCustom(ConfigureBase):
1017
0ea97f483464 core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents: 1016
diff changeset
312 LOGGER_CLASS = None
0ea97f483464 core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents: 1016
diff changeset
313
0ea97f483464 core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents: 1016
diff changeset
314 def __init__(self, logger_class, *args, **kwargs):
0ea97f483464 core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents: 1016
diff changeset
315 ConfigureCustom.LOGGER_CLASS = logger_class
0ea97f483464 core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents: 1016
diff changeset
316
0ea97f483464 core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents: 1016
diff changeset
317
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
318 configure_cls = { None: ConfigureBase,
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
319 C.LOG_BACKEND_CUSTOM: ConfigureCustom
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
320 } # 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
321
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
322
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
323 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
324 """Configure logging behaviour
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
325 @param backend: can be:
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
326 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
327 C.LOG_BACKEND_CUSTOM: use a given Logger subclass
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
328 """
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
329 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
330 if backend is not None:
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
331 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
332 backend = backend_
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
333
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
334 try:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
335 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
336 except KeyError:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
337 raise ValueError("unknown backend [%s]" % backend)
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
338 if backend == C.LOG_BACKEND_CUSTOM:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
339 logger_class = options.pop('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
340 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
341 else:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
342 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
343
1021
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
344 def memoryGet(size=None):
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
345 if not C.LOG_OPT_OUTPUT_MEMORY in handlers:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
346 raise ValueError('memory output is not used')
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
347 return configure_cls[backend].memoryGet(size)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
348
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
349 def getLogger(name=C.LOG_BASE_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
350 try:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
351 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
352 except KeyError:
a836b6da2c5c core (log): moved configuration to core.log_config; this avoid import issues with pyjamas.
Goffi <goffi@goffi.org>
parents: 1017
diff changeset
353 raise ValueError("This method should not be called with backend [%s]" % backend)
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
354 return _loggers.setdefault(name, logger_class(name))
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
355
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
356 _root_logger = getLogger()
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
357
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
358 def debug(msg):
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
359 _root_logger.debug(msg)
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
360
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
361 def info(msg):
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
362 _root_logger.info(msg)
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
363
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
364 def warning(msg):
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
365 _root_logger.warning(msg)
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
366
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
367 def error(msg):
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
368 _root_logger.error(msg)
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
369
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
370 def critical(msg):
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
371 _root_logger.critical(msg)