减少Postgres Docker的日志输出量?

时间:2018-06-09 06:20:56

标签: postgresql docker logging

如果你跑

docker run --rm postgres

你得到:

The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    pg_ctl -D /var/lib/postgresql/data -l logfile start

****************************************************
WARNING: No password has been set for the database.
         This will allow anyone with access to the
         Postgres port to access your database. In
         Docker's default configuration, this is
         effectively any other container on the same
         system.

         Use "-e POSTGRES_PASSWORD=password" to set
         it in "docker run".
****************************************************
waiting for server to start....2018-06-09 06:11:42.530 UTC [39] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2018-06-09 06:11:42.543 UTC [40] LOG:  database system was shut down at 2018-06-09 06:11:42 UTC
2018-06-09 06:11:42.552 UTC [39] LOG:  database system is ready to accept connections
 done
server started
ALTER ROLE


/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*

2018-06-09 06:11:42.665 UTC [39] LOG:  received fast shutdown request
waiting for server to shut down....2018-06-09 06:11:42.668 UTC [39] LOG:  aborting any active transactions
2018-06-09 06:11:42.669 UTC [39] LOG:  worker process: logical replication launcher (PID 46) exited with exit code 1
2018-06-09 06:11:42.672 UTC [41] LOG:  shutting down
2018-06-09 06:11:42.687 UTC [39] LOG:  database system is shut down
 done
server stopped

PostgreSQL init process complete; ready for start up.

2018-06-09 06:11:42.776 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2018-06-09 06:11:42.776 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2018-06-09 06:11:42.780 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2018-06-09 06:11:42.791 UTC [57] LOG:  database system was shut down at 2018-06-09 06:11:42 UTC
2018-06-09 06:11:42.800 UTC [1] LOG:  database system is ready to accept connections

启动信息的63个日志行,更不用说当您开始对其执行查询时所有额外的详细程度。这是一个很好的信息,但是当你在本地开发中不断启动并一遍又一遍地销毁相同的容器时,这就是噪音。

我怎样才能“安静”这些日志?我不想完全摆脱它们......我仍然希望看到严重错误或严重警告。我还希望将它们定向到stdoutstderr,而不是单独的文件或分离的过程。

理想情况下,我正在寻找一个我可以配置的环境变量,但我没有太多运气在图像的文档中找到一个。我在官方的RabbitMQ图像上遇到了类似的问题,并且能够让它显得更加安静:

RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS=-rabbit log [{console,[{level,warning}]}]

我正在寻找与官方Postgres images类似的东西。

1 个答案:

答案 0 :(得分:0)

我没有找到环境变量,但是您可以使用配置文件来设置日志记录选项。

docker run -i --rm postgres:12.1 cat /usr/share/postgresql/postgresql.conf.sample > my-postgres.conf
echo "log_min_messages = error" >> my-postgres.conf
docker run --rm postgres -v "$PWD/my-postgres.conf":/etc/postgresql/postgresql.conf postgres -c 'config_file=/etc/postgresql/postgresql.conf'