Colourful handlers for Logbook, based on Rich and Journald.
Project description
🦎 ChameleonLog
Colourful logging handlers for Logbook.
ChameleonLog provides colorful, structured logging for Python applications using the Logbook.
📦 Installation
Install ChameleonLog using pip:
pip install chameleon_log
Or using uv:
uv add chameleon_log
🔧 Optional Dependencies
To use the JournaldHandler for sending logs to systemd journald:
pip install chameleon_log[journald]
Or using uv:
uv add chameleon_log --extra journald
This will also install the journald-send package, requiring systemd-based Linux distros.
🚀 Usage
✨ RichHandler
For development and debugging in terminal environments, use RichHandler for colorful, formatted console output:
import logbook
from chameleon_log import RichHandler
# Create a RichHandler with default settings
handler = RichHandler()
with handler:
logger = logbook.Logger(__name__)
logger.info('Application started successfully')
logger.warning('This is a warning message')
logger.error('An error occurred')
The rich_rendering parameter controls Rich formatting:
True: Always use Rich colorful rendering
False: Disable Rich formatting, render plain output
None (default): Auto-detect based on isatty()
Additionally, RichHandler accepts a rich_tracebacks boolean to control how exceptions are rendered:
rich_tracebacks=True: Render exceptions using Rich Traceback objects when terminal rendering is enabled.
rich_tracebacks=False (default): Append the plain-text formatted traceback to the log message. This is useful when logs are captured to files or external systems that do not support Rich rendering.
🖼️ Example output
🐧 JournaldHandler
For applications deployed on Linux servers or in production environments, use JournaldHandler to write logs directly to journald, using its native protocol. This provides more efficient troubleshooting capabilities compared to file-based logging or stdout / stderr capture.
Basic usage:
import logbook
from chameleon_log.journald import JournaldHandler
handler = JournaldHandler(syslog_identifier='my-app')
with handler:
logger = logbook.Logger(__name__)
logger.info('Application started successfully')
logger.warning('This is a warning message')
logger.error('An error occurred')
📝 Simple logging output:
🏗️ With extra fields for structured filtering:
Logbook provides two ways to attach extra fields:
Option 1: Use the extra= parameter (simple and direct)
import logbook
from chameleon_log.journald import JournaldHandler
handler = JournaldHandler(syslog_identifier='my-app')
with handler:
logger = logbook.Logger(__name__)
logger.info('User logged in', extra={'user_id': 123, 'action': 'login'})
Option 2: Use a Processor (for reusable context)
import logbook
from logbook import Logger, Processor
from chameleon_log.journald import JournaldHandler
handler = JournaldHandler()
# or
handler = JournaldHandler(syslog_identifier='my-app')
# Use a Processor to inject context into multiple log records
def inject_request_context(record):
record.extra['user_id'] = 123
record.extra['request_id'] = 'abc-456'
with handler:
logger = logbook.Logger(__name__)
with Processor(inject_request_context):
logger.info('User logged in') # Fields injected automatically
logger.info('Data processed')
View logs with journalctl:
journalctl -fu my-service
journalctl -t my-app F_USER_ID=123
journalctl -eu my-service -o json
Normally, you view your app logs with -u (unit), the syslog_identifier is helpful if your app scatters across many systemd units, you then can use journalctl -t to view all.
📖 Documentation
Full documentation is available at: https://chameleon-log.readthedocs.io
logbook-stubs
If you come here for logbook-stubs source, it is already moved to a separate repository.
📄 License
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Logo by Freepik.
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 chameleon_log-1.7.2.tar.gz.
File metadata
- Download URL: chameleon_log-1.7.2.tar.gz
- Upload date:
- Size: 103.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1d2431b8b30d544deff2a1b9b3fdbca725269b56dd4474e43f6e64a2e8ec2499
|
|
| MD5 |
b5bddfe083f958f5b993de0afba971f0
|
|
| BLAKE2b-256 |
202dcb56c9848eda0106aceaf4a7f31d3b9cfedd146dd4ebb3db1e736b2ad043
|
Provenance
The following attestation bundles were made for chameleon_log-1.7.2.tar.gz:
Publisher:
ci.yml on hongquan/chameleon-log
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
chameleon_log-1.7.2.tar.gz -
Subject digest:
1d2431b8b30d544deff2a1b9b3fdbca725269b56dd4474e43f6e64a2e8ec2499 - Sigstore transparency entry: 1317121695
- Sigstore integration time:
-
Permalink:
hongquan/chameleon-log@f0bc9b8e237bb29e4bd8b366533bdcaee095183a -
Branch / Tag:
refs/tags/v1.7.2 - Owner: https://github.com/hongquan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@f0bc9b8e237bb29e4bd8b366533bdcaee095183a -
Trigger Event:
push
-
Statement type:
File details
Details for the file chameleon_log-1.7.2-py3-none-any.whl.
File metadata
- Download URL: chameleon_log-1.7.2-py3-none-any.whl
- Upload date:
- Size: 16.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a95c27cd5b7252995a5e63836b5af7106332918ca3ce12fdc86f789333c79776
|
|
| MD5 |
b603c8e95630f8e19238237152e0f0a0
|
|
| BLAKE2b-256 |
25180bd61d974117cdde81e66496d64c8b03549ffcafd0b9cce9dfad09c730c7
|
Provenance
The following attestation bundles were made for chameleon_log-1.7.2-py3-none-any.whl:
Publisher:
ci.yml on hongquan/chameleon-log
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
chameleon_log-1.7.2-py3-none-any.whl -
Subject digest:
a95c27cd5b7252995a5e63836b5af7106332918ca3ce12fdc86f789333c79776 - Sigstore transparency entry: 1317121723
- Sigstore integration time:
-
Permalink:
hongquan/chameleon-log@f0bc9b8e237bb29e4bd8b366533bdcaee095183a -
Branch / Tag:
refs/tags/v1.7.2 - Owner: https://github.com/hongquan
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@f0bc9b8e237bb29e4bd8b366533bdcaee095183a -
Trigger Event:
push
-
Statement type: