Blog
logger.ts in mcp-mermaid codebase.

logger.ts in mcp-mermaid codebase.

In this article, we review logger.ts in mcp-mermail codebase. We will look at:

  1. Purpose of logger.ts

  2. Methods defined

Purpose of logger.ts

It is a common best practice to define a logger that can be used across the codebase. I have seen some OSS projects using colors or picocolors npm packages to apply colors to the logs, but this was for the CLIs.

You will find the below comment in mcp-mermaid/src/utils/logger.ts.

/**
 * Unified logger for consistent logging across the application
 */
const prefix = "[MCP-Mermaid]";

Methods defined

The following code snippet shows how the methods such as info, warn, error and success are defined:

/**
 * Log info message
 */
export function info(message: string, ...args: unknown[]): void {
  console.log(`${prefix} ℹ️  ${message}`, ...args);
}

/**
 * Log warning message
 */
export function warn(message: string, ...args: unknown[]): void {
  console.warn(`${prefix} ⚠️  ${message}`, ...args);
}

/**
 * Log error message
 */
export function error(message: string, error?: unknown): void {
  console.error(`${prefix} ❌ ${message}`, error || "");
}

/**
 * Log success message
 */
export function success(message: string, ...args: unknown[]): void {
  console.log(`${prefix} ✅ ${message}`, ...args);
}

serverStartUp method

/**
 * Log server startup information
 */
export function serverStartup(
  serverType: string,
  port: number,
  endpoint: string,
): void {
  const serverUrl = `http://localhost:${port}${endpoint}`;
  const healthUrl = `http://localhost:${port}/health`;
  const pingUrl = `http://localhost:${port}/ping`;

  console.log(
    `${prefix} 🚀 ${serverType} running on: \x1b[32m\u001B[4m${serverUrl}\u001B[0m\x1b[0m`,
  );
  console.log("\nTest endpoints:");
  console.log(`• Health check: \u001B[4m${healthUrl}\u001B[0m`);
  console.log(`• Ping test: \u001B[4m${pingUrl}\u001B[0m`);
}

cleanup method

/**
 * Log cleanup information
 */
export function cleanup(message: string): void {
  console.log(`${prefix} 🧹 ${message}`);
}

/**
 * Logger object for backward compatibility
 */
export const Logger = {
  info,
  warn,
  error,
  success,
  serverStartup,
  cleanup,
};

About me:

Hey, my name is Ramu Narasinga. I study codebase architecture in large open-source projects.

Email: ramu.narasinga@gmail.com

Want to learn from open-source? Solve challenges inspired by open-source projects.

References:

  1. https://github.com/hustcc/mcp-mermaid/blob/main/src/utils/logger.ts#L62