Logging utilities for Certbot.
The best way to use this module is through
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.
Setup logging before command line arguments are parsed.
Terminal logging is setup using
certbot.constants.QUIET_LOGGING_LEVELso 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_setupis called, these buffered messages are written to a temporary file. If Certbot doesn’t exit,
post_arg_parse_setupwrites the messages to the normal log files.
Setup logging after command line arguments are parsed.
This function assumes
pre_arg_parse_setupwas 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.
Parameters: config (certbot.interface.IConfig) – Configuration object
setup_log_file_handler(config, logfile, fmt)¶
Setup file debug logging.
file handler and absolute path to the log file
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
Buffers logging messages in memory until the buffer is flushed.
This differs from
logging.handlers.MemoryHandlerin that flushing only happens when flush(force=True) is called.
Close the memory handler, but don’t set the target to None.
Flush the buffer if force=True.
If force=False, this call is a noop.
Parameters: force (bool) – True if the buffer should be flushed.
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.
Variables: path (str) – file system path to the temporary log file
Log the specified logging record.
Parameters: record (logging.LogRecord) – Record to be formatted
Close the handler and the temporary log file.
The temporary log file is deleted if it wasn’t used.
pre_arg_parse_except_hook(memory_handler, *args, **kwargs)¶
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.
post_arg_parse_except_hook(exc_type, exc_value, trace, debug, log_path)¶
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.