On Mo, 27.09.21 15:40, Arjun D R (drarju...@gmail.com) wrote: > Hi Folks, > > Currently we are using systemd-journald for service logging. We run > journalctl for a bunch of services and redirect those to the custom log > files for every few seconds. This takes up the CPU for that particular > time period since we have lot of IO operations as well. We came to know > that systemd version v236+ supports direct logging > (StandardOutput:file:<log_file>) to the custom log file by the service. I > would like to use that facility but we don't get the prefix that we used to > get when using the journal. > > Is there a way to prepare a custom patch locally to add the necessary > prefix to the stdout before writing to the custom log file? Is that a good > idea? Any other suggestions?
You might define a socket unit 'prefixlogger@.socket' like this: [Unit] StopWhenUnneeded=yes [Socket] ListenFIFO=/run/prefixlogger.fifo.%i Service=prefixlogger@%i.service And then a matching service 'prefixlogger@.service': [Service] StandardInput=socket StandardOutput=file:/var/log/foo.log.%i ExecStart=sed -e 's/^/foo:/' And then in the services that shall run with this: [Unit] Wants=prefixlogger@%N.socket After=prefixlogger@%N.socket [Service] ExecStart=/my/service/binary StandardOutput=file:/run/prefixlogger/fifo.%N (This is all untested, might need some minor changes to actually work, but you get the idea). So what this does is this: first we define a little socket service that can be initialized easily a bunch of times: it listens on a FIFO in the fs and everything it reads from it it writes to some log file. The service is just an invocation of "sed" with standard input being the fifo and standard output being the log file to write to. You then use it by using StandrdOutput=… in your main unit, to connect its stdout/stderr to that fifo. Also, you add deps so that each time a service that tneeds this starts the log prefix service socket for it starts too. Lennart -- Lennart Poettering, Berlin