@fluent-org/logger

中文文档
1.0.10发布于 2022/09/17Apache-2.0TypeScriptCommonJS近 30 天下载 52.5万

A node fluent protocol compatible logger

原始使用文档
文档目录16 节
  1. Install
  2. Client
  3. Prerequisites
  4. Sending an event record to an upstream Fluent server
  5. Acknowledgements
  6. Event modes
  7. Disable automatic reconnect
  8. Shared key authentication
  9. TLS/SSL encryption
  10. Mutual TLS Authentication
  11. EventTime support
  12. Handling errors
  13. Retrying events
  14. Server
  15. License
  16. About NodeJS versions

@fluent-org/logger

Build Status License Docs

Fluent Forward Protocol implementation for Node.js. Built upon fluent-logger-node.

NPM

Install

$ npm install @fluent-org/logger

Client

@fluent-org/logger provides a fully functional client that implements the Forward protocol. It supports reconnection, acknowledgements, timeouts, event retries, and more, and exposes its functionality via a simple typed Promise interface.

For a full list of the client options and methods, see the FluentClient docs

Prerequisites

The fluent daemon should be listening in forward mode.

A simple starting configuration for Fluentd is the following:

<source>
  @type forward
  port 24224
</source>

<match **.*>
  @type stdout
</match>

See the FluentD docs for more info.

A similar starting configuration for Fluent Bit is the following:

[INPUT]
    Name              forward
    Listen            0.0.0.0
    Port              24224
    Buffer_Chunk_Size 1M
    Buffer_Max_Size   6M

[OUTPUT]
    Name   stdout
    Match  *

See the Fluent Bit docs for more info.

Sending an event record to an upstream Fluent server

const FluentClient = require("@fluent-org/logger").FluentClient;
const logger = new FluentClient("tag_prefix", {
  socket: {
    host: "localhost",
    port: 24224,
    timeout: 3000, // 3 seconds
  }
});

The emit method has following signature

emit(data: Record<string, any>): Promise<void>;
emit(data: Record<string, any>, timestamp: number | Date | EventTime): Promise<void>;
emit(label: string, data: Record<string, any>): Promise<void>;
emit(label: string, data: Record<string, any>, timestamp: number | Date | EventTime): Promise<void>;

The returned Promise is resolved once the event is written to the socket, or rejected if an error occurs.

Acknowledgements

The Fluent forward protocol provides explicit support for acknowledgements, which allow the client to be sure that the event reached its destination.

Enabling acknowledgements means that the promise returned by emit will be resolved once the client receives an explicit acknowledgement from the server.

const FluentClient = require("@fluent-org/logger").FluentClient;
const logger = new FluentClient("tag_prefix", {
  ack: {}
});

Event modes

The Fluent forward protocol provides multiple message modes, Message, Forward, PackedForward(default), CompressedPackedForward. The Fluent client supports all of them.

const FluentClient = require("@fluent-org/logger").FluentClient;
const logger = new FluentClient("tag_prefix", {
  eventMode: "Message" | "Forward" | "PackedForward" | "CompressedPackedForward"
});

Disable automatic reconnect

const logger = new FluentClient("tag_prefix", {
  socket: {
    host: "localhost",
    port: 24224,
    timeout: 3000, // 3 seconds
    disableReconnect: true
  }
});
// If you disable reconnections, the socket has to be manually connected, 
// connect() returns a promise, which rejects on connection errors.
logger.connect();

Shared key authentication

Logger configuration:

const logger = new FluentClient("tag_prefix", {
  socket: {
    host: "localhost",
    port: 24224,
    timeout: 3000, // 3 seconds
  }
  security: {
    clientHostname: "client.localdomain",
    sharedKey: "secure_communication_is_awesome"
  }
});

Fluentd configuration:

<source>
  @type forward
  port 24224
  <security>
    self_hostname input.testing.local
    shared_key secure_communication_is_awesome
  </security>
</source>

<match dummy.*>
  @type stdout
</match>

See also the Fluentd examples.

TLS/SSL encryption

Logger configuration:

const logger = new FluentClient("tag_prefix", {
  socket: {
    host: "localhost",
    port: 24224,
    timeout: 3000, // 3 seconds
  }
  security: {
    clientHostname: "client.localdomain",
    sharedKey: "secure_communication_is_awesome"
  }
  tls: {
    ca: fs.readFileSync("/path/to/ca_cert.pem")
  }
});

Fluentd configuration:

<source>
  @type forward
  port 24224
  <transport tls>
    ca_cert_path /path/to/ca_cert.pem
    ca_private_key_path /path/to/ca_key.pem
    ca_private_key_passphrase very_secret_passphrase
  </transport>
  <security>
    self_hostname input.testing.local
    shared_key secure_communication_is_awesome
  </security>
</source>

<match dummy.*>
  @type stdout
</match>

FYI: You can generate certificates using the fluent-ca-generate command since Fluentd 1.1.0.

See also How to enable TLS/SSL encryption.

Mutual TLS Authentication

Logger configuration:

const logger = new FluentClient("tag_prefix", {
  socket: {
    host: "localhost",
    port: 24224,
    timeout: 3000, // 3 seconds
  }
  security: {
    clientHostname: "client.localdomain",
    sharedKey: "secure_communication_is_awesome"
  }
  tls: {
    ca: fs.readFileSync("/path/to/ca_cert.pem"),
    cert: fs.readFileSync("/path/to/client-cert.pem"),
    key: fs.readFileSync("/path/to/client-key.pem"),
    passphrase: "very-secret"
  }
});

Fluentd configuration:

<source>
  @type forward
  port 24224
  <transport tls>
    ca_path /path/to/ca-cert.pem
    cert_path /path/to/server-cert.pem
    private_key_path /path/to/server-key.pem
    private_key_passphrase very_secret_passphrase
    client_cert_auth true
  </transport>
  <security>
    self_hostname input.testing.local
    shared_key secure_communication_is_awesome
  </security>
</source>

<match dummy.*>
  @type stdout
</match>

EventTime support

We can also specify an EventTime as a timestamp. See the EventTime docs

const FluentClient = require("@fluent-org/logger").FluentClient;
const EventTime = require("@fluent-org/logger").EventTime;
const eventTime = new EventTime(1489547207, 745003500); // 2017-03-15 12:06:47 +0900
const logger = new FluentClient("tag_prefix", {
  socket: {
    host: "localhost",
    port: 24224,
    timeout: 3000, // 3 seconds
  }
});
logger.emit("tag", { message: "This is a message" }, eventTime);

Handling errors

The Fluent client will manage errors internally, and reject promises on errors. If you"d like to access the non-user facing internal errors, you can do so by passing errorHandler

const FluentClient = require("@fluent-org/logger").FluentClient;
const logger = new FluentClient("tag_prefix", {
  onSocketError: (err: Error) => {
    console.error("error!", err)
  }
});

Retrying events

Sometimes it makes sense to resubmit events if their initial submission failed. You can do this by specifying eventRetry.

const FluentClient = require("@fluent-org/logger").FluentClient;
const logger = new FluentClient("tag_prefix", {
  eventRetry: {}
});

Server

@fluent-org/logger includes a fully functional forward server which can be used as a downstream Fluent sink.

const FluentServer = require("@fluent-org/logger").FluentServer;

const server = new FluentServer({ listenOptions: { port: 24224 }});

await server.listen();

Fluentd config:

<match pattern>
  @type forward
  send_timeout 60s
  recover_wait 10s
  hard_timeout 60s

  <server>
    name fluent_node 
    host 127.0.0.1
    port 24224
    weight 60
  </server>

  <secondary>
    @type file
    path /var/log/fluent/forward-failed
  </secondary>
</match>

See the FluentD docs for more info.

Alternatively, see the Fluent Bit docs for info on setting up Fluent Bit.

For a full list of the server options and methods, see the FluentServer docs

License

Apache License, Version 2.0.

About NodeJS versions

This package is compatible with NodeJS versions >= 12.

版本与发布记录

@fluent-org/logger 共发布 11 个版本,首次发布于 2021/08/06;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.10latest2022/09/174 年前
1.0.92022/06/064 年前
1.0.82022/03/164 年前
1.0.72022/02/184 年前
1.0.62022/02/124 年前
1.0.52021/12/274 年前

关于 @fluent-org/logger 的常见问题

@fluent-org/logger 是什么?

A node fluent protocol compatible logger

如何安装 @fluent-org/logger?

在项目目录运行 npm install @fluent-org/logger,或使用 pnpm add / yarn add / bun add。

@fluent-org/logger 支持 TypeScript 吗?

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

@fluent-org/logger 支持 ESM 还是 CommonJS?

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

@fluent-org/logger 需要什么版本的 Node.js?

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

@fluent-org/logger 可以商用吗?

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

@fluent-org/logger 的下载量有多少?

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

@fluent-org/logger 还在维护吗?

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