proc-log

中文文档
7.0.0发布于 2026/05/09ISCCommonJS零依赖构建来源已验证近 30 天下载 2.5亿

在进程对象上发送“ log” 事件

中文使用文档
文档目录6 节
  1. API 密码
  2. 实例
  3. 日志
  4. 投入
  5. start 财务报告和审定财务报表 end
  6. 使用 read 调用 read()

proc 日志

在进程对象上显示一个听众可以消费的事件并打印到终端或日志文件.

这被npm CLI堆栈内的各种模块所使用,以便发送可以被进程对象上的收听器所消耗的日志事件.

目前发送 log, (中文(简体) ), output, (中文(简体) ), input,以及 time 事件.

API 密码

const { log, output, input, time } = require('proc-log')

产出

  • output.standard(...args) 电话 process.emit('output', 'standard', ...args)

    这是用于一般标准输出. 消费者通常会在stdout上显示(在可选格式化或过滤后).

  • output.error(...args) 电话 process.emit('output', 'error', ...args)

    这是用于一般错误输出 。 消费者一般会在stderr上(在可选格式化或过滤后)显示这一点.

  • output.buffer(...args) 电话 process.emit('output', 'buffer', ...args)

    这是用于缓冲输出的. 消费者通常会缓冲,直到他们准备好展示为止.

  • output.flush(...args) 电话 process.emit('output', 'flush', ...args)

    这表明输出缓冲器应该被冲出.

  • output.LEVELS 所有输出方法名称的字符串数组

日志

  • log.error(...args) 电话 process.emit('log', 'error', ...args)

    最高的日志级别。 因为打印出非常严重的错误 显示出出问题了.

  • log.warn(...args) 电话 process.emit('log', 'warn', ...args)

    相当高的日志水平。 用户需要意识到的事物,但不一定会导致系统运行不当.

  • log.notice(...args) 电话 process.emit('log', 'notice', ...args)

    重要但不一定危险或引起过分关切的通知.

  • log.info(...args) 电话 process.emit('log', 'info', ...args)

    信息信息可能有益于用户,但并不特别重要.

  • log.verbose(...args) 电话 process.emit('log', 'verbose', ...args)

    噪音输出是大多数用户会关心的更细节.

  • log.silly(...args) 电话 process.emit('log', 'silly', ...args)

    极其吵闹的过度日志信息,通常只用于调试.

  • log.http(...args) 电话 process.emit('log', 'http', ...args)

    提出和(或)完成关于HTTP请求的资料.

  • log.timing(...args) 电话 process.emit('log', 'timing', ...args)

    时间信息.

  • log.pause() 电话 process.emit('log', 'pause')

    曾叫消费者停止印刷信息.

  • log.resume() 电话 process.emit('log', 'resume')

    曾经告诉消费者,可以再次打印信息.

  • log.LEVELS 所有日志方法名称的字符串数组

投入

  • input.start(...args) 电话 process.emit('input', 'start', ...args)

    曾经告诉消费者,终端会开始读取用户输入. 返回将调用的函数 input.end(...args) 为方便.

    如果第一个参数是函数,则用作运行的回调 input.end() 完成报告。 如果回电返回 a Promise 接下来 input.end(...args) 将在 finally()。 。 。 .

  • input.end(...args) 电话 process.emit('input', 'end', ...args)

    曾经告诉消费者,终端已经停止阅读用户输入.

  • input.read(...args): Promise 电话 process.emit('input', 'read', resolve, reject, ...args)

    曾经告诉消费者终端正在读取用户输入并返回 a Promise 制作人可以 await 直到消费者完成了其同步动作.

    这个发音 resolve 财务报告和审定财务报表 reject 消费者必须用来解决返回的函数(除所有参数外) Promise。 。 。 .

时间

  • time.start(timerName, fn?) 电话 process.emit('time', 'start', 'timerName')

    用于启动指定名称的定时器 。 返回将调用的函数 time.end() 为方便.

    这还需要一个可选的回调来运行 time.end() 完成报告。 如果回电返回 a Promise 接下来 time.end() 将在 finally()。 。 。 .

  • time.end(timerName) 电话 process.emit('time', 'end', timeName)

    曾告诉消费者停止带有指定名称的计时器.

实例

日志

每个 log 方法调用 process.emit('log', level, ...otherArgs) 内部。 所以,为了消耗那些事件 你需要做 process.on('log', fn)。 。 。 .

基于关卡的颜色

这是消费的例子 proc-log 日志事件,并根据关卡进行颜色化:

const chalk = require('chalk')

process.on('log', (level, ...args) => {
  if (level === 'error') {
    console.log(chalk.red(level), ...args)
  } else {
    console.log(chalk.blue(level), ...args)
  }
})

暂停并恢复

log.pause 财务报告和审定财务报表 log.resume 包含在其中,所以您有能力告诉消费者您想要暂停或恢复显示日志。 在npm CLI中,我们使用这个来缓冲所有输入的日志,直到我们知道要显示的正确日志级别. 但我们也设置了第二个处理器,即使暂停,也将所有东西写入文件.

let paused = true
const buffer = []

// this handler will buffer and replay logs only after `procLog.resume()` is called
process.on('log', (level, ...args) => {
  if (level === 'resume') {
    buffer.forEach((item) => console.log(...item))
    paused = false
    return
  } 

  if (paused) {
    buffer.push([level, ...args])
  } else {
    console.log(level, ...args)
  }
})

// this handler will write everything to a file
process.on('log', (...args) => {
  fs.appendFileSync('debug.log', args.join(' '))
})

投入

start 财务报告和审定财务报表 end

制作人员.js

const { output, input } = require('proc-log')
const { readFromUserInput } = require('./my-read')

// Using callback passed to `start`
try {
  const res = await input.start(
    readFromUserInput({ prompt: 'OK?', default: 'y' })
  )
  output.standard(`User said ${res}`)
} catch (err) {
  output.error(`User cancelled: ${err}`)
}

// Manually calling `start` and `end`
try {
  input.start()
  const res = await readFromUserInput({ prompt: 'OK?', default: 'y' })
  output.standard(`User said ${res}`)
} catch (err) {
  output.error(`User cancelled: ${err}`)
} finally {
  input.end()
}

消费者.js

const { read } = require('read')

process.on('input', (level) => {
  if (level === 'start') {
    // Hide UI to make room for user input being read
  } else if (level === 'end') {
    // Restore UI now that reading is ended
  }
})

使用 read 调用 read()

制作人员.js

const { output, input } = require('proc-log')

try {
  const res = await input.read({ prompt: 'OK?', default: 'y' })
  output.standard(`User said ${res}`)
} catch (err) {
  output.error(`User cancelled: ${err}`)
}

消费者.js

const { read } = require('read')

process.on('input', (level, ...args) => {
  if (level === 'read') {
    const [res, rej, opts] = args
    read(opts).then(res).catch(rej)
  }
})

版本与发布记录

proc-log 共发布 11 个版本,首次发布于 2021/04/16;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
7.0.0latest2026/05/095 个月前
6.1.02025/11/2611 个月前
6.0.02025/10/221 年前
5.0.02024/09/052 年前
4.2.02024/04/172 年前
4.1.02024/04/162 年前

关于 proc-log 的常见问题

proc-log 是什么?

在进程对象上发送“ log” 事件

如何安装 proc-log?

在项目目录运行 npm install proc-log,或使用 pnpm add / yarn add / bun add。

proc-log 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/proc-log 是否存在。

proc-log 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

proc-log 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js ^22.22.2 || ^24.15.0 || >=26.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

proc-log 可以商用吗?

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

proc-log 的下载量有多少?

根据 npm 官方下载统计,proc-log 在 2026/10/05 – 2026/11/03 这 30 天内被下载 252,150,568 次,日均约 8,405,019 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

proc-log 还在维护吗?

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