@devicefarmer/adbkit-logcat

中文文档
3.0.0发布于 2026/10/25Apache-2.0TypeScriptCommonJS零依赖构建来源已验证近 30 天下载 270.9万

A Node.js interface for working with Android's logcat output.

原始使用文档
文档目录12 节
  1. Requirements
  2. Getting started
  3. Examples
  4. API
  5. Logcat
  6. Priority
  7. Reader
  8. Entry
  9. More information
  10. Releasing
  11. Contributing
  12. License

adbkit-logcat

adbkit-logcat provides a Node.js interface for working with output produced by the Android logcat tool. It takes a log stream (that you must create separately), parses it, and emits log entries in real-time as they occur. Possible use cases include storing logs in a database, forwarding logs via MessagePack, or just advanced filtering.

Requirements

  • Node.js 22 or newer. Older versions are not supported.

Getting started

Install via NPM:

npm install --save @devicefarmer/adbkit-logcat

Examples

Output all log messages

JavaScript
const logcat = require('adbkit-logcat')
const {spawn} = require('child_process')

// Retrieve a binary log stream
const proc = spawn('adb', ['logcat', '-B'])

// Connect logcat to the stream
reader = logcat.readStream(proc.stdout)
reader.on('entry', entry => {
  console.log(entry.message)
})

// Make sure we don't leave anything hanging
process.on('exit', () => {
  proc.kill()
})

API

Logcat

logcat.Priority

Exposes Priority. See below for details.

logcat.Reader

Exposes Reader. See below for details.

logcat.readStream(stream[, options])

Creates a logcat reader instance from the provided logcat event Stream. Note that you must create the stream separately.

IMPORTANT: The default line break behavior has changed on newer Android versions. Since adbkit-logcat merely parses output and is not able to communicate with ADB, you must externally detect whether you need the fixLineFeeds option enabled or not. For newer versions it should be off.

  • stream The event stream to read.
  • options Optional. The following options are supported:
    • format The format of the stream. Currently, the only supported value is 'binary', which (for example) adb logcat -B produces. Defaults to 'binary'.
    • fixLineFeeds On older Android versions, ADB shell automatically transformed any '\n' into '\r\n', which broke binary content. If set, this option reverses the transformation before parsing the stream. Defaults to true for backwards compatibility. You MUST set this option to false on newer versions.
  • Returns: The Reader instance.

Priority

Constants

The following static properties are available:

  • Priority.UNKNOWN i.e. 0.
  • Priority.DEFAULT i.e. 1. Not available when reading a stream.
  • Priority.VERBOSE i.e. 2.
  • Priority.DEBUG i.e. 3.
  • Priority.INFO i.e. 4.
  • Priority.WARN i.e. 5.
  • Priority.ERROR i.e. 6.
  • Priority.FATAL i.e. 7.
  • Priority.SILENT i.e. 8. Not available when reading a stream.

Priority.fromLetter(letter)

Static method to convert the given letter into a numeric priority. For example, Priority.fromName('d') would return Priority.DEBUG.

  • letter The priority as a String. Any single, case-insensitive character matching the first character of any Priority constant is accepted.
  • Returns: The priority as a Number, or undefined.

Priority.fromName(name)

Static method to convert the given name into a numeric priority. For example, Priority.fromName('debug') (or Priority.fromName('d')) would return Priority.DEBUG.

  • name The priority as a String. Any full, case-insensitive match of the Priority constants is accepted. If no match is found, falls back to Priority.fromLetter().
  • Returns: The priority as a Number, or undefined.

Priority.toLetter(priority)

Static method to convert the numeric priority into its letter representation. For example, Priority.toLetter(Priority.DEBUG) would return 'D'.

  • priority The priority as a Number. Any Priority constant value is accepted.
  • Returns: The priority as a String letter, or undefined.

Priority.toName(priority)

Static method to convert the numeric priority into its full string representation. For example, Priority.toLetter(Priority.DEBUG) would return 'DEBUG'.

  • priority The priority as a Number. Any Priority constant value is accepted.
  • Returns: The priority as a String, or undefined.

Reader

A reader instance, which is an EventEmitter.

Events

The following events are available:

  • error (err) Emitted when an error occurs.
    • err An Error.
  • end Emitted when the stream ends.
  • finish Emitted when the stream finishes.
  • entry (entry) Emitted when the stream finishes.
    • entry A log Entry. See below for details.

constructor([options])

For advanced users. Manually constructs a Reader instance. Useful for testing and/or playing around. Normally you would use logcat.readStream() to create the instance.

  • options See logcat.readStream() for details.
  • Returns: N/A

reader.connect(stream)

For advanced users. When instantiated manually (not via logcat.readStream()), connects the Reader instance to the given stream.

  • stream See logcat.readStream() for details.
  • Returns: The Reader instance.

reader.end()

Convenience method for ending the stream.

  • Returns: The Reader instance.

reader.exclude(tag)

Skip entries with the provided tag. Alias for reader.include(tag, Priority.SILENT). Note that even skipped events have to be parsed so that they can be ignored.

  • tag The tag string to exclude. If '*', works the same as reader.excludeAll().
  • Returns: The Reader instance.

reader.excludeAll()

Skip ALL entries. Alias for reader.includeAll(Priority.SILENT). Any entries you wish to see must be included via include()/includeAll().

  • Returns: The Reader instance.

reader.include(tag[, priority])

Include all entries with the given tag and a priority higher or equal to the given priority.

  • tag The tag string to include. If '*', works the same as reader.includeAll(priority).
  • priority Optional. A lower bound for the priority. Any numeric Priority constant or any String value accepted by Priority.fromName() is accepted. Defaults to Priority.DEBUG.
  • Returns: The Reader instance.

reader.includeAll([priority])

Include all entries with a priority higher or equal to the given priority.

  • tag The tag string to exclude.
  • priority Optional. See reader.include() for details.
  • Returns: The Reader instance.

reader.resetFilters()

Resets all inclusions/exclusions.

  • Returns: The Reader instance.

Entry

A log entry.

Properties

The following properties are available:

  • date Event time as a Date.
  • pid Process ID as a Number.
  • tid Thread ID as a Number.
  • priority Event priority as a Number. You can use logcat.Priority to convert the value into a String.
  • tag Event tag as a String.
  • message Message as a String.

entry.toBinary()

Converts the entry back to the binary log format.

  • Returns: The binary event as a Buffer.

More information

Releasing

The package is published to npm as @devicefarmer/adbkit-logcat by two GitHub Actions workflows:

  1. Run Prepare release with the version as input, for example 2.1.4. It bumps package.json, writes the CHANGELOG.md section from GitHub's generated notes, and pushes release/v2.1.4. It writes nothing to master.
  2. Open the pull request it links to in the job summary, label it ignore-for-release, and merge it once the checks pass.
  3. Run Release on master with the same version. It tags v2.1.4, publishes the GitHub release with the notes from CHANGELOG.md, then builds, tests and publishes the package to npm with trusted publishing (OIDC), so no npm token is stored anywhere.

The bump goes through a pull request because master is protected. Release refuses to run on any branch but master, so an unmerged release/v* branch cannot be published.

Contributing

See CONTRIBUTING.md.

License

See LICENSE.

Copyright © The OpenSTF Project. All Rights Reserved.

版本与发布记录

@devicefarmer/adbkit-logcat 共发布 8 个版本,首次发布于 2020/05/16;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.0.0latest2026/10/2518 天前
2.1.32023/03/303 年前
2.1.22022/04/214 年前
2.1.12021/03/085 年前
2.1.02021/03/085 年前
1.1.02020/05/166 年前

关于 @devicefarmer/adbkit-logcat 的常见问题

@devicefarmer/adbkit-logcat 是什么?

A Node.js interface for working with Android's logcat output.

如何安装 @devicefarmer/adbkit-logcat?

在项目目录运行 npm install @devicefarmer/adbkit-logcat,或使用 pnpm add / yarn add / bun add。

@devicefarmer/adbkit-logcat 支持 TypeScript 吗?

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

@devicefarmer/adbkit-logcat 支持 ESM 还是 CommonJS?

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

@devicefarmer/adbkit-logcat 需要什么版本的 Node.js?

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

@devicefarmer/adbkit-logcat 可以商用吗?

@devicefarmer/adbkit-logcat 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@devicefarmer/adbkit-logcat 的下载量有多少?

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

@devicefarmer/adbkit-logcat 还在维护吗?

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