ファイルに出力する

ファイルへの出力はFileOutputプラグインを使用する

docker-compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
version: '3.5'
services:
web1:
image: nginx:latest
ports:
- "8888:80"
depends_on:
- fluent-bit
command: [nginx-debug, '-g', 'daemon off;']
logging:
driver: fluentd
options:
fluentd-address: "localhost:24224"
fluentd-async-connect: "false"
web2:
image: nginx:latest
ports:
- "8889:80"
depends_on:
- fluent-bit
command: [nginx-debug, '-g', 'daemon off;']
logging:
driver: fluentd
options:
fluentd-address: "localhost:24224"
fluentd-async-connect: "false"
fluent-bit:
image: fluent/fluent-bit
volumes:
- ./fluent-bit.conf:/fluent-bit/etc/fluent-bit.conf
- ./log:/log:rw
ports:
- "24224:24224"

fluent-bit.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[SERVICE]
Log_Level info
Parsers_File parsers.conf

[INPUT]
Name forward
Listen 0.0.0.0
Port 24224

[FILTER]
Name parser
Match *
Key_Name log
Parser nginx
Preserve_Key Off
Reserve_Data On

[OUTPUT]
Name file
Match *
Path /log/

どのように記録されるか?

標準設定で出力してみる

‘/log’ディレクトリには以下のファイルが作成された

1
2
3
4
Mode                 LastWriteTime         Length Name
---- ------------- ------ ----
-a---l 2021/08/15 14:15 16383 247a34282381
-a---l 2021/08/15 14:12 2205 9b0fd4a80449

ファイルにはeginxのログが記録されている

1
2
3
4
5
6
7
8
9
10
247a34282381: [1629004368.000000000, {"source":"stdout","log":"/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration","container_id":"247a342823810c30c81814b59743aa4da98240b96bac829f0558770956925f67","container_name":"/output-jsonfile_web1_1"}]
247a34282381: [1629004368.000000000, {"container_id":"247a342823810c30c81814b59743aa4da98240b96bac829f0558770956925f67","container_name":"/output-jsonfile_web1_1","source":"stdout","log":"/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/"}]
247a34282381: [1629004368.000000000, {"container_name":"/output-jsonfile_web1_1","source":"stdout","log":"/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh","container_id":"247a342823810c30c81814b59743aa4da98240b96bac829f0558770956925f67"}]
247a34282381: [1629004368.000000000, {"log":"10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf","container_id":"247a342823810c30c81814b59743aa4da98240b96bac829f0558770956925f67","container_name":"/output-jsonfile_web1_1","source":"stdout"}]
247a34282381: [1629004368.000000000, {"log":"10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf","container_id":"247a342823810c30c81814b59743aa4da98240b96bac829f0558770956925f67","container_name":"/output-jsonfile_web1_1","source":"stdout"}]
247a34282381: [1629004368.000000000, {"log":"/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh","container_id":"247a342823810c30c81814b59743aa4da98240b96bac829f0558770956925f67","container_name":"/output-jsonfile_web1_1","source":"stdout"}]
247a34282381: [1629004368.000000000, {"log":"/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh","container_id":"247a342823810c30c81814b59743aa4da98240b96bac829f0558770956925f67","container_name":"/output-jsonfile_web1_1","source":"stdout"}]
247a34282381: [1629004368.000000000, {"container_id":"247a342823810c30c81814b59743aa4da98240b96bac829f0558770956925f67","container_name":"/output-jsonfile_web1_1","source":"stdout","log":"/docker-entrypoint.sh: Configuration complete; ready for start up"}]
247a34282381: [1629004383.000000000, {"remote":"172.27.0.1","host":"-","user":"-","method":"GET","path":"/","code":"304","size":"0","referer":"-","agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36","container_id":"247a342823810c30c81814b59743aa4da98240b96bac829f0558770956925f67","container_name":"/output-jsonfile_web1_1","source":"stdout"}]
247a34282381: [1629004384.000000000, {"remote":"172.27.0.1","host":"-","user":"-","method":"GET","path":"/","code":"304","size":"0","referer":"-","agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36","container_id":"247a342823810c30c81814b59743aa4da98240b96bac829f0558770956925f67","container_name":"/output-jsonfile_web1_1","source":"stdout"}]

ログフォーマットは?

標準のフォーマットはtag: [time, {"key1":"value1", "key2":"value2", "key3":"value3"}]というもの。

タグがファイル名として使用され、タグとタイムスタンプ以外はJSON形式になっている。

ログファイル名をカスタマイズしたい

ログファイル名はタグが使用されるので、タグ名をファイル名として整形すれば希望のログファイル名を設定できる。
docker-composeで起動している場合、docker-composeのオプションでタグを調整する。

loggingoptionstag: "logfile_name_${DATE_YYYYMMDD}.log"のような設定を行う。
dateコマンドなどでYYYYMMDD形式に整形した日付を環境変数DATE_YYYYMMDDとして定義する。

ログは/log/logfile_name_YYYYMMDD.logのようなファイルで生成される。

docker-compose.ymlタグ修正版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
version: '3.5'
services:
web1:
image: nginx:latest
ports:
- "8888:80"
depends_on:
- fluent-bit
command: [nginx-debug, '-g', 'daemon off;']
logging:
driver: fluentd
options:
fluentd-address: "localhost:24224"
fluentd-async-connect: "false"
tag: "logfile_name_${DATE_YYYYMMDD}.log"
web2:
image: nginx:latest
ports:
- "8889:80"
depends_on:
- fluent-bit
command: [nginx-debug, '-g', 'daemon off;']
logging:
driver: fluentd
options:
fluentd-address: "localhost:24224"
fluentd-async-connect: "false"
tag: "logfile_name_${DATE_YYYYMMDD}.log"
fluent-bit:
image: fluent/fluent-bit
volumes:
- ./fluent-bit.conf:/fluent-bit/etc/fluent-bit.conf
- ./log:/log:rw
ports:
- "24224:24224"