@andrewheberle/ts-slog

中文文档
1.5.0发布于 2026/09/16MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 2859

Simple structured logger for TypeScript

原始使用文档
文档目录13 节
  1. Installation
  2. Usage
  3. Basic Example
  4. Log Levels
  5. Configuration
  6. Adding Context
  7. Groups
  8. Text Output
  9. API
  10. new Logger(settings?)
  11. Methods
  12. Functions
  13. License

@andrewheberle/ts-slog

A simple structured logger for TypeScript applications.

Installation

npm install @andrewheberle/ts-slog

Usage

Basic Example

import { Logger } from '@andrewheberle/ts-slog'

const logger = new Logger()

logger.info("Application started")
logger.debug("Debug information", "userId", 123, "action", "login")
logger.warn("Warning message", "retries", 3)
logger.error("An error occurred", "errorCode", 500)

Log Levels

The logger supports four log levels:

  • Debug (0) - Detailed debugging information
  • Info (1) - General informational messages (default)
  • Warning (2) - Warning messages
  • Error (3) - Error messages

Configuration

Configure the minimum log level when creating a logger instance:

import { Logger, LogLevel } from "@andrewheberle/ts-slog"

// Only log warnings and errors
const logger = new Logger({ minLevel: LogLevel.Warning })

logger.debug("This won't be logged")
logger.info("This won't be logged either")
logger.warn("This will be logged")
logger.error("This will be logged too")

Adding Context

Add key-value pairs to log entries by passing additional arguments:

import { Logger } from "@andrewheberle/ts-slog"

const logger = new Logger()

logger.info(
    "User logged in",
    "userId", 123,
    "username", "john.doe",
    "ip", "192.168.1.1"
)

// Output:
// {
//   level: 'info',
//   message: 'User logged in',
//   userId: 123,
//   username: 'john.doe',
//   ip: '192.168.1.1'
// }

Groups

A group of K/V pairs may be grouped as follows:

import { group, Logger } from "@andrewheberle/ts-slog"

const logger = new Logger()

logger.info(
    "User logged in",
    ...group("user", "id", 123, "username", "john.doe"),
    "ip", "192.168.1.1"
)

// Output:
// {
//   level: 'info',
//   message: 'User logged in',
//   user.id: 123,
//   user.username: 'john.doe',
//   ip: '192.168.1.1'
// }

Text Output

By default, the default logHandler passes the log object straight to console.log. Use the built-in TextHandler instead to format entries as a single line of text:

import { Logger, TextHandler } from "@andrewheberle/ts-slog"

const logger = new Logger({ logHandler: TextHandler })

logger.info("User logged in", "userId", 123, "action", "login")

// Output:
// INFO User logged in userId=123 action=login

String values containing whitespace are quoted:

logger.info("User logged in", "name", "John Doe", "userId", 123)

// Output:
// INFO: User logged in name="John Doe" userId=123

API

new Logger(settings?)

Creates a new logger instance.

Parameters:

  • settings (optional): Configuration object
    • logHandler: Handler to output logs (default: console.log)
    • minLevel: Minimum log level (default: LogLevel.Info)

Methods

  • debug(message: string, ...args: unknown[]): void - Log at "debug" level
  • info(message: string, ...args: unknown[]): void - Log at "info" level
  • warn(message: string, ...args: unknown[]): void - Log at "warning" level
  • error(message: string, ...args: unknown[]): void - Log at "error" level
  • with(...args: unknown[]): Logger - Returns a new Logger with additional K/V pairs added

All methods accept a message string followed by optional key-value pairs.

Functions

  • group(key: string, ...args: unknown[]): unknown[] - Create K/V pairs based on key prefix
  • TextHandler(output: Record<string, unknown>): void - A logHandler that formats log entries as text (level message key=value ...) and writes them via console.log

License

MIT

版本与发布记录

@andrewheberle/ts-slog 共发布 7 个版本,首次发布于 2025/11/07;过去 12 个月发布了 5 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.5.0latest2026/09/161 个月前
1.4.12026/07/024 个月前
1.4.02026/07/024 个月前
1.3.02026/04/027 个月前
1.2.02025/11/2111 个月前
1.1.02025/11/071 年前

关于 @andrewheberle/ts-slog 的常见问题

@andrewheberle/ts-slog 是什么?

Simple structured logger for TypeScript

如何安装 @andrewheberle/ts-slog?

在项目目录运行 npm install @andrewheberle/ts-slog,或使用 pnpm add / yarn add / bun add。

@andrewheberle/ts-slog 支持 TypeScript 吗?

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

@andrewheberle/ts-slog 支持 ESM 还是 CommonJS?

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

@andrewheberle/ts-slog 可以商用吗?

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

@andrewheberle/ts-slog 的下载量有多少?

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

@andrewheberle/ts-slog 还在维护吗?

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