docker-compose logs 用法

前言

最近用docker-compose的时候,想看下程序运行的日志,就想到使用logs进行查看。

help

使用docker-compose help logs可以看到具体的用法
比如:

# docker-compose help logs
View output from containers.

Usage: logs [options] [SERVICE...]

Options:
    --no-color          Produce monochrome output.
    -f, --follow        Follow log output.
    -t, --timestamps    Show timestamps.
    --tail="all"        Number of lines to show from the end of the logs
                        for each container.

logs使用

查看最近100条日志

# docker-compose logs -t --tail="100"  djc8redis
Attaching to dockercompose_djc8redis_1
djc8redis_1       | 2021-03-28T07:32:35.012819865Z 1:M 28 Mar 2021 07:32:35.012 * 1 changes in 3600 seconds. Saving...
djc8redis_1       | 2021-03-28T07:32:35.016782869Z 1:M 28 Mar 2021 07:32:35.013 * Background saving started by pid 1485
djc8redis_1       | 2021-03-28T07:32:35.117356752Z 1485:C 28 Mar 2021 07:32:35.115 * DB saved on disk
····································
djc8redis_1       | 2021-03-29T01:32:53.056085328Z 1:M 29 Mar 2021 01:32:53.051 * Background saving started by pid 1503
djc8redis_1       | 2021-03-29T01:32:53.137325988Z 1503:C 29 Mar 2021 01:32:53.134 * DB saved on disk
djc8redis_1       | 2021-03-29T01:32:53.137347569Z 1503:C 29 Mar 2021 01:32:53.135 * RDB: 0 MB of memory used by copy-on-write
djc8redis_1       | 2021-03-29T01:32:53.152178636Z 1:M 29 Mar 2021 01:32:53.152 * Background saving terminated with success
djc8redis_1       | 2021-03-29T02:32:54.094003922Z 1:M 29 Mar 2021 02:32:54.093 * 1 changes in 3600 seconds. Saving...
djc8redis_1       | 2021-03-29T02:32:54.097660363Z 1:M 29 Mar 2021 02:32:54.094 * Background saving started by pid 1504
djc8redis_1       | 2021-03-29T02:32:54.168244713Z 1504:C 29 Mar 2021 02:32:54.168 * DB saved on disk
djc8redis_1       | 2021-03-29T02:32:54.169085968Z 1504:C 29 Mar 2021 02:32:54.169 * RDB: 0 MB of memory used by copy-on-write
djc8redis_1       | 2021-03-29T02:32:54.195118003Z 1:M 29 Mar 2021 02:32:54.195 * Background saving terminated with success

可以看到执行后马上就结束程序了

跟随模式看最近10条日志

# docker-compose logs -f -t --tail="10"  djc8php
Attaching to dockercompose_djc8php_1
djc8php_1         | 2021-03-29T03:34:21.140855922Z 172.28.0.8 -  29/Mar/2021:03:34:21 +0000 "POST /tooooo0010O.php" 200
djc8php_1         | 2021-03-29T03:34:21.275166829Z 172.28.0.8 -  29/Mar/2021:03:34:21 +0000 "POST /tooooo0010O.php" 200
djc8php_1         | 2021-03-29T03:34:26.145481449Z 172.28.0.8 -  29/Mar/2021:03:34:26 +0000 "POST /tooooo0010O.php" 200
djc8php_1         | 2021-03-29T03:34:31.153824197Z 172.28.0.8 -  29/Mar/2021:03:34:31 +0000 "POST /tooooo0010O.php" 200
djc8php_1         | 2021-03-29T03:34:32.272943932Z 172.28.0.8 -  29/Mar/2021:03:34:32 +0000 "POST /tooooo0010O.php" 200
djc8php_1         | 2021-03-29T03:34:36.146157105Z 172.28.0.8 -  29/Mar/2021:03:34:36 +0000 "POST /tooooo0010O.php" 200
djc8php_1         | 2021-03-29T03:34:41.153941741Z 172.28.0.8 -  29/Mar/2021:03:34:41 +0000 "POST /tooooo0010O.php" 200
djc8php_1         | 2021-03-29T03:34:43.273966660Z 172.28.0.8 -  29/Mar/2021:03:34:43 +0000 "POST /tooooo0010O.php" 200

可以看到会自动刷新最新的日志。

搜索日志

配置管道命令,可以直接搜索日志中的内容。比如我要找index.php的访问情况

[root@VM-8-8-centos docker-compose]# docker-compose logs -f -t --tail="10"  djc8php | grep index.php
djc8php_1         | 2021-03-29T03:37:28.947307833Z 172.28.0.8 -  29/Mar/2021:03:37:28 +0000 "POST /index.php" 200
djc8php_1         | 2021-03-29T03:37:46.705196221Z 172.28.0.8 -  29/Mar/2021:03:37:46 +0000 "GET /index.php" 200
djc8php_1         | 2021-03-29T03:38:10.222839549Z 172.28.0.8 -  29/Mar/2021:03:38:09 +0000 "GET //index.php" 200
djc8php_1         | 2021-03-29T03:38:11.877778625Z 172.28.0.8 -  29/Mar/2021:03:38:11 +0000 "GET //index.php" 200
djc8php_1         | 2021-03-29T03:38:12.552731209Z 172.28.0.8 -  29/Mar/2021:03:38:12 +0000 "GET //index.php" 200
djc8php_1         | 2021-03-29T03:38:12.980108652Z 172.28.0.8 -  29/Mar/2021:03:38:12 +0000 "GET //index.php" 200
djc8php_1         | 2021-03-29T03:38:13.162310437Z 172.28.0.8 -  29/Mar/2021:03:38:13 +0000 "GET //index.php" 200
djc8php_1         | 2021-03-29T03:38:13.312737794Z 172.28.0.8 -  29/Mar/2021:03:38:13 +0000 "GET //index.php" 200
djc8php_1         | 2021-03-29T03:38:13.864792885Z 172.28.0.8 -  29/Mar/2021:03:38:13 +0000 "GET /index.php" 200

只会显示index.php访问的情况


本文作者:小蚊子
本文链接:docker-compose logs 用法 - https://djc8.cn/archives/docker-compose-logs-usage.html
版权声明: 知识共享许可协议本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

标签: docker-compose, logs, 用法

添加新评论