Skip to content

Commit 1aeb926

Browse files
committed
log rotation based on env variable
1 parent 3284615 commit 1aeb926

3 files changed

Lines changed: 24 additions & 2 deletions

File tree

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ UV := uv
55
PIP := $(BIN)/pip
66
LOG_FILE := runtime-logs.log
77
IRI_LOG_FILE ?= $(LOG_FILE)
8+
LOG_ROTATION_DAYS := 5
9+
IRI_LOG_ROTATION_DAYS ?= $(LOG_ROTATION_DAYS)
810

911
STAMP_VENV := $(VENV)/.created
1012
STAMP_DEPS := $(VENV)/.deps
@@ -37,6 +39,7 @@ dev: deps
3739
IRI_API_ADAPTER_filesystem=app.demo_adapter.DemoAdapter \
3840
IRI_API_ADAPTER_task=app.demo_adapter.DemoAdapter \
3941
IRI_LOG_FILE="$${IRI_LOG_FILE:-$${LOG_FILE:-$(IRI_LOG_FILE)}}" \
42+
IRI_LOG_ROTATION_DAYS="$${IRI_LOG_ROTATION_DAYS:-$${LOG_ROTATION_DAYS:-$(IRI_LOG_ROTATION_DAYS)}}" \
4043
DEMO_QUEUE_UPDATE_SECS=2 \
4144
OPENTELEMETRY_ENABLED=true \
4245
API_URL_ROOT='http://localhost:8000' fastapi dev

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ Links to data, created by this api, will concatenate these values producing link
6262
- `LOG_LEVEL`: logging level for the API and adapters. Defaults to `DEBUG`.
6363
- `IRI_LOG_FILE`: file path for API logs. Logs always go to stdout; when this is set, logs also go to the file.
6464
- `LOG_FILE`: fallback file path for API logs when `IRI_LOG_FILE` is not set.
65+
- `IRI_LOG_ROTATION_DAYS`: number of daily rotated log files to retain. Defaults to `5`.
66+
- `LOG_ROTATION_DAYS`: fallback retention when `IRI_LOG_ROTATION_DAYS` is not set.
6567

66-
For local development, `make` writes logs to `runtime-logs.log` by default. Use `make LOG_FILE=/tmp/iri-api.log` or `make IRI_LOG_FILE=/tmp/iri-api.log` to choose a different file. You can also put either variable in `local.env`.
68+
For local development, `make` writes logs to `runtime-logs.log` by default and keeps `5` daily rotated files. Use `make LOG_FILE=/tmp/iri-api.log`, `make IRI_LOG_FILE=/tmp/iri-api.log`, or `make LOG_ROTATION_DAYS=10` to override those defaults. You can also put the same variables in `local.env`.
6769

6870
## Docker support
6971

app/apilogger.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
import os
55
import sys
6+
from logging.handlers import TimedRotatingFileHandler
67
from pathlib import Path
78

89
LEVELS = {"FATAL": logging.FATAL,
@@ -14,6 +15,7 @@
1415
DEFAULT_FORMAT = "%(asctime)s.%(msecs)03d - %(name)s - %(levelname)s - %(message)s"
1516
DEFAULT_DATE_FORMAT = "%a, %d %b %Y %H:%M:%S"
1617
IRI_HANDLER_ATTR = "_iri_facility_api_handler"
18+
DEFAULT_ROTATION_DAYS = 5
1719

1820
_CONFIGURED = False
1921

@@ -29,6 +31,15 @@ def _log_file_path() -> Path | None:
2931
return Path(log_file) if log_file else None
3032

3133

34+
def _rotation_days() -> int:
35+
raw_days = os.environ.get("IRI_LOG_ROTATION_DAYS") or os.environ.get("LOG_ROTATION_DAYS")
36+
try:
37+
days = int(raw_days) if raw_days is not None else DEFAULT_ROTATION_DAYS
38+
except ValueError:
39+
days = DEFAULT_ROTATION_DAYS
40+
return max(days, 0)
41+
42+
3243
def configure_logging(level: str | int | None = None) -> None:
3344
"""
3445
Configure root logging for the API.
@@ -65,7 +76,13 @@ def configure_logging(level: str | int | None = None) -> None:
6576
if log_file:
6677
if log_file.parent != Path("."):
6778
log_file.parent.mkdir(parents=True, exist_ok=True)
68-
file_handler = logging.FileHandler(log_file)
79+
file_handler = TimedRotatingFileHandler(
80+
log_file,
81+
when="midnight",
82+
interval=1,
83+
backupCount=_rotation_days(),
84+
encoding="utf-8",
85+
)
6986
file_handler.setLevel(log_level)
7087
file_handler.setFormatter(formatter)
7188
setattr(file_handler, IRI_HANDLER_ATTR, True)

0 commit comments

Comments
 (0)