Daydreams Logo
Extra reading

logger.ts

This file provides the Logger class used throughout the Daydreams framework for recording informational messages, warnings, and errors that occur during agent execution. It helps you understand what your agent is doing and diagnose problems.

How to Use

You don't typically create a Logger instance yourself. The agent object returned by createDreams already has a pre-configured logger available at agent.logger. You use this instance inside your action handlers, context lifecycle methods, or service definitions to log relevant information:

import {
  action,
  type ActionCallContext,
  type AnyAgent,
} from "@daydreamsai/core";
 
export const myAction = action({
  name: "processData",
  // ... schema ...
  async handler(args, ctx: ActionCallContext, agent: AnyAgent) {
    // Log informational message
    agent.logger.info("processData:handler", "Starting data processing", {
      inputArgs: args,
    });
 
    try {
      // ... do some work ...
      const result = { status: "completed" };
      // Log successful completion (at debug level)
      agent.logger.debug("processData:handler", "Processing successful", {
        result,
      });
      return result;
    } catch (error) {
      // Log an error
      agent.logger.error("processData:handler", "Processing failed!", {
        error,
      });
      throw error; // Re-throw or handle error
    }
  },
});
  • Common methods are agent.logger.info(), agent.logger.warn(), agent.logger.error(), agent.logger.debug(), and agent.logger.trace().
  • Each method takes a context string (often the function/component name), a message string, and optional data object.

Benefit

Provides a standard way to record what's happening inside your agent. This is crucial for:

  • Debugging: Seeing the flow of execution, variable values, and errors.
  • Monitoring: Understanding how your agent is performing in production.
  • Auditing: Keeping a record of important events or decisions. The default logger prints messages to the console with timestamps, levels, and context, making it easy to follow along.

Anticipated Questions

  • "How can I change the logging level (e.g., see DEBUG messages)?" You can set the logLevel option when calling createDreams. For example: createDreams({ ..., logLevel: LogLevel.DEBUG }). The levels are ERROR, WARN, INFO, DEBUG, TRACE (most verbose).
  • "Can I send logs somewhere other than the console?" Yes, the logger is designed with "transports". While the default is ConsoleTransport, you could potentially implement custom transports (though this is an advanced topic not covered here) and provide them via the logger or transports option in createDreams.
  • "Why provide a context string (like 'processData:handler')?" It helps identify where in the code the log message originated, which is very useful for debugging complex agents.

On this page