Skip to main content

Centralized logging made simple using MongoDB.

Project description

CI status Coverage status PyPI version

MongoLog is a Python logging handler that stores standard logging records in a MongoDB collection. It keeps the normal log record fields and adds useful context such as host, current user, UTC time, rendered message text, and formatted exception tracebacks.

MongoLog supports Python 3.9 and newer. CI tests against MongoDB 5, 6, 7, and 8.

Installation

Install from PyPI:

$ python -m pip install mongolog

Or install the latest source checkout:

$ python -m pip install git+https://github.com/puentesarrin/mongodb-log.git

MongoDB setup

Create a collection for log records. A capped collection is useful for log data because older records automatically roll out when the collection reaches its configured size:

> use mongolog
> db.createCollection('log', {capped: true, size: 100000})

Usage

Add MongoHandler to any standard Python logger:

import logging

from mongolog import MongoHandler


log = logging.getLogger('demo')
log.setLevel(logging.DEBUG)
log.addHandler(MongoHandler.to('log', db='mongolog'))

log.debug('Some message')

Structured messages remain queryable in MongoDB because MongoLog stores the original record.msg value:

log.info({'address': '340 N 12th St', 'state': 'PA', 'country': 'US'})

Development

Install development tools and run the test suite with tox:

$ python -m pip install -e ".[dev]"
$ tox

The tests use a real MongoDB server. By default they connect to localhost:27017. Override that with MONGO_HOST and MONGO_PORT:

$ MONGO_HOST=localhost MONGO_PORT=27017 tox

Run the same tests directly with unittest:

$ python -m unittest discover -s tests -p 'test_*.py'

To test against specific Python interpreters installed on your machine:

$ tox -e py39,py310,py311,py312,py313,py314

Releases

CI builds the source distribution and wheel on every push. Tag pushes publish to PyPI through GitHub Actions Trusted Publishing using the pypi environment.

License

MongoLog is available under the BSD 2-Clause License.

Authors

Original author: Andrei Savu

Maintainer: Jorge Puente Sarrín

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

mongolog-0.2.0.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

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

mongolog-0.2.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file mongolog-0.2.0.tar.gz.

File metadata

  • Download URL: mongolog-0.2.0.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mongolog-0.2.0.tar.gz
Algorithm Hash digest
SHA256 422b3b57304791a8106d4078255bf30f6efe2f9e183b92ff9703d82a988fecfa
MD5 220964a976de97561d59d24e303c73ad
BLAKE2b-256 8e3a3064bcdc389321e2f32fe9f54f7d88fd79cf44c13c87ac66cb9fb49438df

See more details on using hashes here.

Provenance

The following attestation bundles were made for mongolog-0.2.0.tar.gz:

Publisher: ci.yml on puentesarrin/mongodb-log

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mongolog-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: mongolog-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mongolog-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fe6b245115db03fe45497aa252a1361f1e10022bf15cd6b2a21d6a0d97451d67
MD5 031220df8881b65b3347898f7c7b5b5d
BLAKE2b-256 e00352256784edfb85d1c4f90d8da49d896919b25c83915c519ec7de74dd59cb

See more details on using hashes here.

Provenance

The following attestation bundles were made for mongolog-0.2.0-py3-none-any.whl:

Publisher: ci.yml on puentesarrin/mongodb-log

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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