@fluent-org/logger (英语)
流利前进协议 执行Node.js. 建造在 流利- 浏览器- 节点。 。 。 .
安装
$ npm install @fluent-org/logger
客户端
@fluent-org/logger 提供一个完全功能化的客户端来实施"前进协议". 它支持重联,确认,超时,事件重试等功能,并通过简单的打入的"承诺"接口来暴露其功能.
关于客户选择和方法的完整清单,请参见: 流利的文档
先决条件
流畅的守护进程应该以前向模式监听 .
Fluentd 简单的启动配置如下:
<source>
@type forward
port 24224
</source>
<match **.*>
@type stdout
</match>
见 流利文件 需要更多信息.
Fluent Bit 类似的起始配置如下:
[INPUT]
Name forward
Listen 0.0.0.0
Port 24224
Buffer_Chunk_Size 1M
Buffer_Max_Size 6M
[OUTPUT]
Name stdout
Match *
见 流利的比特文件 需要更多信息.
向上游流利服务器发送事件记录
const FluentClient = require("@fluent-org/logger").FluentClient;
const logger = new FluentClient("tag_prefix", {
socket: {
host: "localhost",
port: 24224,
timeout: 3000, // 3 seconds
}
});
发射方法跟随签名
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>;
返回的许诺在事件写入套接字后解决, 如果出错则被拒绝 .
鸣谢
这个 流利的前进协议 为确认提供明确支持,使客户能够确定事件到达目的地.
促成承认意味着承诺通过下列方式得到回报: emit 将在客户端收到服务器明确确认后解决.
const FluentClient = require("@fluent-org/logger").FluentClient;
const logger = new FluentClient("tag_prefix", {
ack: {}
});
事件模式
这个 流利的前进协议 提供多个消息模式, Message, (中文(简体) ), Forward, (中文(简体) ), PackedForward(违约), CompressedPackedForward。流利客户端支持所有它们.
const FluentClient = require("@fluent-org/logger").FluentClient;
const logger = new FluentClient("tag_prefix", {
eventMode: "Message" | "Forward" | "PackedForward" | "CompressedPackedForward"
});
禁用自动重接
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();
共享密钥认证
锁定配置 :
const logger = new FluentClient("tag_prefix", {
socket: {
host: "localhost",
port: 24224,
timeout: 3000, // 3 seconds
}
security: {
clientHostname: "client.localdomain",
sharedKey: "secure_communication_is_awesome"
}
});
流利配置 :
<source>
@type forward
port 24224
<security>
self_hostname input.testing.local
shared_key secure_communication_is_awesome
</security>
</source>
<match dummy.*>
@type stdout
</match>
另见 流利 实例.
TLS/ SSL 加密
锁定配置 :
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")
}
});
流利配置 :
<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: 您可以使用 fluent-ca-generate 命令自流利 1.1.0起.
另见 如何启用 TLS/ SSL 加密。 。 。 .
相互 TLS 认证
锁定配置 :
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"
}
});
流利配置 :
<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>
事件时间支持
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);
处理错误
流利客户端将内部管理出错,拒绝对出错的承诺. 如果你想访问 面对内部错误的非用户, 你可以这样做,通过 errorHandler
const FluentClient = require("@fluent-org/logger").FluentClient;
const logger = new FluentClient("tag_prefix", {
onSocketError: (err: Error) => {
console.error("error!", err)
}
});
重试事件
有时如果初次提交失败,重新提交事件是有意义的。 您可以指定 eventRetry。 。 。 .
const FluentClient = require("@fluent-org/logger").FluentClient;
const logger = new FluentClient("tag_prefix", {
eventRetry: {}
});
服务器
@fluent-org/logger 包括可以用作下游流水槽的功能完备的前置服务器.
const FluentServer = require("@fluent-org/logger").FluentServer;
const server = new FluentServer({ listenOptions: { port: 24224 }});
await server.listen();
流利配置 :
<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>
见 流利文件 需要更多信息.
或者,见 流利的比特文件 用于设置流利比特的信息.
服务器选项和方法的完整列表,请参见 流利服务器文件
许可证
阿帕奇许可证2.0版.
关于节点JS版本
此软件包与NodeJS版本相容 QQ 12.