Learn Lua · Your first CoCoCo script · Lesson 2 of 3

Writing to the run log

Scripts can't print. Instead, they write to the run log, which you see with the run in CoCoCo. ctx.log.info records a message, and an optional table of details that you can search:

LUA
-- In a workflow script node:
-- ctx.log.info("Order received", { orderId = "SO-1042" })
-- ctx.log.warn("Order has no lines", { orderId = "SO-1042" })

These lines are comments because ctx is only there when CoCoCo runs the script. In this lesson you write the calls for real, and the checks record what you logged.

ctx.log.warn and ctx.log.error record more serious messages. Keep messages short and put the details in the table, so you can find every log line about one order.

What your code receives

Your code gets its data as input. The type Input line at the top of the code lists every field, and the editor uses it to suggest field names and spot typos. Here is an example. Each check sends different values, so work from the fields rather than these exact values.

LUA
input = { orderId = "SO-1042", lines = { { product = "Flyer" } } }

Steps

  1. Log "Order received" with ctx.log.info, with the order's id as orderId in the details.
  2. When the order has no lines, also log "Order has no lines" with ctx.log.warn.
  3. Return { lines = … }: how many lines the order has.
Show a hint

Log first, before anything that might stop the script. # tells you how many lines there are, so you can test for none.