Coverage for certbot/log.py : 100%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
"""Logging utilities for Certbot.
The best way to use this module is through `pre_arg_parse_setup` and `post_arg_parse_setup`. `pre_arg_parse_setup` configures a minimal terminal logger and ensures a detailed log is written to a secure temporary file if Certbot exits before `post_arg_parse_setup` is called. `post_arg_parse_setup` relies on the parsed command line arguments and does the full logging setup with terminal and rotating file handling as configured by the user. Any logged messages before `post_arg_parse_setup` is called are sent to the rotating file handler. Special care is taken by both methods to ensure all errors are logged and properly flushed before program exit.
"""
# Logging format
"""Setup logging before command line arguments are parsed.
Terminal logging is setup using `certbot.constants.QUIET_LOGGING_LEVEL` so Certbot is as quiet as possible. File logging is setup so that logging messages are buffered in memory. If Certbot exits before `post_arg_parse_setup` is called, these buffered messages are written to a temporary file. If Certbot doesn't exit, `post_arg_parse_setup` writes the messages to the normal log files.
This function also sets `logging.shutdown` to be called on program exit which automatically flushes logging handlers and `sys.excepthook` to properly log/display fatal exceptions.
"""
# logging.shutdown will flush the memory handler because flush() and # close() are explicitly called pre_arg_parse_except_hook, memory_handler, debug='--debug' in sys.argv, log_path=temp_handler.path)
"""Setup logging after command line arguments are parsed.
This function assumes `pre_arg_parse_setup` was called earlier and the root logging configuration has not been modified. A rotating file logging handler is created and the buffered log messages are sent to that handler. Terminal logging output is set to the level requested by the user.
:param certbot.interface.IConfig config: Configuration object
""" config, 'letsencrypt.log', FILE_FMT)
else:
post_arg_parse_except_hook, debug=config.debug, log_path=logs_dir)
"""Setup file debug logging.
:param certbot.interface.IConfig config: Configuration object :param str logfile: basename for the log file :param str fmt: logging format string
:returns: file handler and absolute path to the log file :rtype: tuple
""" # TODO: logs might contain sensitive data such as contents of the # private key! #525 config.logs_dir, 0o700, compat.os_geteuid(), config.strict_permissions) log_file_path, maxBytes=2 ** 20, backupCount=config.max_log_backups) # rotate on each invocation, rollover only possible when maxBytes # is nonzero and backupCount is nonzero, so we set maxBytes as big # as possible not to overrun in single CLI invocation (1MB).
"""Sends colored logging output to a stream.
If the specified stream is not a tty, the class works like the standard `logging.StreamHandler`. Default red_level is `logging.WARNING`.
:ivar bool colored: True if output should be colored :ivar bool red_level: The level at which to output
""" stream.isatty())
"""Formats the string representation of record.
:param logging.LogRecord record: Record to be formatted
:returns: Formatted, string representation of record :rtype: str
""" else:
"""Buffers logging messages in memory until the buffer is flushed.
This differs from `logging.handlers.MemoryHandler` in that flushing only happens when flush(force=True) is called.
""" # capacity doesn't matter because should_flush() is overridden
"""Close the memory handler, but don't set the target to None.""" # This allows the logging module which may only have a weak # reference to the target handler to properly flush and close it.
"""Flush the buffer if force=True.
If force=False, this call is a noop.
:param bool force: True if the buffer should be flushed.
""" # This method allows flush() calls in logging.shutdown to be a # noop so we can control when this handler is flushed.
"""Should the buffer be automatically flushed?
:param logging.LogRecord record: log record to be considered
:returns: False because the buffer should never be auto-flushed :rtype: bool
"""
"""Safely logs messages to a temporary file.
The file is created with permissions 600. If no log records are sent to this handler, the temporary file is deleted when the handler is closed.
:ivar str path: file system path to the temporary log file
"""
"""Log the specified logging record.
:param logging.LogRecord record: Record to be formatted
"""
"""Close the handler and the temporary log file.
The temporary log file is deleted if it wasn't used.
""" # StreamHandler.close() doesn't close the stream to allow a # stream like stderr to be used finally:
"""A simple wrapper around post_arg_parse_except_hook.
The additional functionality provided by this wrapper is the memory handler will be flushed before Certbot exits. This allows us to write logging messages to a temporary file if we crashed before logging was fully configured.
Since sys.excepthook isn't called on SystemExit exceptions, the memory handler will not be flushed in this case which prevents us from creating temporary log files when argparse exits because a command line argument was invalid or -h, --help, or --version was provided on the command line.
:param MemoryHandler memory_handler: memory handler to flush :param tuple args: args for post_arg_parse_except_hook :param dict kwargs: kwargs for post_arg_parse_except_hook
""" finally: # flush() is called here so messages logged during # post_arg_parse_except_hook are also flushed.
"""Logs fatal exceptions and reports them to the user.
If debug is True, the full exception and traceback is shown to the user, otherwise, it is suppressed. sys.exit is always called with a nonzero status.
:param type exc_type: type of the raised exception :param BaseException exc_value: raised exception :param traceback trace: traceback of where the exception was raised :param bool debug: True if the traceback should be shown to the user :param str log_path: path to file or directory containing the log
""" # constants.QUIET_LOGGING_LEVEL or higher should be used to # display message the user, otherwise, a lower level like # logger.DEBUG should be used else: # Remove the ACME error prefix from the exception else:
"""Print a message about the log location and exit.
The message is printed to stderr and the program will exit with a nonzero status.
:param str log_path: path to file or directory containing the log
""" else: |