orangeqs.juice.service._logging#

Handlers and Utilities for Service Logging.

Module Contents#

Classes#

JuiceQueueListener

Queue listener that stops with a time limit.

JuiceQueueHandler

Queue handler that passes records to target in a background thread.

JuiceInfluxLoggingHandler

JuiceInfluxLoggingHandler instances dispatch logging events to influx.

Functions#

influx_queue_handler

Return a queue handler that writes the service logs to InfluxDB.

Data#

API#

orangeqs.juice.service._logging.QUEUE_SIZE#

1000

orangeqs.juice.service._logging.STOP_TIMEOUT_S#

3

class orangeqs.juice.service._logging.JuiceQueueListener(log_queue: queue.Queue[logging.LogRecord], *handlers: logging.Handler)#

Bases: logging.handlers.QueueListener

Queue listener that stops with a time limit.

Podman kills a service 10 s after asking it to stop, so on stop the listener gets STOP_TIMEOUT_S to flush the queue. The records still waiting after that are dropped and their number is reported on stderr.

start() → None#

Start the listener.

This starts up a background thread to monitor the queue for LogRecords to process.

stop() → None#

Stop the listener.

This asks the thread to terminate, and then waits for it to do so. Note that if you don’t call this before your application exits, there may be some records still left on the queue, which won’t be processed.

is_thread_alive() → bool#
class orangeqs.juice.service._logging.JuiceQueueHandler(target: logging.Handler)#

Bases: logging.handlers.QueueHandler

Queue handler that passes records to target in a background thread.

Logging never waits for target, so a slow handler (such as the InfluxDB one) does not block the event loop. The queue is bounded: when it is full, new records are dropped and one warning is printed to stderr. The number of dropped records is printed by the first log call after the queue drained to half, or by close().

filter(record: logging.LogRecord) → bool#

Determine if a record is loggable by consulting all the filters.

The default is to allow the record to be logged; any filter can veto this by returning a false value. If a filter attached to a handler returns a log record instance, then that instance is used in place of the original log record in any further processing of the event by that handler. If a filter returns any other true value, the original log record is used in any further processing of the event by that handler.

If none of the filters return false values, this method returns a log record. If any of the filters return a false value, this method returns a false value.

.. versionchanged:: 3.2

Allow filters to be just callables.

.. versionchanged:: 3.12 Allow filters to return a LogRecord instead of modifying it in place.

enqueue(record: logging.LogRecord) → None#

Enqueue a record.

The base implementation uses put_nowait. You may want to override this method if you want to use blocking, timeouts or custom queue implementations.

prepare(record: logging.LogRecord) → logging.LogRecord#

Prepare a record for queuing. The object returned by this method is enqueued.

The base implementation formats the record to merge the message and arguments, and removes unpickleable items from the record in-place. Specifically, it overwrites the record’s msg and message attributes with the merged message (obtained by calling the handler’s format method), and sets the args, exc_info and exc_text attributes to None.

You might want to override this method if you want to convert the record to a dict or JSON string, or send a modified copy of the record while leaving the original intact.

close() → None#

Tidy up any resources used by the handler.

This version removes the handler from an internal map of handlers, _handlers, which is used for handler lookup by name. Subclasses should ensure that this gets called from overridden close() methods.

orangeqs.juice.service._logging.influx_queue_handler(*, bucket: str, service: str) → orangeqs.juice.service._logging.JuiceQueueHandler#

Return a queue handler that writes the service logs to InfluxDB.

class orangeqs.juice.service._logging.JuiceInfluxLoggingHandler(*, bucket: str, service: str)#

Bases: logging.Handler

JuiceInfluxLoggingHandler instances dispatch logging events to influx.

There is no need to set a Formatter. The raw input will be passed on to the influx write api.

DEFAULT_LOG_RECORD_KEYS#

None

close() → None#

Close the write_api, client and logger.

emit(record: logging.LogRecord) → None#

Emit a record via the influxDB WriteApi.