annotate src/core/log.py @ 1007:a7d33c7a8277

core (log): refactoring + twisted backend: - configuration is now done with classes, so inheritance is possible - twisted backend now use twisted log methods - log not produced by SàT directly in twisted backend are captured by an observer (twistedObserver) and sent back to SàT logging chain, with the "twisted" logger. - \\default output use normal twistd output, except in debug mode where it add an stdout output in addition to the log file. - when log_colors = True, colors are enabled only where it's possible (tty out), when log_colors=force colors are always enabled. - fixed some bad log.xxx calls
author Goffi <goffi@goffi.org>
date Mon, 05 May 2014 18:58:34 +0200
parents 325fd230c15d
children d70d4fe5c5f8
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
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
94 except AttributeError:
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")
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
97 return self.fmt % record if self.fmt is not None else message
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
98
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 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
100 self.log(C.LOG_LVL_DEBUG, msg)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
101
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 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
103 self.log(C.LOG_LVL_INFO, msg)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
104
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 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
106 self.log(C.LOG_LVL_WARNING, msg)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
107
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 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
109 self.log(C.LOG_LVL_ERROR, msg)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
110
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 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
112 self.log(C.LOG_LVL_CRITICAL, msg)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
113
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
114
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
115 class TwistedLogger(Logger):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
116 colors = True
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
117 force_colors = False
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
118
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
119 def __init__(self, *args, **kwargs):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
120 super(TwistedLogger, self).__init__(*args, **kwargs)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
121 from twisted.python import log
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
122 self.twisted_log = log
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
123
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
124 def out(self, message, level=None):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
125 """Actually log the message
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
126
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
127 @param message: formatted message
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
128 """
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
129 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
130
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
131
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
132 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
133 """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
134
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
135 def __init__(self, name_re):
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
136 """Initialise name filter
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
137
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
138 @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
139 """
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
140 assert name_re
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
141 import re
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
142 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
143
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
144 def filter(self, record):
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
145 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
146 return 1
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
147 return 0
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
148
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
149 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
150 """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
151
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
152 @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
153 @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
154 """
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
155 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
156 pass
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
157 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
158 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
159 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
160
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
161
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
162 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
163 """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
164
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
165 @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
166 @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
167 @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
168 """
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
169 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
170 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
171 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
172 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
173 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
174 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
175 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
176 r'/!\ ',
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
177 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
178 message,
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
179 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
180 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
181 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
182 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
183 '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
184 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
185 message,
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
186 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
187 else:
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
188 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
189 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
190
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
191
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
192 class Configure(object):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
193 LOGGER_CLASS = None
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
194
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
195 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
196 """Configure backend
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
197 @param level: one of C.LOG_LEVELS
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
198 @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
199 - "message"
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
200 - "levelname"
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
201 - "name" (logger name)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
202 @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
203 Use search to match expression, so ^ or $ can be necessary.
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
204 @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
205 @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
206 """
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
207 self.preTreatment()
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
208 self.configureLevel(level)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
209 self.configureFormat(fmt)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
210 self.configureOutput(output)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
211 self.configureLogger(logger)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
212 self.configureColors(colors, force_colors)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
213 self.postTreatment()
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
214 self.updateCurrentLogger()
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
215
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
216 def updateCurrentLogger(self):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
217 """update existing logger to the class needed for this backend"""
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
218 if self.LOGGER_CLASS is None:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
219 return
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
220 for name, logger in _loggers.items():
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
221 _loggers[name] = self.LOGGER_CLASS(logger)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
222
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
223 def preTreatment(self):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
224 pass
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
225
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
226 def configureLevel(self, level):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
227 pass
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
228
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
229 def configureFormat(self, fmt):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
230 pass
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
231
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
232 def configureOutput(self, output):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
233 pass
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
234
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
235 def configureLogger(self, logger):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
236 pass
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
237
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
238 def configureColors(self, colors, force_colors):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
239 pass
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
240
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
241 def postTreatment(self):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
242 pass
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 def manageOutputs(self, outputs_raw):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
245 """ 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
246
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
247 @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
248 """
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
249 if not outputs_raw:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
250 return
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
251 outputs = outputs_raw.split(C.LOG_OPT_OUTPUT_SEP)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
252 global _handlers
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
253 if len(outputs) == 1:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
254 _handlers[C.LOG_OPT_OUTPUT_FILE] = [outputs.pop()]
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
255
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
256 for output in outputs:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
257 if not output:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
258 continue
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
259 if output[-1] == ')':
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
260 # we have options
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
261 opt_begin = output.rfind('(')
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
262 options = output[opt_begin+1:-1]
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
263 output = output[:opt_begin]
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
264 else:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
265 options = None
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
266
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
267 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
268 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
269
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
270 if output == C.LOG_OPT_OUTPUT_DEFAULT:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
271 # no option for defaut handler
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
272 _handlers[output] = None
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
273 elif output == C.LOG_OPT_OUTPUT_FILE:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
274 if not options:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
275 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
276 _handlers.setdefault(output, []).append(options)
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
277 options = None # option are parsed, we can empty them
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
278 elif output == C.LOG_OPT_OUTPUT_MEMORY:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
279 # 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
280 try:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
281 limit = int(options)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
282 options = None # option are parsed, we can empty them
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
283 except (TypeError, ValueError):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
284 limit = C.LOG_OPT_OUTPUT_MEMORY_LIMIT
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
285 _handlers[output] = limit
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 if options: # we should not have unparsed options
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
288 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
289
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
290
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
291 class ConfigureBasic(Configure):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
292 def configureLevel(self, level):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
293 if level is not None:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
294 # we deactivate methods below level
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
295 level_idx = C.LOG_LEVELS.index(level)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
296 def dev_null(self, msg):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
297 pass
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
298 for _level in C.LOG_LEVELS[:level_idx]:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
299 setattr(Logger, _level.lower(), dev_null)
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 def configureFormat(self, fmt):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
302 if fmt is not None:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
303 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
304 Logger.fmt = fmt
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
305
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
306 def configureOutput(self, output):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
307 if output is not None:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
308 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
309 # TODO: manage other outputs
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
310 raise NotImplementedError("Basic backend only manage default output yet")
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
311
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
312 def configureLogger(self, logger):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
313 if logger:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
314 Logger.filter_name = FilterName(logger)
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
315
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
316 def configureColors(self, colors, force_colors):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
317 if colors:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
318 import sys
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
319 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
320 # we need colors
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
321 Logger.post_treat = lambda self, level, message: _ansiColors(level, message)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
322 elif force_colors:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
323 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
324
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
325
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
326 class ConfigureTwisted(ConfigureBasic):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
327 LOGGER_CLASS = TwistedLogger
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
328
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
329 def changeObserver(self, observer, can_color=False):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
330 """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
331
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
332 @param observer: original observer to hook
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
333 @param can_color: True if observer can display ansi colors
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
334 """
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
335 def observer_hook(event):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
336 """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
337 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
338 # we add colors if possible
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
339 if (can_color and self.LOGGER_CLASS.colors) or self.LOGGER_CLASS.force_colors:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
340 message = event.get('message', tuple())
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
341 level = event.get('level', C.LOG_LVL_INFO)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
342 if message:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
343 event['message'] = (_ansiColors(level, ''.join(message)),) # must be a tuple
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
344 observer(event) # we can now call the original observer
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
345
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
346 return observer_hook
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
347
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
348 def changeFileLogObserver(self, observer):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
349 """Install SàT hook for FileLogObserver
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
350
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
351 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
352 @param observer: original observer to hook
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
353 """
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
354 log_obs = observer.__self__
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
355 log_file = log_obs.write.__self__
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
356 try:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
357 can_color = log_file.isatty()
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
358 except AttributeError:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
359 can_color = False
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
360 return self.changeObserver(observer, can_color=can_color)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
361
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
362 def installObserverHook(self, observer):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
363 """Check observer type and install SàT hook when possible
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
364
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
365 @param observer: observer to hook
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
366 @return: hooked observer or original one
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
367 """
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
368 if hasattr(observer, '__self__'):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
369 ori = observer
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
370 if isinstance(observer.__self__, self.log.FileLogObserver):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
371 observer = self.changeFileLogObserver(observer)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
372 elif isinstance(observer.__self__, self.log.DefaultObserver):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
373 observer = self.changeObserver(observer, can_color=True)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
374 else:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
375 # we use print because log system is not fully initialized
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
376 print("Unmanaged observer [%s]" % observer)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
377 return observer
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
378 self.observers[ori] = observer
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
379 return observer
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
380
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
381 def preTreatment(self):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
382 """initialise needed attributes, and install observers hooks"""
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
383 self.observers = {}
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
384 from twisted.python import log
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
385 self.log = log
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
386 self.log_publisher = log.msg.__self__
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
387 def addObserverObserver(self_logpub, other):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
388 """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
389 other = self.installObserverHook(other)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
390 return self_logpub._originalAddObserver(other)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
391 def removeObserverObserver(self_logpub, ori):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
392 """removeObserver hook fix
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
393
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
394 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
395 """
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
396 if ori in self.observers:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
397 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
398 else:
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
399 try:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
400 self_logpub._originalRemoveObserver(ori)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
401 except ValueError:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
402 try:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
403 ori in self.cleared_observers
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
404 except AttributeError:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
405 raise ValueError("Unknown observer")
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
406
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
407 # we replace addObserver/removeObserver by our own
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
408 log.LogPublisher._originalAddObserver = log.LogPublisher.addObserver
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
409 log.LogPublisher._originalRemoveObserver = log.LogPublisher.removeObserver
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
410 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
411 log.addObserver = types.MethodType(addObserverObserver, self.log_publisher, log.LogPublisher)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
412 log.removeObserver = types.MethodType(removeObserverObserver, self.log_publisher, log.LogPublisher)
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 # we now change existing observers
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
415 for idx, observer in enumerate(self.log_publisher.observers):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
416 self.log_publisher.observers[idx] = self.installObserverHook(observer)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
417
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
418
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
419 def configureLevel(self, level):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
420 self.LOGGER_CLASS.level = level
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
421 super(ConfigureTwisted, self).configureLevel(level)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
422
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
423 def configureOutput(self, output):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
424 import sys
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
425 if output is None:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
426 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
427 self.manageOutputs(output)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
428 addObserver = self.log.addObserver
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
429
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
430 if C.LOG_OPT_OUTPUT_DEFAULT in _handlers:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
431 # 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
432 from twisted.internet import defer
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
433 if defer.Deferred.debug:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
434 addObserver(self.log.FileLogObserver(sys.stdout).emit)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
435 else:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
436 # \\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
437 self.cleared_observers = self.log_publisher.observers
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
438 self.observers.clear()
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
439 del self.log_publisher.observers[:]
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
440 # and we forbid twistd to add any observer
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
441 self.log.addObserver = lambda other: None
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
442
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
443 if C.LOG_OPT_OUTPUT_FILE in _handlers:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
444 from twisted.python import logfile
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
445 for path in _handlers[C.LOG_OPT_OUTPUT_FILE]:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
446 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
447 addObserver(self.log.FileLogObserver(log_file).emit)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
448
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
449 if C.LOG_OPT_OUTPUT_MEMORY in _handlers:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
450 raise NotImplementedError("Memory observer is not implemented in Twisted backend")
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
451
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
452 def configureColors(self, colors, force_colors):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
453 self.LOGGER_CLASS.colors = colors
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
454 self.LOGGER_CLASS.force_colors = force_colors
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
455 if force_colors and not colors:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
456 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
457
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
458 def postTreatment(self):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
459 """Install twistedObserver which manage non SàT logs"""
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
460 def twistedObserver(event):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
461 """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
462 if not 'sat_logged' in event:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
463 # this log was not produced by SàT
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
464 from twisted.python import log
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
465 text = log.textFromEventDict(event)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
466 if text is None:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
467 return
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
468 twisted_logger = getLogger(C.LOG_TWISTED_LOGGER)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
469 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
470 log_method(text.decode('utf-8'))
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
471
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
472 self.log_publisher._originalAddObserver(twistedObserver)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
473
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 class ConfigureStandard(Configure):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
476
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
477 def __init__(self, level=None, fmt=C.LOG_OPT_FORMAT[1], output=C.LOG_OPT_OUTPUT[1], logger=None, colors=False, force_colors=False):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
478 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
479
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
480 def preTreatment(self):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
481 """We use logging methods directly, instead of using Logger"""
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
482 global getLogger
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
483 global debug
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
484 global info
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
485 global warning
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
486 global error
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
487 global critical
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
488 import logging
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
489 getLogger = logging.getLogger
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
490 debug = logging.debug
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
491 info = logging.info
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
492 warning = logging.warning
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
493 error = logging.error
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
494 critical = logging.critical
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
495
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
496 def configureLevel(self, level):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
497 if level is None:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
498 level = C.LOG_LVL_DEBUG
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
499 self.level = level
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 def configureFormat(self, fmt):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
502 import logging
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
503 format_ = fmt
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
504
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
505 class SatFormatter(logging.Formatter):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
506 u"""Formatter which manage SàT specificities"""
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
507
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
508 def __init__(self, fmt=None, datefmt=None):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
509 super(SatFormatter, self).__init__(fmt, datefmt)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
510
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
511 def format(self, record):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
512 s = super(SatFormatter, self).format(record)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
513 if self.with_color:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
514 s = _ansiColors(record.levelname, s)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
515 return s
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
516
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
517 self.formatter = SatFormatter(format_)
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
518
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
519 def configureOutput(self, output):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
520 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
521
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
522 def configureLogger(self, logger):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
523 self.name_filter = FilterName(logger) if logger else None
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
524
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
525 def configureColors(self, colors, force_colors):
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
526 import sys
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
527 self.with_color = colors & (sys.stdout.isatty() or force_colors)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
528 if not colors and force_colors:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
529 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
530
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
531 def _addHandler(self, root_logger, hdlr):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
532 hdlr.setFormatter(self.formatter)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
533 root_logger.addHandler(hdlr)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
534 root_logger.setLevel(self.level)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
535 if self.name_filter is not None:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
536 hdlr.addFilter(self.name_filter)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
537
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
538 def postTreatment(self):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
539 import logging
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
540 root_logger = logging.getLogger()
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
541 if len(root_logger.handlers) == 0:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
542 for handler, options in _handlers.items():
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
543 if handler == C.LOG_OPT_OUTPUT_DEFAULT:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
544 hdlr = logging.StreamHandler()
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
545 self._addHandler(root_logger, hdlr)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
546 elif handler == C.LOG_OPT_OUTPUT_MEMORY:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
547 import logging.handlers
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
548 hdlr = logging.handlers.BufferingHandler(options)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
549 _handlers[handler] = hdlr # we keep a reference to the handler to read the buffer later
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
550 self._addHandler(root_logger, hdlr)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
551 elif handler == C.LOG_OPT_OUTPUT_FILE:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
552 import os.path
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
553 for path in options:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
554 hdlr = logging.FileHandler(os.path.expanduser(path))
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
555 self._addHandler(root_logger, hdlr)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
556 else:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
557 raise ValueError("Unknown handler type")
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
558 else:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
559 root_logger.warning(u"Handlers already set on root logger")
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
560
1006
325fd230c15d core (log): added advanced feature to basic backend (colors/formatting/level and logger filtering)
Goffi <goffi@goffi.org>
parents: 1005
diff changeset
561
994
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
562 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
563 """Configure logging behaviour
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
564 @param backend: can be:
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
565 C.LOG_BACKEND_STANDARD: use standard logging module
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
566 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
567 C.LOG_BACKEND_BASIC: use a basic print based logging
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
568 """
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
569 global _backend
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
570 if _backend is not None:
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
571 raise exceptions.InternalError("Logging can only be configured once")
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
572 _backend = backend
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
573
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
574 if backend == C.LOG_BACKEND_BASIC:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
575 ConfigureBasic(**options)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
576
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
577 elif backend == C.LOG_BACKEND_TWISTED:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
578 ConfigureTwisted(**options)
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
579
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
580 elif backend == C.LOG_BACKEND_STANDARD:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
581 ConfigureStandard(**options)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
582
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
583 else:
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
584 raise ValueError("unknown backend")
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
585
994
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
586 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
587 """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
588
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
589 @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
590 """
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
591 COLORS = C.LOG_OPT_COLORS[0]
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
592 LEVEL = C.LOG_OPT_LEVEL[0]
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
593
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
594 if COLORS in options:
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
595 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
596 options[COLORS] = True
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
597 elif options[COLORS] == 'force':
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
598 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
599 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
600 else:
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
601 options[COLORS] = False
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
602 if LEVEL in options:
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
603 level = options[LEVEL].upper()
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
604 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
605 level = C.LOG_LVL_INFO
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
606 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
607
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
608 def satConfigure(backend=C.LOG_BACKEND_TWISTED):
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
609 """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
610
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
611 logs conf is read in SàT conf, then in environment variables. It must be done before Memory init
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
612 """
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
613 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
614 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
615 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
616 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
617 config.read(C.CONFIG_FILES)
1005
b4af31a8a4f2 core (logs): added formatting, name filter and outputs management:
Goffi <goffi@goffi.org>
parents: 994
diff changeset
618 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
619 try:
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
620 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
621 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
622 try:
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
623 log_conf[opt_name] = config.get('DEFAULT', C.LOG_OPT_PREFIX + opt_name)
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
624 except ConfigParser.NoOptionError:
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
625 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
626
652c01ca69b1 core (log): configuration and environment variables are now checked for log level and colors:
Goffi <goffi@goffi.org>
parents: 992
diff changeset
627 _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
628 configure(backend, **log_conf)
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
629
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
630 def getLogger(name=C.LOG_BASE_LOGGER):
1007
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
631 if _backend in (None, C.LOG_BACKEND_BASIC):
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
632 logger_class = Logger
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
633 elif _backend == C.LOG_BACKEND_TWISTED:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
634 logger_class = TwistedLogger
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
635 else:
a7d33c7a8277 core (log): refactoring + twisted backend:
Goffi <goffi@goffi.org>
parents: 1006
diff changeset
636 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
637 return _loggers.setdefault(name, logger_class(name))
991
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
638
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
639 _root_logger = getLogger()
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
640
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
641 def debug(msg):
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
642 _root_logger.debug(msg)
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 def info(msg):
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
645 _root_logger.info(msg)
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
646
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
647 def warning(msg):
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
648 _root_logger.warning(msg)
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
649
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
650 def error(msg):
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
651 _root_logger.error(msg)
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
652
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
653 def critical(msg):
05e02f8b7eb4 core: logging refactoring, first step:
Goffi <goffi@goffi.org>
parents:
diff changeset
654 _root_logger.critical(msg)