syslog mechanism

syslog mechanism

When developing a server program, the server is generally a daemon process, which is separated from the control terminal. Some output information and debugging information are recorded in log files. Linux provides a common syslog mechanism. Here, the basic use of syslog is introduced. According to the needs of the project, you can further study the syslog.conf configuration file.

Unix uses the syslog mechanism. Linux such as ubuntu and redhat now use rsyslog, which is an enhanced version of syslog. 

1. Configure rsyslog

south you /etc/rsyslog.conf

Add the following line to configure

# mod by itcast
local2.info /var/log/itcast_server.log 

# itcast en

2. Restart the service to take effect and create /var/log/itcast_server.log

sudo /etc/init.d/rsyslog restart

itcast@ubuntu:~$ ls -l /var/log/itcast_server.log
-rw-r----- 1 syslog adm 0 11月 22 07:47 /var/log/itcast_server.log

3. Check that /var/log/itcast_server.log is an empty file at the moment

itcast@ubuntu:~$ cat /var/log/itcast_server.log

4. Write test files and compile and run

#include <syslog.h> #include <string.h> #include <stdio.h> int main(void)

{
int log_test;

/*Open log*/
openlog("itcast.itcast ", LOG_PID|LOG_CONS, LOG_LOCAL2); /*write log*/
syslog(LOG_INFO, "PID information, pid=%d", getpid()); /*close log */
closelog();
return 0;

}

5. Check the day to file /var/log/itcast_server.log

itcast@ubuntu:~/test$ cat /var/log/itcast_server.log
Nov 22 07:51:06 ubuntu itcast.itcast [5326]: PID information, pid=5326

6. The rsyslog configured by ubuntu by default will record all syslog output in /var/log/syslog, including the information already recorded in /var/log/itcast_server.log

itcast@ubuntu:~/test$ cat /var/log/syslog




212 Chapter 16 The  syslog Mechanism

#include <syslog.h> #include <string.h> #include <stdio.h> int main(void)

{
int log_test;

/*Open log*/
openlog("itcast.itcast ", LOG_PID|LOG_CONS, LOG_LOCAL2); /*write log*/
syslog(LOG_INFO, "PID information, pid=%d", getpid()); /*close log */
closelog();
return 0;

}

5. Check the day to file /var/log/itcast_server.log

6. The rsyslog configured by ubuntu by default will record all syslog output in /var/log/syslog, including the information already recorded in /var/log/itcast_server.log

itcast@ubuntu:~/test$ cat /var/log/itcast_server.log
Nov 22 07:51:06 ubuntu itcast.itcast [5326]: PID information, pid=5326

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325809744&siteId=291194637