rays/observability/log
Write logs from a Worker or Durable Object to
Workers Logs,
wrangler tail, and the wrangler dev terminal.
Each level is written with the matching console method, so logs show at
the right severity in the Cloudflare dashboard. Logs with fields are
written as JSON objects, which Workers Logs indexes field by field, so you
can filter on user_id instead of searching message text.
import gleam/json
import rays/observability/log
log.info("Cache warmed", [])
log.warning("Rate limit reached", [
#("user_id", json.int(42)),
#("limit", json.int(100)),
])
// console.warn({ user_id: 42, limit: 100, message: "Rate limit reached" })
To add the same fields to every log for a request, such as its ID, build
a Logger and pass it to the code handling the request:
let logger =
log.new()
|> log.with_fields([#("request_id", json.string(request_id))])
log.log(logger, log.Info, "User signed in", [#("user_id", json.int(42))])
// console.info({ request_id: "…", user_id: 42, message: "User signed in" })
A Logger is a plain value, so concurrent requests in the same isolate
each keep their own fields.
Workers Logs already records every request’s method, URL, status, and
uncaught exceptions, so there is no need to log those yourself. To see
logs from a deployed Worker, enable observability in its Wrangler
configuration.
Types
How severe a log is. Each level is written with its own console method.
pub type Level {
Debug
Info
Warning
Error
}
Constructors
-
DebugDetail for debugging, written with
console.debug. -
InfoNormal operation, written with
console.info. -
WarningSomething unexpected that the Worker recovered from, written with
console.warn. -
ErrorA failure, written with
console.error.
Values
pub fn debug(
message: String,
fields: List(#(String, json.Json)),
) -> Nil
Write a debug log. The same as log(new(), Debug, message, fields).
pub fn error(
message: String,
fields: List(#(String, json.Json)),
) -> Nil
Write an error log. The same as log(new(), Error, message, fields).
pub fn info(
message: String,
fields: List(#(String, json.Json)),
) -> Nil
Write an info log. The same as log(new(), Info, message, fields).
Examples
log.info("Cache warmed", [#("entries", json.int(120))])
pub fn is_enabled(level: Level) -> Bool
Whether logs at the given level are written under the level set with
set_level. Use it to skip building fields that are costly to compute.
pub fn log(
logger: Logger,
level: Level,
message: String,
fields: List(#(String, json.Json)),
) -> Nil
Write a log at the given level with the logger’s fields and the given
fields. Nothing is written if the level is below the one set with
set_level.
With no fields at all, the message is written as a plain string.
Otherwise the log is a JSON object holding the fields and the message
under the "message" key. Where names clash, the given fields replace the
logger’s, and the message replaces a field named "message".
Examples
log.log(logger, log.Info, "User signed in", [
#("user_id", json.int(42)),
#("method", json.string("passkey")),
])
pub fn set_level(level: Level) -> Nil
Only write logs at the given level or more severe ones. For example,
set_level(Info) drops debug logs. The default is Debug, which writes
everything.
The level applies to the whole isolate, which serves many requests, so
call this when the module loads, for example in main, rather than in a
handler. Dropping debug logs in production saves on the 256 KB of logs
each invocation may write and on Workers Logs events.
Examples
pub fn main() -> worker.Worker {
log.set_level(log.Info)
worker.new(handle_request)
}
pub fn warning(
message: String,
fields: List(#(String, json.Json)),
) -> Nil
Write a warning log. The same as log(new(), Warning, message, fields).