Skip to main content

Logging

Logging from Workflows and Activities#

Activities#

Activities run in the standard NodeJS environment and can use any NodeJS logger.

Workflows#

Logging from Workflows is tricky for 2 reasons:

  1. Workflows run in an isolated JS environment and may not do any I/O
  2. Workflow code might get replayed generating duplicate log messages

In order to get around this limitation, use the External Dependencies mechanism to inject replay aware functions from the NodeJS environment into a Workflow isolate.

Logs generated by SDK components#

The Worker comes with a default logger which defaults to log any messages with level INFO and higher to STDERR using console.error.

The rationale behind this is to minimize Worker dependencies and allow SDK users to bring their own logger.

Customizing the default logger#

import {Worker, DefaultLogger} from "@temporalio/worker";
// Set up the DefaultLogger to log only WARNING and ERROR messages with a custom log function
const logger = new DefaultLogger("WARNING", (severity, message, meta) => {
/* Implement this in order to generate custom log output */
});
const worker = await Worker.create(__dirname, {logger});

BYOL - Bring your own logger#

A common logging use case is logging to a file to be picked up by a collector like the Datadog Agent.

import {Worker} from "@temporalio/worker";
import winston from "winston";
const logger = winston.createLogger({
level: "info",
format: winston.format.json(),
transports: [new transports.File({filename: "/path/to/worker.log"})],
});
const worker = await Worker.create(__dirname, {logger});

Get notified of updates