<div dir="ltr">Hi Lennart,<div><br></div><div>Please help me understand how the journald is figuring out the PID of the log line. I believe, with the PID, the journald is able to get the remaining details (process name) from proc fs. I wonder how the journal is able to get the PID of the log contributor as there can be many processes spawned under a service! I am trying to understand the journald and it would be really helpful to know this magic. </div><div><br></div><div>Thanks in advance,</div><div>Arjun</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Sep 29, 2021 at 12:14 PM Arjun D R <<a href="mailto:drarjun95@gmail.com">drarjun95@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi Lennart,<div><br></div><div>That's a good idea but still I would like to have the prefix as it is in the journal . I understand it is impossible to bypass the journal and expect the direct logging to be the same as journal entries. We can achieve it through socket but still we cannot have the luxurious prefix as in journal. </div><div><br></div><div>I would better like to have a timer unit and corresponding logger unit. Based on each service logging capability, let me decide the time. </div><div><br></div><div>So it's like:</div><div><br></div><div>Timer unit - To trigger the logger service for every n seconds (we can set it as per the service logging range).</div><div>Logger Service - Run journalctl -u <service> and the service is designed to pass the stdout to the file (using StandardOutput)</div><div><br></div><div>By this way, I can avoid the script running for every few seconds which runs journalctl for almost 30+ services and redirect it to the respective log files one by one at a moment which is impacting the load. </div><div><br></div><div>Thanks for the help.</div><div><br></div><div>--</div><div>Arjun</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Sep 28, 2021 at 7:06 PM Lennart Poettering <<a href="mailto:lennart@poettering.net" target="_blank">lennart@poettering.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Mo, 27.09.21 15:40, Arjun D R (<a href="mailto:drarjun95@gmail.com" target="_blank">drarjun95@gmail.com</a>) wrote:<br>
<br>
> Hi Folks,<br>
><br>
> Currently we are using systemd-journald for service logging. We run<br>
> journalctl for a bunch of services and redirect those to the custom log<br>
> files for every few seconds. This takes up the CPU for that particular<br>
> time period since we have lot of IO operations as well. We came to know<br>
> that systemd version v236+ supports direct logging<br>
> (StandardOutput:file:<log_file>) to the custom log file by the service. I<br>
> would like to use that facility but we don't get the prefix that we used to<br>
> get when using the journal.<br>
><br>
> Is there a way to prepare a custom patch locally to add the necessary<br>
> prefix to the stdout before writing to the custom log file? Is that a good<br>
> idea? Any other suggestions?<br>
<br>
<br>
You might define a socket unit 'prefixlogger@.socket' like this:<br>
<br>
[Unit]<br>
StopWhenUnneeded=yes<br>
<br>
[Socket]<br>
ListenFIFO=/run/prefixlogger.fifo.%i<br>
Service=prefixlogger@%i.service<br>
<br>
And then a matching service 'prefixlogger@.service':<br>
<br>
[Service]<br>
StandardInput=socket<br>
StandardOutput=file:/var/log/foo.log.%i<br>
ExecStart=sed -e 's/^/foo:/'<br>
<br>
And then in the services that shall run with this:<br>
<br>
[Unit]<br>
Wants=prefixlogger@%N.socket<br>
After=prefixlogger@%N.socket<br>
<br>
[Service]<br>
ExecStart=/my/service/binary<br>
StandardOutput=file:/run/prefixlogger/fifo.%N<br>
<br>
(This is all untested, might need some minor changes to actually work,<br>
but you get the idea).<br>
<br>
So what this does is this: first we define a little socket service<br>
that can be initialized easily a bunch of times: it listens on a FIFO<br>
in the fs and everything it reads from it it writes to some log<br>
file. The service is just an invocation of "sed" with standard input<br>
being the fifo and standard output being the log file to write to.<br>
<br>
You then use it by using StandrdOutput=… in your main unit, to connect<br>
its stdout/stderr to that fifo. Also, you add deps so that each time a<br>
service that tneeds this starts the log prefix service socket for it<br>
starts too.<br>
<br>
Lennart<br>
<br>
--<br>
Lennart Poettering, Berlin<br>
</blockquote></div>
</blockquote></div>