A JSON logger with OpenTelemetry and Loki support
Project description
JSON Logger Loki
A Python logging utility for logging JSON formatted logs, integrated with Loki for log aggregation. This project uses uv to manage dependencies, testing, and workflows.
Table of Contents
About
json-logger-loki is a Python-based logging utility that outputs logs in a structured JSON format. This log format is useful for log aggregation systems like Loki.
The project is managed using uv, which simplifies setting up Python virtual environments, installing dependencies, and running the project.
Installation
Prerequisites
Make sure you have uv installed. If not, install it by running:
pip install uv
Setup
-
Clone the repository:
git clone https://github.com/rafaelbrandaof/json_logger_loki.git cd json_logger_loki
-
Install dependencies using
uv:uv venv # Creates a virtual environment uv pip install -e . # Installs the dependencies in the current environment
Usage
After setting up the project, you can use the JSONLogger class to log messages in JSON format.
Example:
from json_logger.logger import JSONLogger
# Initialize the logger
logger = JSONLogger(log_file="test.log", loki_url="http://localhost:3100/loki/api/v1/push", loki_labels={"job": "test_logger"})
# Log an info message
logger.info("This is an info log", module="test")
# Log an error message
logger.error("This is an error log", module="test")
Logs will be written to the specified log file in JSON format, and if Loki is configured, the logs will be pushed to the Loki server.
Testing
To run tests, use uv to set up the virtual environment and install dependencies:
uv venv # Creates a virtual environment
uv pip install -e . # Installs dependencies
uv run pytest # Run tests with pytest
Linting and Code Style
To ensure your code follows Python's PEP-8 style guide, we use flake8 and pylint for linting.
You can run these tools manually:
flake8 . # Run flake8 for linting
pylint . # Run pylint for linting
Or, these linters will run automatically in the GitHub Actions workflows.
GitHub Actions Workflows
The project includes GitHub Actions workflows that automate testing and linting.
Workflow: python-lint.yml
This workflow is triggered on every push and pull request to the main branch.
It does the following:
- Installs Python 3.12 (as defined in the matrix).
- Installs dependencies using
uv. - Runs tests with
pytest. - Lints the code with flake8 and pylint.
You can find the workflow file in .github/workflows/python-lint.yml.
Here’s the content of the file:
name: Python Linting and Testing
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.12"]
steps:
- uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v3
with:
enable-cache: true
cache-dependency-glob: uv.lock
- name: Set up Python ${{ matrix.python-version }}
run: uv python install ${{ matrix.python-version }}
- name: Install Venv
run: uv venv
- name: Install Dependencies
run: uv pip install -e .
- name: Test
run: uv run pytest
- name: Flake 8
run: flake8 .
- name: Pylint
run: pylint .
Additional Workflows
- Continuous Integration (CI): The workflow ensures that tests are run and that the code adheres to the linting standards before being merged into the
mainbranch. - Deployment: In the future, you can add another workflow for automatic deployment to a server or PyPI.
Contributing
If you want to contribute to this project, follow these steps:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Write tests for your changes.
- Ensure that tests pass by running
pytestand linting usingflake8andpylint. - Create a pull request with a detailed description of your changes.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file json_logger_loki-0.2.0.tar.gz.
File metadata
- Download URL: json_logger_loki-0.2.0.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
acc82bacb3c0a96a701dab59487250a8869632c65c86e015011071cfb683bf6d
|
|
| MD5 |
1ddf5f36815252c693bc0a7decfa376b
|
|
| BLAKE2b-256 |
6f365338ad812aabb104fbb21bb8278971f8190ceaf9d09fad506aef9af0f6dd
|
File details
Details for the file json_logger_loki-0.2.0-py3-none-any.whl.
File metadata
- Download URL: json_logger_loki-0.2.0-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
976c0d62efcaadd55893ce4b40044953df94036c55049350e20d0ec1e16b95ec
|
|
| MD5 |
c01061d3f2d51db06961aa4f2a74f1e6
|
|
| BLAKE2b-256 |
636b1aa35c77c7b249685914c811b5198ed68df3ca096ca5fc7cdaf04dce7844
|