A simple Python logging wrapper for Better Stack (Logtail) with automatic environment context.
-
Install package
pip install fewsats-logger
-
Set environment variables (in .env file or environment)
BETTER_STACK_SOURCE_TOKEN=your_source_token BETTER_STACK_HOST=your_source_host ENV=dev
IMPORTANT: Use dev and prod for ENV values.
Get the host & token by creating a new source at https://telemetry.betterstack.com/
-
Initialize once in your main file
import os from dotenv import load_dotenv from fewsats_logger.core import setup_logging load_dotenv() setup_logging(env=os.environ.get("ENV", "dev"), host=os.environ.get("BETTER_STACK_HOST"), token=os.environ.get("BETTER_STACK_SOURCE_TOKEN"))
-
Use standard logging anywhere in your code
import logging logger = logging.getLogger(__name__) logger.info("Message") logger.error("Error", extra={"order_id": "12345"}) # Add filterable fields
That's it. Logs will appear in Better Stack dashboard and console.
logger.debug(): Detailed debugging informationlogger.info(): Confirmation that things are workinglogger.warning(): Something unexpected happenedlogger.error(): A more serious problemlogger.critical(): A fatal error that prevents operation
logger.info("Processing order", extra={
"external_id": "12345",
"customer_id": "67890"
})you can add it to multiple statements like this:
with logtail.context(order={"external_id": "12345" }):
logger.info('Processing order')
# More code here ...
logger.info('Done.')- The server may get blocked when started with
python server/main.py - But works fine with
uvicorn server.main:app --reload --host 0.0.0.0 - This appears to be related to logging in the
__main__scope - Avoid using logging statements directly in the main module scope or in
if __name__ == "__main__":blocks