92

I'm tailing logs of my own app and postgres.

tail -f /tmp/myapp.log /var/log/postgresql/postgresql.main.log

I need to include pgpool's logs. It used to be syslog but now it is in journalctl.

Is there a way to tie tail -f && journalctl -f together?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
bikey
  • 921

5 Answers5

146

You could use:

journalctl -u service-name -f

-f, --follow

Show only the most recent journal entries, and continuously print new entries as they are appended to the journal.

Here I've added "service-name" to distinguish this answer from others; you substitute the actual service name instead of the text service-name.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Toufic
  • 1,561
21

You could forward your log entries to journal:

systemd-cat tail -f /tmp/myapp.log /var/log/postgresql.log

and then use journalctl -f... though as Mark mentioned, that will print all journal entries.


One way to filter only the messages from those logs and from that particular unit is to use a distinct SYSLOG_IDENTIFIER i.e. edit the unit file and under the [Service] section add e.g.

SyslogIdentifier=my_stuff

restart the unit then run systemd-cat with the same identifier

systemd-cat -t my_stuff tail -f /tmp/myapp.log /var/log/postgresql.log

and finally query the journal only for that particular identifier:

journalctl -f -t my_stuff
don_crissti
  • 82,805
5

If you have bash available, you can use process substitution as one of the tail parameters:

tail -f /tmp/myapp.log /var/log/postgresql/postgresql.main.log <(journalctl -f)
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • 1
    This is the elegant way I also tried, but is doesn't work. Tail shows that it's using /dev/fd/xx, but no logs are displayed. – bikey Oct 07 '16 at 15:12
  • correct; /dev/fd/xxx is the temporary pipe that bash uses in order to implement the process substitution; any journalctl output will show up in there. – Jeff Schaller Oct 07 '16 at 15:13
  • 5
    You probably don't want to follow the whole journal but just those for the pgpool unit. Assuming the service named pgpool.service, try journalctl -fu pgpool.service. Also, if you are not running this as root, make sure the user is in the systemd-journal group! – Mark Stosberg Oct 10 '16 at 13:50
1

try something like:

tail -f /tmp/myapp.log >> /tmp/tail.log &
journalctl -f >> /tmp/tail.log &
tail -f /tmp/tail.log
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Paul
  • 11
0

Using journaltail:

journaltail --unit pgpool.service /tmp/myapp.log /var/log/postgresql/postgresql.main.log

I just threw the script together an hour ago after looking for a solution at stackoverflow et al.

GDR
  • 121