You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用td-agent-bit收集Node.js应用日志失败,目标日志文件为空

问题描述

我尝试用td-agent-bit收集Node.js应用(app1)的日志到/etc/td-agent-bit/file.log,相关配置与操作如下:

td-agent-bit.conf 关键配置

...
[INPUT]
    Name forward
    Listen localhost
    Port 24224

[OUTPUT]
    Name        file
    Match       *
    Path        /etc/td-agent-bit/file.log

已执行的操作

  1. 给日志文件设置全权限:
sudo chmod 777 /etc/td-agent-bit/file.log
  1. 确认td-agent-bit服务运行正常:
sudo service td-agent-bit status

但运行app1后,file.log始终为空,app1代码如下:

const FluentLogger = require('fluent-logger');

// Initialize Fluent Logger
FluentLogger.configure('app1', {
    host: 'localhost', // Fluent Bit host
    port: 24224, // Fluent Bit port
    timeout: 3.0, // Timeout in seconds
});

// Send log messages
FluentLogger.emit('mytag', { message: 'First log' }, () => {
    console.log('First log sent successfully');
});

FluentLogger.emit('mytag', { message: 'Second log' }, () => {
    console.log('Second log sent successfully');
});

// Close the connection after 5 seconds
setTimeout(() => {
    FluentLogger.end(); // Close the connection
    console.log('Connection closed');
}, 5000);
排查与解决步骤

1. 修正td-agent-bit的file输出配置

file输出的Path参数指定的是目录路径,而非具体文件名。你当前直接填写文件名会导致td-agent-bit无法识别正确的写入路径,需修改配置:

[OUTPUT]
    Name        file
    Match       *
    Path        /etc/td-agent-bit
    File_Name   file.log
  • Path:填写日志文件所在的目录
  • File_Name:指定具体的日志文件名

2. 重启td-agent-bit服务

修改配置后必须重启服务才能生效:

sudo service td-agent-bit restart

3. 确认日志发送环节是否正常

在app1代码中添加错误捕获,排查是否存在发送失败的情况:

FluentLogger.emit('mytag', { message: 'First log' }, (err) => {
    if (err) {
        console.error('First log send failed:', err);
    } else {
        console.log('First log sent successfully');
    }
});

FluentLogger.emit('mytag', { message: 'Second log' }, (err) => {
    if (err) {
        console.error('Second log send failed:', err);
    } else {
        console.log('Second log sent successfully');
    }
});

如果输出错误信息,可根据提示进一步排查网络连接或协议兼容问题。

4. 验证td-agent-bit的端口监听状态

确认td-agent-bit确实在监听24224端口:

netstat -tulpn | grep 24224

如果无结果返回,说明forward输入配置未生效,检查[INPUT]段是否存在拼写错误,或端口是否被其他程序占用。

5. 查看td-agent-bit运行日志

检查服务日志,确认是否有接收或写入相关的报错信息:

sudo tail -f /var/log/td-agent-bit/td-agent-bit.log

内容的提问来源于stack exchange,提问作者nix86

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 08:32:35