In order to facilitate operation and maintenance when working on projects, we often need to write system logs to the system syslog. Below we will introduce the operation of php on syslog under Linux:
Configuration in Linux syslog
In Linux, facilities (devices) have the following types:
AUTH common security/authorization messages
AUTHPRIV private security/authorization messages
CRON timer process
DAEMON daemon
KERN kernel message
LOCAL0... LOCAL7 local application, not supported on windows
LPR line printer
MAIL mail service
NEWS news service
SYSLOG by Messages generated within syslogd
USER General user-level information
UUCP UUCP subsystem
Log in to the Linux system, enter the /etc directory, enter:
Copy the code The code is as follows:
vim syslog.conf
Open the syslog configuration file
Here, you can see All configuration information to syslog, here defines the conditions used to store logs for each log type mentioned in the previous section, such as:
daemon.* -/var/log/daemon.log
definition Specifies the storage location of the logs generated by the daemon, where daemon is the log type, and "*" means that all levels of logs are placed in this file. The format is:
facility. level - the path where the log file is saved, such as -/var/log/daemon.log
level includes:
emerg - the system is unavailable
alert - conditions that need to be modified immediately
crit - error conditions that prevent certain tools or subsystem functions from being implemented
err - error conditions that prevent tools or some subsystem functions from being implemented
warning - early warning information
notice - important Ordinary conditions
info - informational messages
debug - does not contain function conditions or other information about the problem
none - no importance level, usually used for debugging
* All levels except none
Next we define a log rule for our own device in the configuration file: local4.info -/var/log/
Next, execute the command /etc/init.d/sysklogd restart or /etc/init.d/sysklogd reload makes the new configuration take effect. Now we can test the new log rules:
1. Enter the command logger -p local4.info "my test log"
2. Execute the command tail /var/log/event_log.log
You can see the log information you wrote: Note:
local4.info in syslog.conf represents all Logs of info level and above will be recorded hereok, now we have set up the logs we need in ubuntu, now we use syslog in php to write the logs to syslog in ubuntu.
The following is the php code directly:
Copy the code The code is as follows:
openlog(" Event1.0", LOG_PID | LOG_PERROR, LOG_LOCAL4);
syslog($level, "LOG MESSAGE: " . $errinfo);
closelog();
The above methods Please check the PHP API for specific usage. The specific usage will not be described here.
The first parameter of openlog is the log identifier, which is automatically added to the beginning of the log information to indicate what system wrote the log.
Since we want to write the log to local4.info, the third parameter needs to use LOG_LOCAL4, which represents the device information for writing the log.
$level in syslog is the log level, including:
LOG_EMERG system is unusable LOG_ALERT action must be taken immediately
LOG_CRIT critical conditions
LOG_ERR error conditions
LOG_WARNING warning conditions
LOG_NOTICE normal, but significant, condition
LOG_INFO informational message
LOG_DEBUG debug-level message
The second parameter is the specific log content.
http://www.bkjia.com/PHPjc/327942.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327942.htmlTechArticleIn order to facilitate operation and maintenance when doing projects, we often need to write system logs to the system syslog. Below we Let me introduce the operation of syslog by PHP under Linux: Configure in Linux...