Skip to main content

Interface to Google Cloud Logging that enables aggregated operation logs

Project description

nqlog

Luca de Alfaro, 2020

Not quite the logger Google wrote, not quite the logger Luca wanted, but at least:

  • It logs to the Google cloud.
  • It groups logs for the same "request" into the same log entry.
  • The severity of the grouped lines is equal to the maximum severity of any line in the group.
  • It avoids running string formatting for strings that are logged below the minimum level.

This seems trivial and normal, but unfortunately, it is no longer the standard behavior. In GC, log lines are no longer automatically grouped together for each request, and even if you figure out how to have them grouped, the group no longer inherits the maximum severity of any line in the group.

The chief limitation of this logger is that it works only for single-threaded services. Extension to multi-threaded services is a work in progress.

Use

Keys to GC

In your "main" file, you need:

import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "keys/google_cloud_keys.json"

where of course you replace the path with the actual one. The keys are json keys to a service account that has logging rights on Google cloud.

Logger use

All you need is:

from nqlog import logthis, logging

Then, you need to decorate the functions whose execution corresponds to a "request". This can be the execution of a web request (e.g., a bottle entry point), or the execution of a task from a scheduler.

@logthis()
def function_to_be_logged():
    ...

Once this is done, whenever inside the function to be logged, or one of the things it calls, you do:

logging.info("This is a log line")

the log line is logged together with all other lines of the request. The grouping is thread-safe, so if you call function_to_be_logged from multiple threads (e.g., to service requests in parallel), everything works as it should.

The logthis decorator takes optional parameters:

Decorator to cause a function to be logged.
:param log_name: Name of the log to be used for logging.
:param resource: resource to be used for logging.  You can create a
    resource e.g. via:
    from google.cloud.logging.resource import Resource
    resource = Resource( type='gae_app',
                labels={
                    "project_id": os.getenv("GOOGLE_CLOUD_PROJECT")
                    "module_id": os.getenv("GAE_MODULE")
                    "service_id": os.getenv("GAE_SERVICE")
                    "version_id": os.getenv("GAE_VERSION")
                   })
:param max_duration: Max duration of a log entry in seconds.
    If the request takes longer than this, the log entry is broken into
    multiple log entries, each of at most max_duration length.
:param level: Minimum level at which logging occurs.

Efficiency

Often, building a log line is computationally intensive, due to the process of converting data types and structures to a string. nqlog contains an optimization. If instead of doing:

logging.debug("The value of my monster data structure is: %r" % monster)

you do:

logging.debug("The value of my monster data structure is: %r", monster)

with a , instead of a %, then nqlog avoids converting monster to its representation unless the log level is DEBUG or lower.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nqlog-0.1.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nqlog-0.1.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file nqlog-0.1.0.tar.gz.

File metadata

  • Download URL: nqlog-0.1.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.7

File hashes

Hashes for nqlog-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2b8a015351d8e07bd77229c8c005de6053a2e14fe18e65f322fcd3cbe8a60239
MD5 1f4bde880e4b83df892da3e7f2f47f01
BLAKE2b-256 56e952ccfd5e6a761cf29b4f198953d36ab42fbd8b1077191db2ee3295898692

See more details on using hashes here.

File details

Details for the file nqlog-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: nqlog-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.7

File hashes

Hashes for nqlog-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e72736ab6d1a2ea87e67302e001d81d4fa2b1884a07e2d83a5792c70af437d1c
MD5 5a0310488ed29f314fbf291f415eb2ee
BLAKE2b-256 f5f94320b6cc78fcc4bc7f424b7f0becaf041638c533e74a28b9ff2ecabf130d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page