skip to content
MJ4

Prometheus file logging

/ 1 min read

Logging

By default prometheus logs to syslog, and it doesn’t have an option to set a logging to a file in this runbook we cover prometheus file logging

Prometheus can be configured to log different levels by default the level of log is info

Terminal window
$ prometheus --log.level="warn"

and you can also control the log format, prometheus supports two formats

Terminal window
$ prometheus --log.format="logfmt|json"

Many companies use some sort of log agent that collect logs from different services and send them to a indexing platform, like filebeat for example, hence the need for prometheus to log in a file.

You can enable file logging using rsyslog configs

Rsyslog config

Create a config under /etc/rsyslog.d/10-prometheus.conf

if $programname contains 'prometheus' then /var/log/prometheus/prometheus.log
& stop

This config tells rsyslog to redirect every log entry containing prometheus in the service name to a file “/var/log/prometheus/prometheus.log`

Be careful not to ship logs for another service named prometheus e.g prometheus-exporter

Terminal window
$ systemctl restart rsyslogd
$ chmod 0660 /var/log/prometheus/prometheus.log
$ chown prometheus:syslog /var/log/prometheus/prometheus.log

Debug rsyslog

You can mimic logs being produced to syslog using logger and tailing the prometheus file, and/or check logs for rsyslog.

Terminal window
$ logger -t prometheus "Hello, World! This is prometheus logging tutorial"
$ tail -n 100 -f /var/log/prometheus/prometheus.log
$ journalctl -u rsyslogd