Rahul
Rahul

Reputation: 1807

Forwarding logs from file to journald

I have an application on the isolated machine. It writes logs to /var/log/app/log.txt for example. However, I want it to write logs to journald daemon. However, I can't change the way application run, because it is encapsulated.

I mean I can not do smth like app | systemd-cat

1) Am I right that all services started with systemd write logs to journald?

2) If so, will the children of process, started by systemd, will also write logs to journald?

3) Is there any way to tell journald to take logs from a specific file?

4) If not, are there any workarounds?

Upvotes: 1

Views: 1230

Answers (2)

brada
brada

Reputation: 469

4) I can only try to help with a workaround:

MY_LOG_FILE=/var/log/app/log.txt

# Create a FIFO PIPE
PIPE=/tmp/my_fifo_pipe
mkfifo $PIPE
MY_IDENTIFIER="my_app_name"     # just a label for later searching in journalctl 

# Start logging to journal
systemd-cat -t $MY_IDENTIFIER -p info < $PIPE &
exec 3>$PIPE

tail -f $MY_LOG_FILE > $PIPE &

exec 3>&- #closing file descriptor 3 closes the fifo

This is the basic idea, you should now think about timings, when it's needed to have this started and when to be stopped.

Upvotes: 0

AbdulKareem
AbdulKareem

Reputation: 1207

warning: this is not tested

You could mount bind /dev/stdout to log file in ExecStartPre

Example:

ExecStartPre=/use/sbin/mount --bind /dev/stdout  /var/log/app/log.txt

Or soft link /dev/stdout to log file in ExecStartPre

Example:

ExecStartPre=/use/bin/ln -s /dev/stdout  /var/log/app/log.txt

Upvotes: 1

Related Questions