Context
The CalCOFI Plumber API has no usage logging. Add a request logging filter to capture endpoint usage over time.
Implementation
Add a plumber middleware filter before pr_run() in the run script or plumber.R:
pr |> pr_filter("logger", function(req, res) {
cat(
jsonlite::toJSON(list(
ts = format(Sys.time(), "%Y-%m-%dT%H:%M:%SZ"),
method = req$REQUEST_METHOD,
path = req$PATH_INFO,
query = req$QUERY_STRING,
ip = req$REMOTE_ADDR
), auto_unbox = TRUE), "\n",
file = "/var/log/calcofi-api.log",
append = TRUE
)
plumber::forward()
})
GCS Sync
Periodically sync /var/log/calcofi-api.log to a GCS bucket for long-term retention:
googleCloudStorageR::gcs_upload(
"/var/log/calcofi-api.log",
bucket = "calcofi-logs",
name = paste0("api/", Sys.Date(), ".log")
)
Consider the logger package for structured JSON output and log rotation.
Context
The CalCOFI Plumber API has no usage logging. Add a request logging filter to capture endpoint usage over time.
Implementation
Add a plumber middleware filter before
pr_run()in the run script orplumber.R:GCS Sync
Periodically sync
/var/log/calcofi-api.logto a GCS bucket for long-term retention:Consider the
loggerpackage for structured JSON output and log rotation.