Skip to content

majiayu000/spellbook / skills/typescript-project

typescript-project

Flagged

Modern TypeScript project architecture guide for 2025. Use when creating new TS projects, setting up configurations, or designing project structure. Covers tech stack selection, layered architecture, and best practices.

templates/project/src/lib/logger.ts

View on GitHub
/**
 * Structured logging
 *
 * Simple, structured logger for development and production.
 * For more advanced logging, see structured-logging-lite skill.
 */

import { config } from './config.js';

export type LogLevel = 'debug' | 'info' | 'warn' | 'error';

interface LogEntry {
  timestamp: string;
  level: LogLevel;
  message: string;
  service: string;
  [key: string]: unknown;
}

const LEVELS: Record<LogLevel, number> = {
  debug: 0,
  info: 1,
  warn: 2,
  error: 3,
};

class Logger {
  private level: LogLevel;
  private service: string;
  private context: Record<string, unknown> = {};

  constructor(service: string, level: LogLevel = 'info') {
    this.service = service;
    this.level = level;
  }

  private shouldLog(level: LogLevel): boolean {
    return LEVELS[level] >= LEVELS[this.level];
  }

  private write(level: LogLevel, message: string, data?: Record<string, unknown>) {
    if (!this.shouldLog(level)) return;

    const entry: LogEntry = {
      timestamp: new Date().toISOString(),
      level,
      message,
      service: this.service,
      ...this.context,
      ...data,
    };

    const output = config.isDev
      ? `[${entry.timestamp}] ${level.toUpperCase().padEnd(5)} ${message} ${
          data ? JSON.stringify(data) : ''
        }`
      : JSON.stringify(entry);

    if (level === 'error') {
      console.error(output);
    } else {
      console.log(output);
    }
  }

  debug(message: string, data?: Record<string, unknown>) {
    this.write('debug', message, data);
  }

  info(message: string, data?: Record<string, unknown>) {
    this.write('info', message, data);
  }

  warn(message: string, data?: Record<string, unknown>) {
    this.write('warn', message, data);
  }

  error(message: string, data?: Record<string, unknown>) {
    this.write('error', message, data);
  }

  child(context: Record<string, unknown>): Logger {
    const child = new Logger(this.service, this.level);
    child.context = { ...this.context, ...context };
    return child;
  }
}

// Global logger instance
export const logger = new Logger('app', config.logLevel);

// Create child loggers for different modules
export function createLogger(module: string): Logger {
  return logger.child({ module });
}
brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).Stars: 322kUpdated Claude CodeCodexVerifiedInstall
brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).Stars: 198kUpdated Claude CodeCodexVerifiedInstall