npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

winston-sqlite-wal-batch

v0.1.0

Published

Production-ready Winston transport with SQLite WAL batch writes for multi-bot shared logging.

Downloads

95

Readme

winston-sqlite-wal-batch

面向多 bot 场景的 Winston Transport:使用 SQLite(WAL) 做批量日志写入与分析查询。

适用场景

  • 同一台机器跑多个 bot,希望共享一个日志库。
  • 需要 SQL 分析日志,但不想引入额外日志服务。
  • VPS 资源有限(2 vCPU / 4GB)且强调稳定运行。

特性

  • 批量写入:内存队列 + 单事务批量 INSERT
  • WAL 模式:journal_mode=WAL + synchronous=NORMAL
  • 多 bot 共享:统一 bot_logs 表,按 bot_id 分析
  • 低资源保护:队列上限 + 优先保留高等级日志
  • 内置维护:按保留天数清理 + wal_checkpoint(PASSIVE)
  • 内置分析 API:最近日志、等级聚合、错误 Top

安装

npm install winston-sqlite-wal-batch winston

接入示例

import winston from "winston";
import { SqliteWinstonBatchTransport } from "winston-sqlite-wal-batch";

const logger = winston.createLogger({
  level: "info",
  format: winston.format.combine(
    winston.format.timestamp(),
    winston.format.json()
  ),
  transports: [
    new winston.transports.Console(),
    new SqliteWinstonBatchTransport({
      // dbPath 可省略,默认 ~/bot-logs/shared-logs.db
      botId: process.env.BOT_ID ?? "bot-unknown",
      source: "vps-ts-bot",
      flushIntervalMs: 500,
      batchSize: 200,
      maxQueueSize: 10000,
      retentionDays: 14,
      maintenanceIntervalMs: 60000,
    }),
  ],
});

logger.info("bot started", { event: "startup" });
logger.error("execution failed", { event: "exec", execKey: "abc:123", err: "timeout" });

多 bot 共用同一 SQLite

所有 bot 只要 dbPath 指向同一文件即可,例如:

  • ~/bot-logs/shared-logs.db

每个 bot 需要设置不同 botId,例如:

  • bot-a
  • bot-b

分析查询示例

import { openSharedLogDatabaseReadOnly, queryLevelSummary, queryTopErrorMessages } from "winston-sqlite-wal-batch";

const db = openSharedLogDatabaseReadOnly(`${process.env.HOME}/bot-logs/shared-logs.db`);

const levelSummary = queryLevelSummary(db, {
  botId: "bot-a",
  fromMs: Date.now() - 60 * 60 * 1000,
  bucketMs: 60_000,
});

const topErrors = queryTopErrorMessages(db, {
  botId: "bot-a",
  fromMs: Date.now() - 24 * 60 * 60 * 1000,
  limit: 20,
});

console.log(levelSummary.length, topErrors.length);
db.close();

建议的 .env 配置(示例)

BOT_ID=bot-a
LOG_SQLITE_PATH=~/bot-logs/shared-logs.db

说明:本包不读取私钥、token 等敏感配置。敏感信息应仅由 bot 自身 .env 管理。

对外 API

  • SqliteWinstonBatchTransport
  • openSharedLogDatabase
  • openSharedLogDatabaseReadOnly
  • runRetentionCleanup
  • runWalCheckpoint
  • queryRecentLogs
  • queryLevelSummary
  • queryTopErrorMessages

发布

npm run build
npm run pack:dry-run
npm login --registry https://registry.npmjs.org
npm run publish:npm

如果包名已被占用,请先修改 package.jsonname 后再发布。