annotate src/core/log.py @ 1017:0ea97f483464

core (log): added "custom" backend
author Goffi <goffi@goffi.org>
date Thu, 08 May 2014 19:11:20 +0200
parents 0c361fdc76af
children a836b6da2c5c
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
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org)
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
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 _backend = None
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 _loggers = {}
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
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
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
104 record['profile'] = _getProfile()
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
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
109
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 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
111 self.log(C.LOG_LVL_DEBUG, msg)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
112
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 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
114 self.log(C.LOG_LVL_INFO, msg)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
115
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 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
117 self.log(C.LOG_LVL_WARNING, msg)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
118
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 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
120 self.log(C.LOG_LVL_ERROR, msg)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
121
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 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
123 self.log(C.LOG_LVL_CRITICAL, msg)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
124
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
125
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
126 class TwistedLogger(Logger):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
127 colors = True
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
128 force_colors = False
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
129
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
130 def __init__(self, *args, **kwargs):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
131 super(TwistedLogger, self).__init__(*args, **kwargs)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
132 from twisted.python import log
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
133 self.twisted_log = log
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
134
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
135 def out(self, message, level=None):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
136 """Actually log the message
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
137
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
138 @param message: formatted message
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
139 """
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
140 self.twisted_log.msg(message.encode('utf-8', 'ignore'), sat_logged=True, level=level)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
141
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
142
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
143 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
144 """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
145
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
146 def __init__(self, name_re):
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
147 """Initialise name filter
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
148
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
149 @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
150 """
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
151 assert name_re
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
152 import re
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
153 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
154
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
155 def filter(self, record):
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
156 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
157 return 1
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
158 return 0
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
159
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
160 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
161 """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
162
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
163 @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
164 @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
165 """
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
166 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
167 pass
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
168 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
169 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
170 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
171
1010
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
172 def memoryGet(size=None):
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
173 """Return buffered logs
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
174
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
175 @param size: number of logs to return
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
176 """
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
177 if not C.LOG_OPT_OUTPUT_MEMORY in _handlers:
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
178 raise ValueError('memory output is not used')
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
179 if _backend == C.LOG_BACKEND_STANDARD:
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
180 mem_handler = _handlers[C.LOG_OPT_OUTPUT_MEMORY]
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
181 return (log_msg for log_msg in mem_handler.buffer[size if size is None else -size:])
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
182 else:
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
183 raise NotImplementedError
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
184
1008
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
185 def _getProfile():
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
186 """Try to find profile value using introspection"""
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
187 import inspect
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
188 stack = inspect.stack()
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
189 current_path = stack[0][1]
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
190 for frame_data in stack[:-1]:
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
191 if frame_data[1] != current_path:
1013
11409a6c16c7 core (log/standard backend): added "%(profile)s" format management
Goffi <goffi@goffi.org>
parents: 1012
diff changeset
192 if _backend == C.LOG_BACKEND_STANDARD and "/logging/__init__.py" in frame_data[1]:
11409a6c16c7 core (log/standard backend): added "%(profile)s" format management
Goffi <goffi@goffi.org>
parents: 1012
diff changeset
193 continue
1008
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
194 break
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
195
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
196 frame = frame_data[0]
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
197 args = inspect.getargvalues(frame)
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
198 try:
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
199 profile = args.locals.get('profile') or args.locals['profile_key']
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
200 except (TypeError, KeyError):
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
201 try:
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
202 try:
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
203 profile = args.locals['self'].profile
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
204 except AttributeError:
1013
11409a6c16c7 core (log/standard backend): added "%(profile)s" format management
Goffi <goffi@goffi.org>
parents: 1012
diff changeset
205 try:
11409a6c16c7 core (log/standard backend): added "%(profile)s" format management
Goffi <goffi@goffi.org>
parents: 1012
diff changeset
206 profile = args.locals['self'].parent.profile
11409a6c16c7 core (log/standard backend): added "%(profile)s" format management
Goffi <goffi@goffi.org>
parents: 1012
diff changeset
207 except AttributeError:
11409a6c16c7 core (log/standard backend): added "%(profile)s" format management
Goffi <goffi@goffi.org>
parents: 1012
diff changeset
208 profile = args.locals['self'].host.profile # used in quick_frontend for single profile configuration
1008
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
209 except Exception:
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
210 # we can't find profile, we return an empty value
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
211 profile = ''
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
212 return profile
d70d4fe5c5f8 core (log): added magic %(profile)s key to log_fmt:
Goffi <goffi@goffi.org>
parents: 1007
diff changeset
213
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
214 def _ansiColors(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
215 """Colorise message depending on level for terminals
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
216
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
217 @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
218 @param message: formatted message to log
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
219 @return: message with ANSI escape codes for coloration
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
220 """
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
221 if level == C.LOG_LVL_DEBUG:
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
222 out = (C.ANSI_FG_CYAN, message, C.ANSI_RESET)
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
223 elif level == C.LOG_LVL_WARNING:
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
224 out = (C.ANSI_FG_YELLOW, message, C.ANSI_RESET)
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
225 elif level == C.LOG_LVL_ERROR:
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
226 out = (C.ANSI_FG_RED,
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
227 C.ANSI_BLINK,
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
228 r'/!\ ',
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
229 C.ANSI_BLINK_OFF,
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
230 message,
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
231 C.ANSI_RESET)
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
232 elif level == C.LOG_LVL_CRITICAL:
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
233 out = (C.ANSI_BOLD,
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
234 C.ANSI_FG_RED,
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
235 'Guru Meditation ',
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
236 C.ANSI_NORMAL_WEIGHT,
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
237 message,
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
238 C.ANSI_RESET)
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
239 else:
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
240 out = message
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
241 return ''.join(out)
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
242
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
243
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
244 class Configure(object):
1017
0ea97f483464 core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents: 1016
diff changeset
245 LOGGER_CLASS = Logger
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
246
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
247 def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, force_colors=False):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
248 """Configure backend
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
249 @param level: one of C.LOG_LEVELS
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
250 @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
251 - "message"
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
252 - "levelname"
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
253 - "name" (logger name)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
254 @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
255 Use search to match expression, so ^ or $ can be necessary.
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
256 @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
257 @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
258 """
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
259 self.preTreatment()
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
260 self.configureLevel(level)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
261 self.configureFormat(fmt)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
262 self.configureOutput(output)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
263 self.configureLogger(logger)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
264 self.configureColors(colors, force_colors)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
265 self.postTreatment()
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
266 self.updateCurrentLogger()
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
267
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
268 def updateCurrentLogger(self):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
269 """update existing logger to the class needed for this backend"""
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
270 if self.LOGGER_CLASS is None:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
271 return
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
272 for name, logger in _loggers.items():
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
273 _loggers[name] = self.LOGGER_CLASS(logger)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
274
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
275 def preTreatment(self):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
276 pass
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
277
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
278 def configureLevel(self, level):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
279 pass
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
280
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
281 def configureFormat(self, fmt):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
282 pass
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
283
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
284 def configureOutput(self, output):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
285 pass
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
286
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
287 def configureLogger(self, logger):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
288 pass
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
289
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
290 def configureColors(self, colors, force_colors):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
291 pass
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
292
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
293 def postTreatment(self):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
294 pass
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
295
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
296 def manageOutputs(self, outputs_raw):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
297 """ Parse output option in a backend agnostic way, and fill _handlers consequently
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
298
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
299 @param outputs_raw: output option as enterred in environment variable or in configuration
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
300 """
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
301 if not outputs_raw:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
302 return
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
303 outputs = outputs_raw.split(C.LOG_OPT_OUTPUT_SEP)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
304 global _handlers
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
305 if len(outputs) == 1:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
306 _handlers[C.LOG_OPT_OUTPUT_FILE] = [outputs.pop()]
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
307
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
308 for output in outputs:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
309 if not output:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
310 continue
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
311 if output[-1] == ')':
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
312 # we have options
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
313 opt_begin = output.rfind('(')
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
314 options = output[opt_begin+1:-1]
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
315 output = output[:opt_begin]
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
316 else:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
317 options = None
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
318
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
319 if output not in (C.LOG_OPT_OUTPUT_DEFAULT, C.LOG_OPT_OUTPUT_FILE, C.LOG_OPT_OUTPUT_MEMORY):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
320 raise ValueError(u"Invalid output [%s]" % output)
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
321
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
322 if output == C.LOG_OPT_OUTPUT_DEFAULT:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
323 # no option for defaut handler
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
324 _handlers[output] = None
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
325 elif output == C.LOG_OPT_OUTPUT_FILE:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
326 if not options:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
327 ValueError("%(handler)s output need a path as option" % {'handler': output})
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
328 _handlers.setdefault(output, []).append(options)
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
329 options = None # option are parsed, we can empty them
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
330 elif output == C.LOG_OPT_OUTPUT_MEMORY:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
331 # we have memory handler, option can be the len limit or None
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
332 try:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
333 limit = int(options)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
334 options = None # option are parsed, we can empty them
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
335 except (TypeError, ValueError):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
336 limit = C.LOG_OPT_OUTPUT_MEMORY_LIMIT
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
337 _handlers[output] = limit
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
338
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
339 if options: # we should not have unparsed options
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
340 raise ValueError(u"options [%(options)s] are not supported for %(handler)s output" % {'options': options, 'handler': output})
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
341
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
342
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
343 class ConfigureBasic(Configure):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
344 def configureLevel(self, level):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
345 if level is not None:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
346 # we deactivate methods below level
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
347 level_idx = C.LOG_LEVELS.index(level)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
348 def dev_null(self, msg):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
349 pass
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
350 for _level in C.LOG_LEVELS[:level_idx]:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
351 setattr(Logger, _level.lower(), dev_null)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
352
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
353 def configureFormat(self, fmt):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
354 if fmt is not None:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
355 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
356 Logger.fmt = fmt
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
357
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
358 def configureOutput(self, output):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
359 if output is not None:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
360 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
361 # TODO: manage other outputs
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
362 raise NotImplementedError("Basic backend only manage default output yet")
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
363
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
364 def configureLogger(self, logger):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
365 if logger:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
366 Logger.filter_name = FilterName(logger)
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
367
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
368 def configureColors(self, colors, force_colors):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
369 if colors:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
370 import sys
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
371 if force_colors or sys.stdout.isatty(): # FIXME: isatty should be tested on each handler, not globaly
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
372 # we need colors
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
373 Logger.post_treat = lambda self, level, message: _ansiColors(level, message)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
374 elif force_colors:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
375 raise ValueError("force_colors can't be used if colors is False")
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
376
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
377
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
378 class ConfigureTwisted(ConfigureBasic):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
379 LOGGER_CLASS = TwistedLogger
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
380
1012
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
381 def changeObserver(self, observer, can_colors=False):
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
382 """Install a hook on observer to manage SàT specificities
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
383
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
384 @param observer: original observer to hook
1012
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
385 @param can_colors: True if observer can display ansi colors
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
386 """
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
387 def observer_hook(event):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
388 """redirect non SàT log to twisted_logger, and add colors when possible"""
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
389 if 'sat_logged' in event: # we only want our own logs, other are managed by twistedObserver
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
390 # we add colors if possible
1012
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
391 if (can_colors and self.LOGGER_CLASS.colors) or self.LOGGER_CLASS.force_colors:
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
392 message = event.get('message', tuple())
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
393 level = event.get('level', C.LOG_LVL_INFO)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
394 if message:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
395 event['message'] = (_ansiColors(level, ''.join(message)),) # must be a tuple
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
396 observer(event) # we can now call the original observer
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
397
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
398 return observer_hook
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
399
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
400 def changeFileLogObserver(self, observer):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
401 """Install SàT hook for FileLogObserver
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
402
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
403 if the output is a tty, we allow colors, else we don't
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
404 @param observer: original observer to hook
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
405 """
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
406 log_obs = observer.__self__
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
407 log_file = log_obs.write.__self__
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
408 try:
1012
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
409 can_colors = log_file.isatty()
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
410 except AttributeError:
1012
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
411 can_colors = False
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
412 return self.changeObserver(observer, can_colors=can_colors)
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
413
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
414 def installObserverHook(self, observer):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
415 """Check observer type and install SàT hook when possible
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
416
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
417 @param observer: observer to hook
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
418 @return: hooked observer or original one
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
419 """
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
420 if hasattr(observer, '__self__'):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
421 ori = observer
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
422 if isinstance(observer.__self__, self.log.FileLogObserver):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
423 observer = self.changeFileLogObserver(observer)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
424 elif isinstance(observer.__self__, self.log.DefaultObserver):
1012
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
425 observer = self.changeObserver(observer, can_colors=True)
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
426 else:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
427 # we use print because log system is not fully initialized
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
428 print("Unmanaged observer [%s]" % observer)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
429 return observer
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
430 self.observers[ori] = observer
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
431 return observer
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
432
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
433 def preTreatment(self):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
434 """initialise needed attributes, and install observers hooks"""
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
435 self.observers = {}
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
436 from twisted.python import log
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
437 self.log = log
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
438 self.log_publisher = log.msg.__self__
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
439 def addObserverObserver(self_logpub, other):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
440 """Install hook so we know when a new observer is added"""
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
441 other = self.installObserverHook(other)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
442 return self_logpub._originalAddObserver(other)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
443 def removeObserverObserver(self_logpub, ori):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
444 """removeObserver hook fix
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
445
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
446 As we wrap the original observer, the original removeObserver may want to remove the original object instead of the wrapper, this method fix this
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
447 """
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
448 if ori in self.observers:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
449 self_logpub._originalRemoveObserver(self.observers[ori])
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
450 else:
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
451 try:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
452 self_logpub._originalRemoveObserver(ori)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
453 except ValueError:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
454 try:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
455 ori in self.cleared_observers
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
456 except AttributeError:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
457 raise ValueError("Unknown observer")
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
458
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
459 # we replace addObserver/removeObserver by our own
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
460 log.LogPublisher._originalAddObserver = log.LogPublisher.addObserver
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
461 log.LogPublisher._originalRemoveObserver = log.LogPublisher.removeObserver
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
462 import types # see https://stackoverflow.com/a/4267590 (thx Chris Morgan/aaronasterling)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
463 log.addObserver = types.MethodType(addObserverObserver, self.log_publisher, log.LogPublisher)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
464 log.removeObserver = types.MethodType(removeObserverObserver, self.log_publisher, log.LogPublisher)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
465
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
466 # we now change existing observers
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
467 for idx, observer in enumerate(self.log_publisher.observers):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
468 self.log_publisher.observers[idx] = self.installObserverHook(observer)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
469
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
470
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
471 def configureLevel(self, level):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
472 self.LOGGER_CLASS.level = level
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
473 super(ConfigureTwisted, self).configureLevel(level)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
474
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
475 def configureOutput(self, output):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
476 import sys
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
477 if output is None:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
478 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
479 self.manageOutputs(output)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
480 addObserver = self.log.addObserver
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
481
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
482 if C.LOG_OPT_OUTPUT_DEFAULT in _handlers:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
483 # default output is already managed, we just add output to stdout if we are in debug mode
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
484 from twisted.internet import defer
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
485 if defer.Deferred.debug:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
486 addObserver(self.log.FileLogObserver(sys.stdout).emit)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
487 else:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
488 # \\default is not in the output, so we remove current observers
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
489 self.cleared_observers = self.log_publisher.observers
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
490 self.observers.clear()
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
491 del self.log_publisher.observers[:]
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
492 # and we forbid twistd to add any observer
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
493 self.log.addObserver = lambda other: None
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
494
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
495 if C.LOG_OPT_OUTPUT_FILE in _handlers:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
496 from twisted.python import logfile
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
497 for path in _handlers[C.LOG_OPT_OUTPUT_FILE]:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
498 log_file = sys.stdout if path == '-' else logfile.LogFile.fromFullPath(path)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
499 addObserver(self.log.FileLogObserver(log_file).emit)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
500
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
501 if C.LOG_OPT_OUTPUT_MEMORY in _handlers:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
502 raise NotImplementedError("Memory observer is not implemented in Twisted backend")
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
503
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
504 def configureColors(self, colors, force_colors):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
505 self.LOGGER_CLASS.colors = colors
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
506 self.LOGGER_CLASS.force_colors = force_colors
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
507 if force_colors and not colors:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
508 raise ValueError('colors must be True if force_colors is True')
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
509
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
510 def postTreatment(self):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
511 """Install twistedObserver which manage non SàT logs"""
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
512 def twistedObserver(event):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
513 """Observer which redirect log message not produced by SàT to SàT logging system"""
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
514 if not 'sat_logged' in event:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
515 # this log was not produced by SàT
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
516 from twisted.python import log
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
517 text = log.textFromEventDict(event)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
518 if text is None:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
519 return
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
520 twisted_logger = getLogger(C.LOG_TWISTED_LOGGER)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
521 log_method = twisted_logger.error if event.get('isError', False) else twisted_logger.info
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
522 log_method(text.decode('utf-8'))
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
523
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
524 self.log_publisher._originalAddObserver(twistedObserver)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
525
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
526
1017
0ea97f483464 core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents: 1016
diff changeset
527 class ConfigureCustom(ConfigureBasic):
0ea97f483464 core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents: 1016
diff changeset
528 LOGGER_CLASS = None
0ea97f483464 core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents: 1016
diff changeset
529
0ea97f483464 core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents: 1016
diff changeset
530 def __init__(self, logger_class, *args, **kwargs):
0ea97f483464 core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents: 1016
diff changeset
531 ConfigureCustom.LOGGER_CLASS = logger_class
0ea97f483464 core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents: 1016
diff changeset
532
0ea97f483464 core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents: 1016
diff changeset
533
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
534 class ConfigureStandard(Configure):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
535
1010
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
536 def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, force_colors=False):
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
537 if fmt is None:
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
538 fmt = C.LOG_OPT_FORMAT[1]
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
539 if output is None:
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
540 output = C.LOG_OPT_OUTPUT[1]
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
541 super(ConfigureStandard, self).__init__(level, fmt, output, logger, colors, force_colors)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
542
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
543 def preTreatment(self):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
544 """We use logging methods directly, instead of using Logger"""
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
545 global getLogger
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
546 global debug
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
547 global info
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
548 global warning
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
549 global error
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
550 global critical
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
551 import logging
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
552 getLogger = logging.getLogger
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
553 debug = logging.debug
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
554 info = logging.info
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
555 warning = logging.warning
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
556 error = logging.error
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
557 critical = logging.critical
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
558
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
559 def configureLevel(self, level):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
560 if level is None:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
561 level = C.LOG_LVL_DEBUG
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
562 self.level = level
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
563
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
564 def configureFormat(self, fmt):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
565 import logging
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
566
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
567 class SatFormatter(logging.Formatter):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
568 u"""Formatter which manage SàT specificities"""
1012
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
569 _format = fmt
1013
11409a6c16c7 core (log/standard backend): added "%(profile)s" format management
Goffi <goffi@goffi.org>
parents: 1012
diff changeset
570 _with_profile = '%(profile)s' in fmt
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
571
1012
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
572 def __init__(self, can_colors=False):
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
573 super(SatFormatter, self).__init__(self._format)
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
574 self.can_colors = can_colors
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
575
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
576 def format(self, record):
1013
11409a6c16c7 core (log/standard backend): added "%(profile)s" format management
Goffi <goffi@goffi.org>
parents: 1012
diff changeset
577 if self._with_profile:
11409a6c16c7 core (log/standard backend): added "%(profile)s" format management
Goffi <goffi@goffi.org>
parents: 1012
diff changeset
578 record.profile = _getProfile()
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
579 s = super(SatFormatter, self).format(record)
1012
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
580 if self.with_colors and (self.can_colors or self.force_colors):
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
581 s = _ansiColors(record.levelname, s)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
582 return s
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
583
1012
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
584 self.formatterClass = SatFormatter
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
585
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
586 def configureOutput(self, output):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
587 self.manageOutputs(output)
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
588
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
589 def configureLogger(self, logger):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
590 self.name_filter = FilterName(logger) if logger else None
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
591
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
592 def configureColors(self, colors, force_colors):
1012
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
593 self.formatterClass.with_colors = colors
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
594 self.formatterClass.force_colors = force_colors
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
595 if not colors and force_colors:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
596 raise ValueError("force_colors can't be used if colors is False")
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
597
1012
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
598 def _addHandler(self, root_logger, hdlr, can_colors=False):
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
599 hdlr.setFormatter(self.formatterClass(can_colors))
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
600 root_logger.addHandler(hdlr)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
601 root_logger.setLevel(self.level)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
602 if self.name_filter is not None:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
603 hdlr.addFilter(self.name_filter)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
604
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
605 def postTreatment(self):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
606 import logging
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
607 root_logger = logging.getLogger()
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
608 if len(root_logger.handlers) == 0:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
609 for handler, options in _handlers.items():
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
610 if handler == C.LOG_OPT_OUTPUT_DEFAULT:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
611 hdlr = logging.StreamHandler()
1012
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
612 try:
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
613 can_colors = hdlr.stream.isatty()
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
614 except AttributeError:
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
615 can_colors = False
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
616 self._addHandler(root_logger, hdlr, can_colors=can_colors)
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
617 elif handler == C.LOG_OPT_OUTPUT_MEMORY:
1010
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
618 from logging.handlers import BufferingHandler
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
619 class SatMemoryHandler(BufferingHandler):
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
620 def emit(self, record):
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
621 super(SatMemoryHandler, self).emit(self.format(record))
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
622 hdlr = SatMemoryHandler(options)
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
623 _handlers[handler] = hdlr # we keep a reference to the handler to read the buffer later
1012
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
624 self._addHandler(root_logger, hdlr, can_colors=False)
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
625 elif handler == C.LOG_OPT_OUTPUT_FILE:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
626 import os.path
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
627 for path in options:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
628 hdlr = logging.FileHandler(os.path.expanduser(path))
1012
c8771279497e core(log): standard backend: colors are now checked on a per handler basis
Goffi <goffi@goffi.org>
parents: 1010
diff changeset
629 self._addHandler(root_logger, hdlr, can_colors=False)
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
630 else:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
631 raise ValueError("Unknown handler type")
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
632 else:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
633 root_logger.warning(u"Handlers already set on root logger")
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
634
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
635
994
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
636 def configure(backend=C.LOG_BACKEND_STANDARD, **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
637 """Configure logging behaviour
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
638 @param backend: can be:
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
639 C.LOG_BACKEND_STANDARD: use standard logging module
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
640 C.LOG_BACKEND_TWISTED: use twisted logging module (with standard logging observer)
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
641 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
642 C.LOG_BACKEND_CUSTOM: use a given Logger subclass
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
643 """
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
644 global _backend
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
645 if _backend is not None:
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
646 raise exceptions.InternalError("Logging can only be configured once")
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
647 _backend = backend
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
648
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
649 if backend == C.LOG_BACKEND_BASIC:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
650 ConfigureBasic(**options)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
651
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
652 elif backend == C.LOG_BACKEND_TWISTED:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
653 ConfigureTwisted(**options)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
654
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
655 elif backend == C.LOG_BACKEND_STANDARD:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
656 ConfigureStandard(**options)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
657
1017
0ea97f483464 core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents: 1016
diff changeset
658 elif backend == C.LOG_BACKEND_CUSTOM:
0ea97f483464 core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents: 1016
diff changeset
659 logger_class = options.pop('logger_class')
0ea97f483464 core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents: 1016
diff changeset
660 ConfigureCustom(logger_class, **options)
0ea97f483464 core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents: 1016
diff changeset
661
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
662 else:
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
663 raise ValueError("unknown backend")
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
664
994
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
665 def _parseOptions(options):
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
666 """Parse string options as given in conf or environment variable, and return expected python value
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
667
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
668 @param options (dict): options with (key: name, value: string value)
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
669 """
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
670 COLORS = C.LOG_OPT_COLORS[0]
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
671 LEVEL = C.LOG_OPT_LEVEL[0]
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
672
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
673 if COLORS in options:
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
674 if options[COLORS].lower() in ('1', 'true'):
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
675 options[COLORS] = True
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
676 elif options[COLORS] == 'force':
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
677 options[COLORS] = True
994
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
678 options['force_colors'] = True
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
679 else:
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
680 options[COLORS] = False
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
681 if LEVEL in options:
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
682 level = options[LEVEL].upper()
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
683 if level not in C.LOG_LEVELS:
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
684 level = C.LOG_LVL_INFO
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
685 options[LEVEL] = level
994
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
686
1010
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
687 def satConfigure(backend=C.LOG_BACKEND_STANDARD, const=None):
994
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
688 """Configure logging system for SàT, can be used by frontends
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
689
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
690 logs conf is read in SàT conf, then in environment variables. It must be done before Memory init
1010
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
691 @param backend: backend to use, it can be:
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
692 - C.LOG_BACKEND_BASIC: print based backend
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
693 - C.LOG_BACKEND_TWISTED: Twisted logging backend
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
694 - C.LOG_BACKEND_STANDARD: standard logging backend
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
695 @param const: Const class to use instead of sat.core.constants.Const (mainly used to change default values)
994
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
696 """
1010
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
697 if const is not None:
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
698 global C
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
699 C = const
994
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
700 import ConfigParser
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
701 import os
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
702 log_conf = {}
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
703 config = ConfigParser.SafeConfigParser()
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
704 config.read(C.CONFIG_FILES)
1010
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
705 for opt_name, opt_default in C.LOG_OPTIONS():
994
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
706 try:
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
707 log_conf[opt_name] = os.environ[''.join((C.ENV_PREFIX, C.LOG_OPT_PREFIX.upper(), opt_name.upper()))]
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
708 except KeyError:
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
709 try:
1010
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
710 log_conf[opt_name] = config.get(C.LOG_OPT_SECTION, C.LOG_OPT_PREFIX + opt_name)
73a0b7f94674 primitivus: use of new logging system:
Goffi <goffi@goffi.org>
parents: 1008
diff changeset
711 except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
994
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
712 log_conf[opt_name] = opt_default
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
713
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
714 _parseOptions(log_conf)
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
715 configure(backend, **log_conf)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
716
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
717 def getLogger(name=C.LOG_BASE_LOGGER):
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
718 if _backend in (None, C.LOG_BACKEND_BASIC):
1017
0ea97f483464 core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents: 1016
diff changeset
719 logger_class = ConfigureBasic.LOGGER_CLASS
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
720 elif _backend == C.LOG_BACKEND_TWISTED:
1017
0ea97f483464 core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents: 1016
diff changeset
721 logger_class = ConfigureTwisted.LOGGER_CLASS
0ea97f483464 core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents: 1016
diff changeset
722 elif _backend == C.LOG_BACKEND_CUSTOM:
0ea97f483464 core (log): added "custom" backend
Goffi <goffi@goffi.org>
parents: 1016
diff changeset
723 logger_class = ConfigureCustom.LOGGER_CLASS
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
724 else:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
725 raise ValueError("This method should not be called with backend [%s]" % _backend)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
726 return _loggers.setdefault(name, logger_class(name))
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
727
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
728 _root_logger = getLogger()
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
729
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
730 def debug(msg):
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
731 _root_logger.debug(msg)
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
732
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
733 def info(msg):
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
734 _root_logger.info(msg)
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
735
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
736 def warning(msg):
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
737 _root_logger.warning(msg)
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
738
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
739 def error(msg):
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
740 _root_logger.error(msg)
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
741
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
742 def critical(msg):
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
743 _root_logger.critical(msg)