Logging in workflow code

We already have a logger class in place which uses PINO for logging, This is working absolutely fine in activities. But when tried to initialize it in workflow code or when tried to use this logger class in workflow code by initializing the logger class in activity and returning it to workflow, it’s not working. The object is not having the methods that we have defined in the class.
Any reason or any turn around to resolve this issue?

But when tried to initialize it in workflow code

TypeScript workflows run in a “determinism sandbox”, which prevents loading code that would do non-deterministic things. That includes modules that does input/output operations, such as loggers. That’s why you can’t load your logger from Workflow code.

when tried to use this logger class in workflow code by initializing the logger class in activity and returning it to workflow … The object is not having the methods that we have defined in the class.

Every event affecting a workflow is written to that workflow execution history, so that it can be replayed at a later point. That also means that everything going in and our of a workflow need to be serialized (by default, they will be serialized to JSON). Methods on objects can’t survive this serialization process.

any turn around to resolve this issue

Workflows already provide support for logging. For example:

import * as workflow from '@temporalio/workflow';

export async function myWorkflow(): Promise<void> {
  workflow.log.info('hello from my workflow', { key: 'value' });
}

The workflow logger funnels messages through a Workflow sink (more on this subject below), which relay messages to the runtime logger (ie. Runtime.instance().logger). The easiest way to have your workflows export messages using a different logging library is therefore to simply to register a custom logger adaptor on Runtime.instance().logger. See this sample. That approach will automatically collect log messages produced from workflow context using workflow.log.info() (and similar), log messages produced from activity context using Context.current().log.info() (and similar), and log messages produced by the SDK itself.

Sinks provide a way for workflow code to do one-way call to functions that execute out of the sandbox, in a replay and deterministic safe way. If you want to provide a logger interface that is different form the one provided by our default workflow logger, then you can register your own workflow sinks. See the alerter sink in this sample.