patroni.log module

Patroni logging facilities.

Daemon processes will use a 2-step logging handler. Whenever a log message is issued it is initially enqueued in-memory and is later asynchronously flushed by a thread to the final destination.

_class _patroni.log.PatroniLogger View on GitHub

Bases: Thread + Logging thread for the Patroni daemon process. + It is a 2-step logging approach. Any time a log message is issued it is initially enqueued in-memory, and then asynchronously flushed to the final destination by the logging thread. + See also

link:#patroni.log.QueueHandler[`+QueueHandler+`]: object used for enqueueing messages in-memory.
+
Variables:;;
+
DEFAULT_FORMAT_ = '%(asctime)s %(levelname)s: %(message)s'_link:#patroni.log.PatroniLogger.DEFAULT_FORMAT[];;
+
DEFAULT_LEVEL_ = 'INFO'_link:#patroni.log.PatroniLogger.DEFAULT_LEVEL[];;
+
DEFAULT_MAX_QUEUE_SIZE_ = 1000_link:#patroni.log.PatroniLogger.DEFAULT_MAX_QUEUE_SIZE[];;
+
DEFAULT_TRACEBACK_LEVEL_ = 'ERROR'_link:#patroni.log.PatroniLogger.DEFAULT_TRACEBACK_LEVEL[];;
+
DEFAULT_TYPE_ = 'plain'_link:#patroni.log.PatroniLogger.DEFAULT_TYPE[];;
+
LOGGING_BROKEN_EXIT_CODE_ = 5_link:#patroni.log.PatroniLogger.LOGGING_BROKEN_EXIT_CODE[];;
+
NORMAL_LOG_QUEUE_SIZE_ = 2_link:#patroni.log.PatroniLogger.NORMAL_LOG_QUEUE_SIZE[];;
+
\\__init\\__() →  https://docs.python.org/3/library/constants.html#None[None] https://github.com/zalando/patroni/blob/9d231aeecdd69f4d06c75a702755fa70d8c2b5f6/patroni/log.py#L185-L208[View on GitHub]link:#patroni.log.PatroniLogger.\\__init\\__[];;
  Prepare logging queue and proxy handlers as they become ready during daemon startup.
  +
  Note
  While Patroni is starting up it keeps `+DEBUG+` log level, and writes log messages through a proxy handler. Once the logger thread is finally started, it switches from that proxy handler to the queue based logger, and applies the configured log settings. The switching is used to avoid that the logger thread prevents Patroni from shutting down if any issue occurs in the meantime until the thread is properly started.
+
_close_old_handlers() →  https://docs.python.org/3/library/constants.html#None[None] https://github.com/zalando/patroni/blob/9d231aeecdd69f4d06c75a702755fa70d8c2b5f6/patroni/log.py#L439-L455[View on GitHub]link:#patroni.log.PatroniLogger._close_old_handlers[];;
  Close old log handlers.
  +
  Note
  It is used to remove different handlers that were configured previous to a reload in the configuration, e.g. if we are switching from  https://docs.python.org/3/library/logging.handlers.html#logging.handlers.RotatingFileHandler[`+RotatingFileHandler+`] to class:~logging.StreamHandler and vice-versa.
+
_get_formatter(_config:  https://docs.python.org/3/library/typing.html#typing.Dict[Dict][ https://docs.python.org/3/library/stdtypes.html#str[str],  https://docs.python.org/3/library/typing.html#typing.Any[Any]]_) →  https://docs.python.org/3/library/logging.html#logging.Formatter[Formatter] https://github.com/zalando/patroni/blob/9d231aeecdd69f4d06c75a702755fa70d8c2b5f6/patroni/log.py#L367-L388[View on GitHub]link:#patroni.log.PatroniLogger._get_formatter[];;
  Returns a logging formatter based on the type of logger in the given configuration.
  +
  Parameters:::
    *config* – `+log+` section from Patroni configuration.
  Returns:::
    A  https://docs.python.org/3/library/logging.html#logging.Formatter[`+logging.Formatter+`] object that can be used to format log records.
+
_get_json_formatter(_logformat:  https://docs.python.org/3/library/typing.html#typing.List[List][ https://docs.python.org/3/library/stdtypes.html#str[str] |  https://docs.python.org/3/library/typing.html#typing.Dict[Dict][ https://docs.python.org/3/library/stdtypes.html#str[str],  https://docs.python.org/3/library/typing.html#typing.Any[Any]] |  https://docs.python.org/3/library/typing.html#typing.Any[Any]] |  https://docs.python.org/3/library/stdtypes.html#str[str] |  https://docs.python.org/3/library/typing.html#typing.Any[Any]_, _dateformat:  https://docs.python.org/3/library/stdtypes.html#str[str] |  https://docs.python.org/3/library/constants.html#None[None]_, _static_fields:  https://docs.python.org/3/library/typing.html#typing.Dict[Dict][ https://docs.python.org/3/library/stdtypes.html#str[str],  https://docs.python.org/3/library/typing.html#typing.Any[Any]]_) →  https://docs.python.org/3/library/logging.html#logging.Formatter[Formatter] https://github.com/zalando/patroni/blob/9d231aeecdd69f4d06c75a702755fa70d8c2b5f6/patroni/log.py#L300-L365[View on GitHub]link:#patroni.log.PatroniLogger._get_json_formatter[];;
  Returns a logging formatter that outputs JSON formatted messages.
  +
  Note
  If `+pythonjsonlogger+` library is not installed, prints an error message and returns a plain log formatter instead.
  +
  Parameters:::
  Returns:::
    A logging formatter object that can be used to format log records as JSON strings.
+
_get_plain_formatter(_logformat:  https://docs.python.org/3/library/typing.html#typing.List[List][ https://docs.python.org/3/library/stdtypes.html#str[str] |  https://docs.python.org/3/library/typing.html#typing.Dict[Dict][ https://docs.python.org/3/library/stdtypes.html#str[str],  https://docs.python.org/3/library/typing.html#typing.Any[Any]] |  https://docs.python.org/3/library/typing.html#typing.Any[Any]] |  https://docs.python.org/3/library/stdtypes.html#str[str] |  https://docs.python.org/3/library/typing.html#typing.Any[Any]_, _dateformat:  https://docs.python.org/3/library/stdtypes.html#str[str] |  https://docs.python.org/3/library/constants.html#None[None]_) →  https://docs.python.org/3/library/logging.html#logging.Formatter[Formatter] https://github.com/zalando/patroni/blob/9d231aeecdd69f4d06c75a702755fa70d8c2b5f6/patroni/log.py#L282-L298[View on GitHub]link:#patroni.log.PatroniLogger._get_plain_formatter[];;
  Returns a logging formatter with the specified format and date format.
  +
  Note
  If the log format isn’t a string, prints a warning message and uses the default log format instead.
  +
  Parameters:::
  Returns:::
    A logging formatter object that can be used to format log records.
+
_is_config_changed(_config:  https://docs.python.org/3/library/typing.html#typing.Dict[Dict][ https://docs.python.org/3/library/stdtypes.html#str[str],  https://docs.python.org/3/library/typing.html#typing.Any[Any]]_) →  https://docs.python.org/3/library/functions.html#bool[bool] https://github.com/zalando/patroni/blob/9d231aeecdd69f4d06c75a702755fa70d8c2b5f6/patroni/log.py#L245-L280[View on GitHub]link:#patroni.log.PatroniLogger._is_config_changed[];;
  Checks if the given config is different from the current one.
  +
  Parameters:::
    *config* – `+log+` section from Patroni configuration.
  Returns:::
    `+True+` if the config is changed, `+False+` otherwise.
+
_property _queue_size_:  https://docs.python.org/3/library/functions.html#int[int]_link:#patroni.log.PatroniLogger.queue_size[];;
  Number of log records in the queue.
+
_property _records_lost_:  https://docs.python.org/3/library/functions.html#int[int]_link:#patroni.log.PatroniLogger.records_lost[];;
  Number of logging records that have been lost while the queue was full.
+
reload_config(_config:  https://docs.python.org/3/library/typing.html#typing.Dict[Dict][ https://docs.python.org/3/library/stdtypes.html#str[str],  https://docs.python.org/3/library/typing.html#typing.Any[Any]]_) →  https://docs.python.org/3/library/constants.html#None[None] https://github.com/zalando/patroni/blob/9d231aeecdd69f4d06c75a702755fa70d8c2b5f6/patroni/log.py#L390-L437[View on GitHub]link:#patroni.log.PatroniLogger.reload_config[];;
  Apply log related configuration.
  +
  Note
  It is also able to deal with runtime configuration changes.
  +
  Parameters:::
    *config* – `+log+` section from Patroni configuration.
+
run() →  https://docs.python.org/3/library/constants.html#None[None] https://github.com/zalando/patroni/blob/9d231aeecdd69f4d06c75a702755fa70d8c2b5f6/patroni/log.py#L457-L493[View on GitHub]link:#patroni.log.PatroniLogger.run[];;
  Run logger’s thread main loop.
  +
  Keep consuming log queue until requested to quit through `+None+` special log record.
+
shutdown() →  https://docs.python.org/3/library/constants.html#None[None] https://github.com/zalando/patroni/blob/9d231aeecdd69f4d06c75a702755fa70d8c2b5f6/patroni/log.py#L495-L504[View on GitHub]link:#patroni.log.PatroniLogger.shutdown[];;
  Shut down the logger thread.
+
update_loggers(_config:  https://docs.python.org/3/library/typing.html#typing.Dict[Dict][ https://docs.python.org/3/library/stdtypes.html#str[str],  https://docs.python.org/3/library/typing.html#typing.Any[Any]]_) →  https://docs.python.org/3/library/constants.html#None[None] https://github.com/zalando/patroni/blob/9d231aeecdd69f4d06c75a702755fa70d8c2b5f6/patroni/log.py#L210-L243[View on GitHub]link:#patroni.log.PatroniLogger.update_loggers[];;
  Configure custom loggers’ log levels.
  +
  Note
It creates logger objects that are not defined yet in the log manager.
+
Parameters:::
  *config* –
  +
   https://docs.python.org/3/library/stdtypes.html#dict[`+dict+`] object with custom loggers configuration, is set either from:
Example:::
update_loggers({'urllib3.connectionpool': 'WARNING'})
class _patroni.log.ProxyHandler(_patroni_logger: PatroniLogger) View on GitHub

Bases: Handler + Handle log records in place of pending log handlers. + Note

This is used to handle log messages while the logger thread has not started yet, in which case the queue-based handler is not yet started.
+
Variables:;;
  *patroni_logger* – the logger thread.
+
\\__init\\__(_patroni_logger: link:#patroni.log.PatroniLogger[PatroniLogger]_) →  https://docs.python.org/3/library/constants.html#None[None] https://github.com/zalando/patroni/blob/9d231aeecdd69f4d06c75a702755fa70d8c2b5f6/patroni/log.py#L135-L141[View on GitHub]link:#patroni.log.ProxyHandler.\\__init\\__[];;
  Create a new link:#patroni.log.ProxyHandler[`+ProxyHandler+`] instance.
  +
  Parameters:::
    *patroni_logger* – the logger thread.
+
emit(_record:  https://docs.python.org/3/library/logging.html#logging.LogRecord[LogRecord]_) →  https://docs.python.org/3/library/constants.html#None[None] https://github.com/zalando/patroni/blob/9d231aeecdd69f4d06c75a702755fa70d8c2b5f6/patroni/log.py#L143-L151[View on GitHub]link:#patroni.log.ProxyHandler.emit[];;
  Emit each log record that is handled.
  +
  Will push the log record down to `+handle()+` method of the currently configured log handler.
  +
  Parameters:::
    *record* – the record that was emitted.
_class _patroni.log.QueueHandler View on GitHub

Bases: Handler + Queue-based logging handler. +

Variables:

queue – queue to hold log messages that are pending to be flushed to the final destination. +

__init\\__() → None View on GitHub

Queue initialised and initial records_lost established. +

put_record(_record: LogRecord) → None View on GitHub

Asynchronously enqueue a log record. +

Parameters

record – the record to be logged. +

_try_to_report_lost_records() → None View on GitHub

Report the number of log messages that have been lost and reset the counter. + Note

  It will issue an `+WARNING+` message in the logs with the number of lost log messages.
+
emit(record: LogRecord) → None View on GitHub

Handle each log record that is emitted. + Call _put_record() to enqueue the emitted log record. + Also check if we have previously lost any log record, and if so, log a WARNING message. +

Parameters

record – the record that was emitted. +

property _records_lost: int_

Number of log messages that have been lost while the queue was full.

patroni.log.debug_exception(self: Logger, msg: object, *args: Any, **kwargs: Any) → None View on GitHub

Add full stack trace info to debug log messages and partial to others. + Handle exception() calls for self. + Note +

Parameters:
patroni.log.error_exception(self: Logger, msg: object, *args: Any, **kwargs: Any) → None View on GitHub

Add full stack trace info to error messages. + Handle exception() calls for self. + Note +

Parameters:

Previous Next


© Copyright 2015 Compose, Zalando SE. Revision 9d231aee.

Built with Sphinx using a theme provided by Read the Docs.

Read the Docs v: latest

+ Builds