On January 1, 2020 this library will no longer support Python 2 on the latest released version. Previously released library versions will continue to be available. For more information please visit Python 2 support on Google Cloud.

Google Container Engine Log Handler

Logging handler for Google Container Engine (GKE).

Formats log messages in a JSON format, so that Kubernetes clusters with the fluentd Google Cloud plugin installed can format their log messages so that metadata such as log level is properly captured.

class google.cloud.logging.handlers.container_engine.ContainerEngineHandler(name=None, stream=None)[source]

Bases: logging.StreamHandler

Handler to format log messages the format expected by GKE fluent.

This handler is written to format messages for the Google Container Engine (GKE) fluentd plugin, so that metadata such as log level are properly set.

  • name (str) – (optional) the name of the custom log in Stackdriver Logging.

  • stream (file-like object) – (optional) stream to be used by the handler.

Initialize the handler.

If stream is not specified, sys.stderr is used.


Format the message into JSON expected by fluentd.


record (LogRecord) – the log record

Return type



A JSON string formatted for GKE fluentd.