fluent-logger-python is a Python library used to record events from Python applications to Fluentd or Fluent Bit. It provides both an event-based interface (`FluentSender`) and a standard Python `logging.Handler` (`FluentHandler`) for structured logging. The current version is 0.11.1 and it is actively maintained with regular releases.
pip install fluent-loggerVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to configure the standard Python `logging` module to send structured logs to Fluentd using `FluentHandler` and `FluentRecordFormatter`. It includes capturing extra contextual data and exception tracebacks.
Upgrade your Python environment to 3.7+ before upgrading fluent-logger to v0.11.0 or newer.
For critical logging paths requiring delivery confirmation, consider using `fluent.sender.FluentSender` directly and calling its `emit()` method, which returns `True` on success or `False` on error. You can then use `logger.last_error()` to retrieve error details.
If you relied on silent error suppression during serialization, you might need to adjust your error handling logic or explicitly set `forward_packet_error=False` when initializing `FluentSender` or `FluentHandler` (if supported directly). It's generally recommended to handle these exceptions for robustness.
Explicitly define all desired fields in your `FluentRecordFormatter`'s `fmt` dictionary. For dynamic `extra` fields, configure `FluentRecordFormatter` with `exclude_attrs` to control which default `LogRecord` attributes are logged, or handle the merging in a custom formatter or `buffer_overflow_handler` if the default behavior is not suitable. Ensure your Fluentd configuration expects the structure sent by the formatter.
For more control, instantiate `FluentSender` objects directly. `FluentSender` now supports `None` as a root tag, allowing multiple root tags within a single connection. This provides greater flexibility and isolation between loggers.
Install the library using pip: `pip install fluent-logger`
Ensure Fluentd/Fluent Bit is running and configured to listen on the specified host and port (default 24224). Verify network connectivity and firewall rules. Example Fluentd configuration: `<source> @type forward port 24224 </source>`.
Ensure all strings sent to `fluent-logger` are consistently encoded (e.g., UTF-8). If using `FluentRecordFormatter`, verify that the incoming log records' encoding matches the expected decoding in Fluentd/Fluent Bit, and consider using appropriate `encoding` parameters or filtering in Fluentd/Fluent Bit if the issue persists downstream. For direct `emit` calls, ensure dictionary values are properly encoded Python strings.
When creating a custom stream-like object for logging, ensure it implements a `flush` method, even if it's a no-op. For standard `FluentHandler` usage, ensure it's initialized correctly as a logging handler without conflicts from other custom stream configurations that might implicitly require `flush`.