Revision d24bc000 lib/utils/log.py
b/lib/utils/log.py | ||
---|---|---|
129 | 129 |
_LogHandler = _LogErrorsToConsole(_ReopenableLogHandler) |
130 | 130 |
|
131 | 131 |
|
132 |
def _GetLogFormatter(program, multithreaded, debug, syslog): |
|
133 |
"""Build log formatter. |
|
134 |
|
|
135 |
@param program: Program name |
|
136 |
@param multithreaded: Whether to add thread name to log messages |
|
137 |
@param debug: Whether to enable debug messages |
|
138 |
@param syslog: Whether the formatter will be used for syslog |
|
139 |
|
|
140 |
""" |
|
141 |
parts = [] |
|
142 |
|
|
143 |
if syslog: |
|
144 |
parts.append(program + "[%(process)d]:") |
|
145 |
else: |
|
146 |
parts.append("%(asctime)s: " + program + " pid=%(process)d") |
|
147 |
|
|
148 |
if multithreaded: |
|
149 |
if syslog: |
|
150 |
parts.append(" (%(threadName)s)") |
|
151 |
else: |
|
152 |
parts.append("/%(threadName)s") |
|
153 |
|
|
154 |
# Add debug info for non-syslog loggers |
|
155 |
if debug and not syslog: |
|
156 |
parts.append(" %(module)s:%(lineno)s") |
|
157 |
|
|
158 |
# Ses, we do want the textual level, as remote syslog will probably lose the |
|
159 |
# error level, and it's easier to grep for it. |
|
160 |
parts.append(" %(levelname)s %(message)s") |
|
161 |
|
|
162 |
return logging.Formatter("".join(parts)) |
|
163 |
|
|
164 |
|
|
132 | 165 |
def SetupLogging(logfile, debug=0, stderr_logging=False, program="", |
133 | 166 |
multithreaded=False, syslog=constants.SYSLOG_USAGE, |
134 | 167 |
console_logging=False): |
... | ... | |
157 | 190 |
syslog/stderr logging is disabled |
158 | 191 |
|
159 | 192 |
""" |
160 |
fmt = "%(asctime)s: " + program + " pid=%(process)d" |
|
161 |
sft = program + "[%(process)d]:" |
|
162 |
if multithreaded: |
|
163 |
fmt += "/%(threadName)s" |
|
164 |
sft += " (%(threadName)s)" |
|
165 |
if debug: |
|
166 |
fmt += " %(module)s:%(lineno)s" |
|
167 |
# no debug info for syslog loggers |
|
168 |
fmt += " %(levelname)s %(message)s" |
|
169 |
# yes, we do want the textual level, as remote syslog will probably |
|
170 |
# lose the error level, and it's easier to grep for it |
|
171 |
sft += " %(levelname)s %(message)s" |
|
172 |
formatter = logging.Formatter(fmt) |
|
173 |
sys_fmt = logging.Formatter(sft) |
|
193 |
formatter = _GetLogFormatter(program, multithreaded, debug, False) |
|
194 |
syslog_fmt = _GetLogFormatter(program, multithreaded, debug, True) |
|
174 | 195 |
|
175 | 196 |
root_logger = logging.getLogger("") |
176 | 197 |
root_logger.setLevel(logging.NOTSET) |
... | ... | |
193 | 214 |
facility = logging.handlers.SysLogHandler.LOG_DAEMON |
194 | 215 |
syslog_handler = logging.handlers.SysLogHandler(constants.SYSLOG_SOCKET, |
195 | 216 |
facility) |
196 |
syslog_handler.setFormatter(sys_fmt) |
|
217 |
syslog_handler.setFormatter(syslog_fmt)
|
|
197 | 218 |
# Never enable debug over syslog |
198 | 219 |
syslog_handler.setLevel(logging.INFO) |
199 | 220 |
root_logger.addHandler(syslog_handler) |
Also available in: Unified diff