被注册为systemd的service的进程,其标准输出和标准错误会被重定向到journald,因此标准输出和标准错误的输出都会被写到rounal log中:
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
printf("<7>hello debug\n");
printf("hello info\n");
printf("<4>hello warning\n");
printf("<3>hello error\n");
cout<<"<7>hello debug !!!\n"<<endl;
cout<<"hello info !!!\n"<<endl;
cout<<"<4>hello warning !!!\n"<<endl;
cout<<"<3>hello error !!!\n"<<endl;
return 0;
}
//g++ -o p_log p_log.cpp
然后在/etc/systemd/system目录下编写一个p_log.service文件,内容如下:
[Unit]
Description=test log
[Service]
ExecStart=/xxx/p_log
[Install]
WantedBy=multi-user.target
这样p_log