logfire icon indicating copy to clipboard operation
logfire copied to clipboard

Format the console log

Open NourEldin-Osama opened this issue 8 months ago • 5 comments

Description

I want to format the console log like loguru i can't find any function that allow me to change the format

also when i integrate logfire with loguru I can't format the console log i want the console log to be the same as default loguru

LOGURU_FORMAT = (
    "<green>{time:YYYY-MM-DD HH:mm:ss.SSS Z}</green> | "
    "<level>{level: <8}</level> | "
    "<cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>"
)
import logfire
from loguru import logger
from pathlib import Path

logfire.configure()
logger.configure(handlers=[logfire.loguru_handler()])
LOG_DIR = Path("logs")
logger.add(
    LOG_DIR / "transcriber.log",
    level="INFO",
    rotation="1 week",
    backtrace=True,
    diagnose=True,
)

NourEldin-Osama avatar Mar 19 '25 17:03 NourEldin-Osama

any help?

NourEldin-Osama avatar Mar 21 '25 22:03 NourEldin-Osama

Sorry for the slow reply, I'll reply tomorrow.

samuelcolvin avatar Mar 21 '25 23:03 samuelcolvin

@NourEldin-Osama I think you're right that we don't support this at the moment.

I'm sure we can fix logfire so loguru continues to behave as it would if logfire wasn't installed, but I'm not sure we'll allow the level of customisation in logfire terminal output that you're asking about.

@alexmojaki what do you think?

samuelcolvin avatar Mar 22 '25 11:03 samuelcolvin

I agree that customising the console format would be good (also requested in https://github.com/pydantic/logfire/issues/486 and I think elsewhere) but is tricky and might not happen soon.

Loguru does still work with logfire installed, e.g. the code above works in the sense that it writes to LOG_DIR / "transcriber.log". The first problem is that loguru.logger.configure replaces the existing loguru console handler. Using logger.add(**logfire.loguru_handler()) will keep the default so you get loguru in the console.

The next problem is that this will log to console twice, e.g:

11:57:03.895 Hello, World!
2025-03-22 13:57:03.895 | INFO     | __main__:<module>:17 - Hello, World!

logfire.configure(console=False) will prevent the duplication but means that other logfire/OTel logs/spans won't be logged to the console.

Our structlog LogfireProcessor has a parameter console_log: bool = False, I'm surprised we don't have the same for loguru which would be a decent solution. It should be easy to add.

alexmojaki avatar Mar 22 '25 12:03 alexmojaki

For anyone facing the same issue,

Here’s a fully functional minimal example to use as a workaround until console format customization is supported:

import sys
import logfire
from loguru import logger
from pathlib import Path

# Configure logfire with console logging disabled
logfire.configure(console=False)

# Add a console handler for all logs
logger.add(sys.stderr, level="INFO")

# Add Logfire's log handler
logfire_sink = logfire.loguru_handler()["sink"]
logger.add(logfire_sink, level="TRACE")

# Set up log file directory and add a file handler
LOG_DIR = Path("logs")
logger.add(
    LOG_DIR / "transcriber.log",
    level="INFO",
    rotation="1 week",
    backtrace=True,
    diagnose=True,
)

NourEldin-Osama avatar Mar 22 '25 20:03 NourEldin-Osama