Python logging library for ingesting logs into LogiScout
Project description
LogiScout Logger
Python logging library for ingesting logs into LogiScout. Built on top of structlog to provide context-rich, structured logs with intelligent batching.
Features
- Structured Logging: JSON-formatted logs with timestamps, levels, and metadata
- Intelligent Batching: Automatically batches logs (200 logs or 30 seconds) to reduce network overhead
- Correlation ID Tracking: Automatic request correlation across your application
- Environment Support: DEV (console only) and PROD (console + remote) modes
- Confidential Logging: Mark sensitive logs to prevent them from being sent remotely
- Framework Support: Built-in middleware for FastAPI, Flask, and other ASGI/WSGI frameworks
Installation
pip install logiscout-logger
Quick Start
Basic Usage
from logiscout_logger import init, get_logger, PROD, DEV
# Initialize the logger
init(
endpoint="https://api.logiscout.com/logs",
service_name="my-service",
env=PROD # Use DEV for local development (no remote sending)
)
# Get a logger instance
logger = get_logger(__name__)
# Log messages
logger.info("User logged in", user_id=123)
logger.warning("Rate limit approaching", current=95, limit=100)
logger.error("Payment failed", order_id="abc-123", reason="insufficient_funds")
FastAPI Integration
from fastapi import FastAPI
from logiscout_logger import init, get_logger, asgiConfiguration, PROD
app = FastAPI()
# Initialize LogiScout
init(
endpoint="https://api.logiscout.com/logs",
service_name="my-fastapi-app",
env=PROD
)
# Add middleware for automatic correlation ID tracking
app.add_middleware(asgiConfiguration)
logger = get_logger("api")
@app.get("/users/{user_id}")
async def get_user(user_id: int):
logger.info("Fetching user", user_id=user_id)
# Your logic here
return {"user_id": user_id}
Flask Integration
from flask import Flask
from logiscout_logger import init, get_logger, wsgiConfiguration, PROD
app = Flask(__name__)
# Initialize LogiScout
init(
endpoint="https://api.logiscout.com/logs",
service_name="my-flask-app",
env=PROD
)
# Apply WSGI middleware
app.wsgi_app = wsgiConfiguration(app.wsgi_app)
logger = get_logger("api")
@app.route("/users/<int:user_id>")
def get_user(user_id):
logger.info("Fetching user", user_id=user_id)
return {"user_id": user_id}
Django Integration
1. Initialize in settings.py:
from logiscout_logger import init, PROD, DEV
init(
endpoint="https://api.logiscout.com/logs",
service_name="my-django-app",
env=PROD # Use DEV for local development
)
2. Apply middleware in wsgi.py:
import os
from django.core.wsgi import get_wsgi_application
from logiscout_logger import wsgiConfiguration
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
application = get_wsgi_application()
application = wsgiConfiguration(application)
If running Django with an ASGI server (e.g., Uvicorn), apply the middleware in asgi.py instead:
import os
from django.core.asgi import get_asgi_application
from logiscout_logger import asgiConfiguration
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
application = get_asgi_application()
application = asgiConfiguration(application)
3. Use in views:
from logiscout_logger import get_logger
logger = get_logger(__name__)
def my_view(request):
logger.info("Processing request", user_id=request.user.id)
return JsonResponse({"status": "ok"})
Environment Modes
Development Mode (DEV)
In DEV mode, logs are only printed to the console. No logs are sent to the remote endpoint.
from logiscout_logger import init, DEV
init(
endpoint="https://api.logiscout.com/logs",
service_name="my-service",
env=DEV # Logs only go to console
)
Production Mode (PROD)
In PROD mode, logs are printed to the console AND sent to the remote endpoint with intelligent batching.
from logiscout_logger import init, PROD
init(
endpoint="https://api.logiscout.com/logs",
service_name="my-service",
env=PROD # Logs go to console AND remote
)
Confidential Logging
For sensitive data that should not be sent to the remote server, use the send=False parameter:
logger = get_logger(__name__)
# This log will be sent to the remote server
logger.info("User authenticated", user_id=123)
# This log will ONLY appear in the console (not sent remotely)
logger.info("Password reset token generated", token="secret-token", send=False)
# Works with all log levels
logger.debug("Sensitive debug info", data=sensitive_data, send=False)
logger.error("Internal error details", stack_trace=trace, send=False)
Log Levels
logger.debug("Detailed debug information")
logger.info("General information")
logger.warning("Warning message")
logger.error("Error message")
logger.critical("Critical error message")
Adding Metadata
Add any additional context to your logs:
# Inline metadata
logger.info("Order created", order_id="123", total=99.99, currency="USD")
# Bound logger with persistent context
user_logger = logger.bind(user_id=123, session_id="abc")
user_logger.info("User action", action="click") # Includes user_id and session_id
Standalone Usage
You can use logiscout-logger without connecting to a remote service. Logs will only go to the console:
from logiscout_logger import get_logger
# No init() call needed for console-only logging
logger = get_logger("my_script")
logger.info("Script started")
logger.debug("Processing data", count=100)
logger.warning("Disk space low", available_gb=1.5)
API Reference
init()
Initialize the LogiScout logger.
init(
endpoint: str, # Remote logging endpoint URL
service_name: str, # Service identifier
env: Environment # DEV or PROD
)
get_logger()
Get a logger instance.
logger = get_logger(name: str) # Usually __name__
Logger Methods
logger.debug(msg: str, send: bool = True, **kwargs)
logger.info(msg: str, send: bool = True, **kwargs)
logger.warning(msg: str, send: bool = True, **kwargs)
logger.error(msg: str, send: bool = True, **kwargs)
logger.critical(msg: str, send: bool = True, **kwargs)
Middleware
from logiscout_logger import asgiConfiguration, wsgiConfiguration
# For ASGI (FastAPI, Starlette, Django with ASGI)
app.add_middleware(asgiConfiguration)
# For WSGI (Flask, Django with WSGI)
app.wsgi_app = wsgiConfiguration(app.wsgi_app)
Requirements
- Python 3.9+
- structlog >= 24.0.0
- requests >= 2.28.0
License
MIT License - see LICENSE for details.
Support
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 logiscout_logger-0.1.1.tar.gz.
File metadata
- Download URL: logiscout_logger-0.1.1.tar.gz
- Upload date:
- Size: 15.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
50b01fdcd0b653989d1ad7919320d24925ce8e5a33a773bbc73476fbb54e01d2
|
|
| MD5 |
cd6f1df1037ad2a7542a8c976c9d2efe
|
|
| BLAKE2b-256 |
6088b5c53a4b90e33083e9085342716b71a06fe718e9bb5a8195a13955bfead5
|
File details
Details for the file logiscout_logger-0.1.1-py3-none-any.whl.
File metadata
- Download URL: logiscout_logger-0.1.1-py3-none-any.whl
- Upload date:
- Size: 23.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
05a916af2425f3eddfd6c8f651aa68237fbc48194ff0af38bcc1b9559d8b1df1
|
|
| MD5 |
9b5769edc5cbf1d6756e2bd1c767394f
|
|
| BLAKE2b-256 |
32090ecf3c0a16a25c80b9a1ea2bee56dd793b5259b16de4c9e9a2109235300e
|