I have Postgres16.1 running in a container in Docker Swarm, alongside inter alia Promtail, Loki and Grafana. I want to direct Postgres logs in JSON format towards Promtail for easy processing in Promtail pipeline stages (especially of multiline logs). The setting Postgres config log_destination=jsonlog will create JSON logs in log/postgresql-<DATE>.json (see documentation here). But Docker stdout/stderr logs (which Promtail reads) will remain in default config.
docker-compose.yaml:
(...)
db:
container_name: postgres
image: <from my local registry>
command: ["postgres", "-c", "log_statement=all", "-c", "logging_collector=on", "-c", "log_destination=jsonlog"]
labels:
logging: "promtail"
(...)
How can I configure Postgres to log to stderr in JSON format? Also, is this a good idea/good practice in the first place?
Not having stderr as log_destination also does not seem to influence the stderr logging behaviour of the container.
Found a straightforward solution that does not require volume mounting or other workarounds here.
After having set
logging_collector=onandlog_destination=jsonlog, configure postgreslog_filenameto something static (exluding date/time as per default config) likepostgresql.json(see doc here).Then run the container with
ln -sf /dev/stdout /var/lib/postgresql/data/log/postgresql.json.This way, you shouldn't even have to worry about postgres log rotation, as the link will reroute to stdout directly and thus to docker log management.