Skip to main content

A logging handler for publishing the logs to Cloud Pub/Sub.

pypi build

You can use the pubsub_logging.PubsubHandler to publish the logs to Cloud Pub/Sub. You can use this module with the standard Python logging module.

Supported version

Python 2.7 is supported.

Installation

$ pip install pubsub-logging

How to use

Here is an example configuration file.

[loggers]
keys=root

[handlers]
keys=pubsubHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=NOTSET
handlers=pubsubHandler

[handler_pubsubHandler]
class=pubsub_logging.PubsubHandler
level=DEBUG
formatter=simpleFormatter
args=('projects/tmatsuo-pubsub-sample/topics/log-topic', 1000)

[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s

How to use this config file.

logging.config.fileConfig(os.path.join('examples', 'logging.conf'))
logger = logging.getLogger('root')
logger.info('This message will be buffered')
logger.critical('This message will be flushed.')

Here is a dynamic usage example.

pubsub_handler = PubsubHandler(topic=topic)
pubsub_handler.setFormatter(
    logging.Formatter(
        '%(asctime)s - %(name)s - %(levelname)s - %(message)s'))

logger = logging.getLogger('root')
logger.setLevel(logging.DEBUG)
logger.addHandler(pubsub_handler)
logger.info('This message will be buffered')
logger.critical('This message will be flushed.')

The logs are buffered by default, and when the buffer is full, or when the message has a log level higher or equal to flush_level, the buffered logs will be flushed and sent to Cloud Pub/Sub.

Authentication

The module uses the Application Default Credentials. You can configure the authentication as follows.

Authentication on App Engine

It should work out of the box. If you’re getting an authorization error, please make sure that your App Engine service account has an Editor or greater permission on your Cloud project.

Authentication on Google Compute Engine

When creating a new instance, please add the Cloud Pub/Sub scope https://www.googleapis.com/auth/pubsub to the service account of the instance.

Authentication anywhere else

As the documentation suggests, create a new service account and download its JSON key file, then set the environment variable GOOGLE_APPLICATION_CREDENTIALS pointing to the JSON key file. Please note that this service account must have Editor or greater permissions on your Cloud project.

Metadata

Release files for pubsub-logging 0.1.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pubsub-logging 0.1.6
File Size Uploaded
pubsub-logging-0.1.6.tar.gz 5.2 kB Details

Release files / pubsub-logging-0.1.6.tar.gz

Download URL pubsub-logging-0.1.6.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
9f46d47e96c8a36fcad65c67542e0a4d8cca61ae09ab178c7741fdff44829a9a
BLAKE2b-256 checksum
How to use checksums
6c74e15307b6b6b0aab09a5456cb2a8a7e5e804623aa9a1440c84b9d300b1348
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

0.2.1

1 release file

0.2.0

1 release file

This release

0.1.6 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page