Skip to content

Write a condition in the shape Pino logs errors

Source: R/pino.R ยท Help: ?condition_to_pino

A Node.js service that logs with Pino expects an error under its err key with type, message and, optionally, stack -- the fields Pino itself writes for an error, and the ones pino-pretty, transports and error trackers read. condition_to_pino() writes a condition in that shape: the Pino profile the Python DataExcept package publishes (pino-1.0.0.json, see pino_schema()). envelope_to_pino() does the same for an envelope already in hand, such as one read from a queue.

Usage

condition_to_pino(
  cnd,
  include_attributes = TRUE,
  max_depth = 8L,
  include_stack = FALSE
)

condition_to_pino_json(
  cnd,
  include_attributes = TRUE,
  max_depth = 8L,
  include_stack = FALSE,
  pretty = FALSE
)

envelope_to_pino(envelope, stack = NULL)

Arguments

cnd
A condition object.
include_attributes
Include the attributes field?
max_depth
How many levels of chained conditions to render. A cause beyond this depth is replaced by the truncation marker {"truncated": true}.
include_stack
Write the condition's rlang backtrace, when it has one, as stack?
pretty
Indent the JSON for reading?
envelope
An envelope: a list from condition_to_envelope() or jsonlite::parse_json(), or a JSON string.
stack
A stack received with the envelope, as a single string, or NULL. It is redacted.

Value

condition_to_pino() and envelope_to_pino() return the error as a list (JSON objects as named lists, arrays as unnamed lists, null as NULL). condition_to_pino_json() returns a single string of JSON.

Details

The profile is a projection of the envelope, not a replacement for it, and it differs from the envelope in three places only:

  • an exception group's exceptions are errors, the name JavaScript's AggregateError uses;
  • stack is written only when there is a real stack to write, and never made up;
  • attributes stay nested, so that an attribute called type or stack cannot overwrite the fields a consumer reads.

Everything else -- type, module, message, failure, the causes, contexts and members, the cycle and truncation markers, and the redaction already applied to the envelope -- is carried through unchanged.

In R, the real stack is an rlang backtrace: the one rlang::abort() records, or that rlang::global_entrace() adds to base R errors. With include_stack = TRUE it is written, redacted like every other exported string, and without one stack is left out. A call is not a stack, and is not written as one.

Examples

cnd <- tryCatch(
  stop(condition_group(list(
    missing_column_error("customer_id", dataframe = "orders"),
    validation_error("amount", -5)
  ), "orders failed validation")),
  error = identity
)
cat(condition_to_pino_json(cnd, pretty = TRUE))

# A Pino log line, for a log stream a Node.js service reads.
line <- sprintf(
  '{"level":50,"time":%.0f,"msg":"ingest failed","err":%s}',
  as.numeric(Sys.time()) * 1000,
  condition_to_pino_json(cnd)
)

# An envelope received as JSON.
json <- condition_to_json(api_error("https://api.example.com/v1", status_code = 503L))
str(envelope_to_pino(json))

See also

condition_to_envelope() for the envelope itself, which stays the canonical record.