When writing a program, you all like to keep the process and output results in the form of logs for your reference. Most of my friends use the open method to open a file handle and write the information in it. Today I learned to use a syslog service provided by unix or unix-like. In our python, we provide a syslog module, which is an interface provided for the system syslog program.
Below I will explain the following functions using popular languages and examples:
Syslog. openlog (ident [, logopt [, facility])
This is the function used to initialize the syslog interface. Here there is a required parameter and two optional parameters.
The first parameter, ident
This is an identifier string, which is the program name in each line of log, such:
Syslog. openlog ("test. py ")
Syslog. syslog ("The process is test. py ")
Tail-n 1/var/log/messages
Apr 22 16:26:52 databak test. py: The process is test. py
Right? See it, right?
The second parameter, logopt
Option name: LOG_CONS, LOG_NDELAY, LOG_NOWAIT, LOG_PID, LOG_PERROR
You can select one or more (using or operator "|"), for example:
Syslog. openlog ("test. py", syslog. LOG_PID | syslog. LOG_PERROR)
Syslog. syslog ("The messages print pid and messages print to stderr ")
>>> Syslog. syslog ("The messages print pid and messages print to stderr ")
Test. py [16826]: The messages print pid and messages print to stderr
[Root @ databak scripts] # tail-n 1/var/log/messages
Apr 22 16:33:32 databak test. py [16826]: The messages print pid and messages print to stderr
No. On the one hand, it prints it directly, on the other hand, it records the log to messages, and adds the pid Number of the process as per your needs.
Note: LOG_PERROR errors are not only recorded in the syslog server, but also printed in stderr.
Third parameter, facility
Name: LOG_AUTH, LOG_CRON, LOG_DAEMON, LOG_KERN, LOG_LOCALx, LOG_LPR, LOG_MAIL, LOG_NEWS, LOG_USER, LOG_UUCP
How does the system administrator use these parameter configuration information to break down to different files and services.
We have always output the information to the messages file. This time, we need to combine the syslog. conf configuration to output it to other files.
Modify syslog. conf and add
Auth. */var/log/python. auth
How to write the authentication information to python. auth?
Syslog. openlog ("test. py", syslog. LOG_PID, syslog. LOG_AUTH)
Syslog. syslog ("Test Auth !")
[Root @ databak scripts] # tail-f/var/log/python. auth
Apr 22 16:43:47 databak test. py [16829]: Test Auth!
[Root @ databak scripts] # tail-n 1/var/log/messages
Apr 22 16:43:17 databak test. py [16829]: Test Auth!
No. The messages and python. auth files both have related information. What is the problem? I want it to output only to python. auth, and do not want to output to messages? I think it is because we have not configured the priority of syslog, our syslog. the default priority of syslog is LOG_INFO. Naturally, the content is output to the authentication file and messages file. Therefore, we need to modify the configuration of syslog. conf,
*. Info; *****; auth. none/var/log/messages
Try again ....
Do you understand this function? Next, let's look at the syslog function.
In the above example, the syslog function has always been with us. I believe you have also seen a few points. Now I will publicize the function to everyone.
Syslog. syslog ([priority], message)
There are two parameters. One parameter is required.
Message does not need to be said. I have all the examples. I just want to describe the optional parameters.
Priority,
Name: LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_INFO, LOG_DEBUG (LOG_INFO by default)
To write the authentication error information to the python. err file, modify syslog. conf.
Auth. err/var/log/python. err
Program Viewing:
Syslog. openlog ("test. py", syslog. LOG_PID, syslog. LOG_AUTH)
Syslog. syslog (syslog. LOG_ERR, "Add error information to python. err file ")
Result:
[Root @ databak ~] # Tail-f/var/log/python. err
Apr 22 17:05:00 databak test. py [16933]: Add error information to python. err file
[Root @ databak ~] # Tail-n 1/var/log/messages
If the experiment is successful, let me change the priority and check the program:
Syslog. openlog ("test. py", syslog. LOG_PID, syslog. LOG_AUTH)
Syslog. syslog (syslog. LOG_INFO, "Add info information to python. err file ")
Result:
[Root @ databak ~] # Tail-f/var/log/python. err
[Root @ databak ~] # Tail-n 1/var/log/messages
No two files, because your program does not meet the requirements of any one (syslog. conf), so the information will not be written to any file.