@ecopages/logger

中文文档
0.2.3发布于 2026/03/05MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 5394

A lightweight, flexible logging library with color support, timestamps, and timer functionality. This logger supports multiple log levels and allows for easy extension and integration into any project.

原始使用文档
文档目录15 节
  1. Features
  2. Installation
  3. Basic Usage
  4. Advanced Configuration
  5. Configuration Options
  6. Timestamp Formats
  7. Error Stack Traces
  8. Timer Functionality
  9. Debug Utilities
  10. Custom Colors
  11. Examples
  12. With Timestamps and Colors
  13. With Debug Messages
  14. With Timer
  15. With Conditional Debug Timers

@ecopages/logger

A lightweight, flexible logging library with color support, timestamps, and timer functionality. This logger supports multiple log levels and allows for easy extension and integration into any project.

Features

  • Multiple Log Levels: Supports INFO, ERROR, WARN, and DEBUG log levels
  • Colored Output: Configurable colors for different log levels in both browser and Node.js environments
  • Timestamps: Optional timestamps with configurable formats
  • Timer Support: Built-in timer functionality for performance measurements
  • Debug Utilities: Helper methods for conditional debug operations
  • Prefixed Messages: Customizable prefix for all log messages
  • Environment Detection: Automatically adapts output format for browser or Node.js environments
  • Extensible: Easy to extend with custom functionality

Installation

npm install @ecopages/logger
# or
yarn add @ecopages/logger
# or
bun add @ecopages/logger

Basic Usage

import { Logger } from "@ecopages/logger";

// Create a basic logger
const logger = new Logger("[my-app]");

// Log messages at different levels
logger.info("This is an informational message");
logger.warn("This is a warning message");
logger.error("This is an error message");
logger.debug("This is a debug message"); // Only shown if debug is enabled

Advanced Configuration

The logger supports various configuration options:

const logger = new Logger("[my-app]", {
    debug: true, // Enable debug messages
    color: true, // Enable colored output
    timestamp: true, // Add timestamps to messages
    timestampFormat: "full", // Configure timestamp format
    locale: "de-DE", // Optional locale for timestamps
    verboseTimer: true, // Show timer start messages
    colors: {
        // Custom colors
        INFO: "color: purple",
        ERROR: "color: darkred",
    },
});

Configuration Options

OptionTypeDefaultDescription
debugbooleanfalseEnable debug level messages
colorbooleantrueEnable colored output
timestampbooleanfalseAdd timestamps to messages
timestampFormat"full" | "time" | "short""time"Timestamp format
localestring | string[]"en-US"Locale used for timestamps
verboseTimerbooleanfalseShow timer start messages
colorsPartial-Custom colors for log levels

Timestamp Formats

  • full: full date and time
  • time: time only
  • short: short date and time

Timestamp output is generated via toLocaleString(locale, ...). By default, locale is en-US; set locale to customize it.

Error Stack Traces

To keep stack traces, pass the actual Error object to logger.error:

try {
    throw new Error("Something failed");
} catch (error: unknown) {
    if (error instanceof Error) {
        logger.error(error); // preserves stack output
    } else {
        logger.error(new Error(String(error)));
    }
}

If you pass error.message (string) instead, the stack trace is not available.

Timer Functionality

Timer behavior is consistent whether colors are enabled or disabled:

  • color: true → colorized timer output
  • color: false → plain timer output
  • verboseTimer: true → logs start message before duration output
// Start a timer
logger.time("operation");

// ... some operations ...

// End the timer and log duration
logger.timeEnd("operation");

Debug Utilities

The logger provides several debug-specific methods:

// Check if debug mode is enabled
if (logger.isDebugEnabled()) {
    // Perform debug-only operations
}

// Start a timer only when debug is enabled
logger.debugTime("debug-operation");

// ... some operations ...

// End the debug timer (only logs when debug is enabled)
logger.debugTimeEnd("debug-operation");

Custom Colors

Colors can be customized differently for browser and Node.js environments:

// Browser colors (CSS syntax)
const logger = new Logger("[my-app]", {
    colors: {
        INFO: "color: purple",
        ERROR: "color: darkred",
        WARN: "color: orange",
        DEBUG: "color: cyan",
        TIMER: "color: magenta",
    },
});

// Node.js colors (ANSI codes)
const logger = new Logger("[my-app]", {
    colors: {
        INFO: "\x1b[35m", // Purple
        ERROR: "\x1b[31m", // Red
        WARN: "\x1b[33m", // Yellow
        DEBUG: "\x1b[36m", // Cyan
        TIMER: "\x1b[35m", // Magenta
    },
});

Examples

With Timestamps and Colors

const logger = new Logger("[my-app]", {
    timestamp: true,
    timestampFormat: "full",
});

logger.info("Application started");
// Output: [02/16/2024, 15:30:45] [my-app] Application started

With Debug Messages

const logger = new Logger("[my-app]", { debug: true });

logger.debug("Configuration loaded:", { port: 3000 });
// Output: [my-app] Configuration loaded: { port: 3000 }

With Timer

const logger = new Logger("[my-app]", { verboseTimer: true });

logger.time("db-query");
// ... database operation ...
logger.timeEnd("db-query");
// Output: [my-app] db-query: 123.45ms

With Conditional Debug Timers

const logger = new Logger("[my-app]", { debug: true });

// Only starts timer if debug is enabled
logger.debugTime("expensive-calculation");

// Some expensive operation
const result = performExpensiveCalculation();

// Only logs time if debug is enabled
logger.debugTimeEnd("expensive-calculation");

版本与发布记录

@ecopages/logger 共发布 9 个版本,首次发布于 2024/06/29;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.2.3latest2026/03/058 个月前
0.2.22025/04/171.6 年前
0.2.12025/02/171.7 年前
0.2.02025/02/161.7 年前
0.1.32024/09/212 年前
0.1.22024/07/012 年前

关于 @ecopages/logger 的常见问题

@ecopages/logger 是什么?

A lightweight, flexible logging library with color support, timestamps, and timer functionality. This logger supports multiple log levels and allows for easy extension and integration into any project.

如何安装 @ecopages/logger?

在项目目录运行 npm install @ecopages/logger,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:typescript(^5.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@ecopages/logger 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ecopages/logger 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@ecopages/logger 可以商用吗?

@ecopages/logger 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ecopages/logger 的下载量有多少?

根据 npm 官方下载统计,@ecopages/logger 在 2026/10/13 – 2026/11/11 这 30 天内被下载 5,394 次,日均约 180 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ecopages/logger 还在维护吗?

最新版本 0.2.3 发布于 2026/03/05(8 个月前);至今共发布 9 个版本,其中过去 12 个月发布了 1 个;首次发布于 2024/06/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。