Logger Utility
Lila provides a built-in Logger utility for web applications. It writes
buffered, thread-safe log files organized in date folders and keeps file handles open
instead of opening/closing a file per request.
Basic Logging
Import the Logger anywhere in your application to record info, warnings or errors. Each level writes to its own file inside the date folder.
from lila.core.logger import Logger
# Standard logging -> app/logs/YYYY-MM-DD/info.log
Logger.info("Application started successfully.")
Logger.warning("Database connection is slow.") # warning.log
# Errors: passing the exception appends the traceback -> error.log
try:
1 / 0
except Exception as e:
Logger.error("A critical error occurred", exception=e)
# Custom log types -> app/logs/YYYY-MM-DD/audit.log
Logger.log("audit", "User modified their profile")
Request Logging
Logger.format_request() builds a compact one-line entry and
Logger.log_request() writes it to requests.log. The
LoggingMiddleware (enabled with App(logging=True)) calls it
for every non-static request and redacts sensitive query keys
(token, password, _csrf, ...).
from lila.core.logger import Logger
from lila.core.request import Request
async def secure_endpoint(request: Request):
# Logger.request() is async and returns a safe one-line string (it does not log by itself).
log_string = await Logger.request(request)
Logger.info(f"Incoming Request: {log_string}")
# Or write a full request line manually.
Logger.log_request(
client_ip=request.client.host if request.client else "unknown",
method=request.method,
path=request.url.path,
query=request.url.query,
status_code=200,
duration_ms=12.4,
user_agent=request.headers.get("user-agent", ""),
)
Enable it globally from main.py with
App(routes=[...], logging=True). Static assets,
/_lila/, /favicon and /health are skipped.
Log Storage and Rotation
Logs are written to LILA_LOG_DIR when set, otherwise to
app/logs. Lila organizes them into folders by date
(app/logs/YYYY-MM-DD/info.log, error.log,
warning.log, requests.log, plus any custom type).
There is no automatic rotation daemon. Call delete_old_logs(days) from a
cron job / background task; days=30 is the default and
clear_logs() removes everything (also used by the admin panel).
from lila.core.logger import delete_old_logs, clear_logs
# Delete date folders older than 15 days (returns how many were removed)
delete_old_logs(days=15)
# Remove every log folder and file
clear_logs()