By Bernat Sampera 2 min read Follow:

Simple Approach to write readable Logs

Writing Logs is a necessary part of every application, though it can also be tedious. This guide shows how to write minimal logs for an MVP to track what is going on in the app

This guide shows how the steps on how to write logs for an MVP, not to achieve the complete list of events happening but to have readable logs where a human can read what is going on.
It gives much more focus on the Relevant events rather than to track every call made in the app, especially around the calls that contain the huge logic in the app.

Step 1

Write what do you want to log. These example will show the logs for the site translateprompt.com

Logs - What do I want to see?

•  Server Started
•  Migrations needed/not
•  Logic (graph nodes) + user
•  Every translation made + user/ip Requirement
•  New entries/rules
•  Log also total use of tokens every time an invocation is made

Step 2

Set up the logger, here I’ll use Axiom, but any other tool like datadog works also fine.

"""Logging configuration for the backend."""

import logging

import axiom_py
from axiom_py.logging import AxiomHandler

from config import config


def setup_logger():
    client = axiom_py.Client(config.AXIOM_API_TOKEN)
    handler_axiom = AxiomHandler(client, "translateprompt")

    # Get the root logger
    root_logger = logging.getLogger()
    root_logger.addHandler(handler_axiom)
    root_logger.setLevel(logging.INFO)

    # Set specific log levels for noisy third-party libraries
    logging.getLogger("httpx").setLevel(logging.WARNING)
    logging.getLogger("requests").setLevel(logging.WARNING)

    return root_logger

logger = setup_logger()

Then we can import the log and use it like this

from utils.logger import logger
 logger.info(
        f"Translation Graph started. User Id: {state['user_id']}. Lang: {source_language} --> {target_language}. Text to translate: {text_to_translate}"
    )

Step 3

How it looks when the server starts and a basic execution of the translation and a refinement is done

logs for translateprompt.com

Let's connect !!

Get in touch if you want updates, examples, and insights on how AI agents, Langchain and more are evolving and where they’re going next.