createLogger()

The createLogger() function is used to instantiate a logger based on a given configuration. You can create console-based, file-based, or Upstash Redis-based loggers by specifying the type and any additional parameters relevant to that type.

Usage

Console Logger (Development)

const consoleLogger = createLogger({ name: "Mastra", level: "debug" });
consoleLogger.info("App started");

File Transport (Structured Logs)

import { FileTransport } from "@mastra/loggers/file";
 
const fileLogger = createLogger({
  name: "Mastra",
  transports: { file: new FileTransport({ path: "test-dir/test.log" }) },
  level: "warn",
});
fileLogger.warn("Low disk space", {
  destinationPath: "system",
  type: "WORKFLOW",
});

Upstash Logger (Remote Log Drain)

import { UpstashTransport } from "@mastra/loggers/upstash";
 
const logger = createLogger({
  name: "Mastra",
  transports: {
    upstash: new UpstashTransport({
      listName: "production-logs",
      upstashUrl: process.env.UPSTASH_URL!,
      upstashToken: process.env.UPSTASH_TOKEN!,
    }),
  },
  level: "info",
});
 
logger.info({
  message: "User signed in",
  destinationPath: "auth",
  type: "AGENT",
  runId: "run_123",
});

Parameters

type:

0
Specifies the logger implementation to create.

level?:

LogLevel
Minimum severity level of logs to record. One of DEBUG, INFO, WARN, or ERROR.

dirPath?:

string
For FILE type only. Directory path where log files are stored (default: "logs").

url?:

string
For UPSTASH type only. Upstash Redis endpoint URL used for storing logs.

token?:

string
For UPSTASH type only. Upstash Redis access token.

key?:

string
For UPSTASH type only. Redis list key under which logs are stored.