49
49
# FIXME: Unfortunately it turns out that python's logging module
50
50
# is quite expensive, even when the message is not printed by any handlers.
51
51
# We should perhaps change back to just simply doing it here.
53
# On the other hand, as of 1.2 we generally only call the mutter() statement
54
# if (according to debug_flags) we actually intend to write it. So the
55
# increased cost of logging.py is not so bad, and we could standardize on
226
229
"""Configure default logging: messages to stderr and debug to .bzr.log
228
231
This should only be called once per process.
233
Non-command-line programs embedding bzrlib do not need to call this. They
234
can instead either pass a file to _push_log_file, or act directly on
235
logging.getLogger("bzr").
230
237
Output can be redirected away by calling _push_log_file.
232
# TODO: this isn't really appropriate for Launchpad or other bzrlib
233
# users, which might want more control on where the messages go.
234
global _stderr_handler, _trace_file
235
239
# create encoded wrapper around stderr
236
writer_factory = codecs.getwriter(osutils.get_terminal_encoding())
237
encoded_stderr = writer_factory(sys.stderr, errors='replace')
238
# write >=info messages to stderr
239
_stderr_handler = logging.StreamHandler(encoded_stderr)
240
_stderr_handler.setLevel(logging.INFO)
241
240
bzr_log_file = _open_bzr_log()
242
241
_push_log_file(bzr_log_file,
243
242
r'[%(process)5d] %(asctime)s.%(msecs)03d %(levelname)s: %(message)s',
244
243
r'%Y-%m-%d %H:%M:%S')
245
244
# after hooking output into bzr_log, we also need to attach a stderr
247
logging.getLogger('bzr').addHandler(_stderr_handler)
245
# handler, writing only at level info and with encoding
246
writer_factory = codecs.getwriter(osutils.get_terminal_encoding())
247
encoded_stderr = writer_factory(sys.stderr, errors='replace')
248
stderr_handler = logging.StreamHandler(encoded_stderr)
249
stderr_handler.setLevel(logging.INFO)
250
logging.getLogger('bzr').addHandler(stderr_handler)
250
253
def _push_log_file(to_file, log_format=None, date_format=None):
353
356
# TODO: if this is not used, remove it. if it is, maybe set the logger
354
357
# level, rather than the handler level?
356
_stderr_handler.setLevel(logging.WARNING)
359
_bzr_logger.setLevel(logging.WARNING)
358
_stderr_handler.setLevel(logging.INFO)
361
_bzr_logger.setLevel(logging.INFO)