node.js/express で fluent bit を使用してログ送信

メモとして残しておきます。

環境

OS: Rocky Linux 8.6
Server: node.js 16.x / express
Logger: winston / winston-daily-rotate-file
Sender: Fluent bit 2.1
Log Server: OpenObserve 0.5.2

設定

OpenObserve

ログサーバーとしてOpenObserveを使用する。詳細は以下参照

node.js /express / winston

loggerとしてwinstonを使用する。
デフォルトでjson形式の構造化されたログを出力するため。
ここではファイルにjson形式のログを出力するのみ。ファイルからサーバーへの送信はfluent bitを使用する。

npm install winston
npm install express-winston
npm install winston-daily-rotate-file

ログをファイルに出力するため、app.js に以下の記述を行う。

var winston = require('winston')
var expressWinston = require('express-winston');
require('winston-daily-rotate-file');

app.use(expressWinston.logger({
  transports: [
    new winston.transports.DailyRotateFile({
      level: 'info',
      filename: '/home/aaaaa/log/application-%DATE%.log',
      //filename: './log/application-%DATE%.log',
      datePattern: 'YYYY-MM-DD-HH',
      zippedArchive: true,
      maxSize: '20m',
      maxFiles: '14d'
    }),
  ],
  format: winston.format.combine(
    winston.format.json()
  ),
  expressFormat: true
}));

GitHub - winstonjs/winston: A logger for just about everything.
A logger for just about everything. Contribute to winstonjs/winston development by creating an account on GitHub.
GitHub - bithavoc/express-winston: express.js middleware for winstonjs
express.js middleware for winstonjs. Contribute to bithavoc/express-winston development by creating an account on GitHub.
GitHub - winstonjs/winston-daily-rotate-file: A transport for winston which logs to a rotating file each day.
A transport for winston which logs to a rotating file each day. - GitHub - winstonjs/winston-daily-rotate-file: A transport for winston which logs to a rotatin...

fluent bit

install

curl https://raw.githubusercontent.com/fluent/fluent-bit/master/install.sh | sh

configを以下のように設定する。
OUTPUTの設定は OpenObserve の「摂取」メニューにあるものをコピペすればよい。(先頭のスペース文字は4つのためその点を修正しなければエラーになるので注意)

[INPUT]
    name   tail
    path   /home/aaaaa/log/application-*.log
    tag    in_aaaaa_access

[OUTPUT]
    Name http
    Match *
    URI /api/default/default/_json
    Host 192.168.0.100
    Port 5080
    tls Off
    Format json
    Json_date_key    _timestamp
    Json_date_format iso8601
    HTTP_User root@example.com
    HTTP_Passwd M8xHnlMQkaaaaaaaaa
    compress gzip

実行

systemctl start fluent-bit
systemctl status fluent-bit
systemctl enable fluent-bit

Fluent Bit v2.1 Documentation - Fluent Bit: Official Manual
High Performance Telemetry Agent for Logs, Metrics and Traces

以上、これで動作するはずです。

参考

忙しい人のための Fluent Bit スターター
こんにちは。未だに競馬で当たったことがないホシイです。 今日も、クラウド機能をお手軽に使ってみるお試しネタをひとつお届けします。 オンプレでも Cloud Logging を使いたい… ふだん GCE や GKE を使っていると、非常にすんなり Cloud Logging に log が入ってくれるので、 …

コメント

タイトルとURLをコピーしました