This article is the PHP in the log into the Syslog method for a detailed analysis of the introduction, the need for friends under the reference
In doing the project in order to facilitate operation, we often need to write system log to system syslog, below we will introduce, Linux under the operation of the syslog:
Configuring Syslog in Linux
in Linux, facility (devices) have the following:
AUTH General Security/Authorization message
Authpriv Private Security/Authorization message
CRON Timer Process
DAEMON Daemon Process
KERN Kernel Message
LOCAL0 ... LOCAL7 local application, not supported on Windows
LPR Line Printer
Mail Mail Service
News Service
A message generated by the SYSLOG from within the SYSLOGD
User-level information common to users
UUCP UUCP Subsystem
Login to Linux system, enter/etc directory, enter:
Copy Code code as follows:
Vim syslog.conf
Open the configuration file for the Syslog
Here you can see all the configuration information for Syslog, which defines the conditions used for each log type store log mentioned in the previous section, such as:
Daemon.*-/var/log/daemon.log
Defines the location where the log produced by Daemon is saved, where Daemon is the journal type, and "*" represents all levels of logging to the file. The format is:
Facility. Level-the path where the log file is saved, such as-/var/log/daemon.log
Level includes:
Emerg-This system is not available
Alert-conditions that require immediate modification
Crit-Error conditions that prevent certain tools or subsystem features from being implemented
ERR-block tool or error condition implemented by some subsystem functions
Warning-Early warning information
Notice-General conditions of importance
Info-messages to provide information
Debug-Other information that does not contain function conditions or problems
None-no important level, usually used for scheduling errors
* All levels, except none
Below 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 make the new configuration take effect, and we can test the new log rules:
1. Input command logger-p local4.info "My test Log"
2. Execute Command tail/var/log/event_log.log
you can see the log information written by yourself:
Note:The syslog.conf local4.info represents all info levels and the above logs will be recorded here
OK, now that we've set up the logs we need in Ubuntu, we'll use syslog in PHP to write the logs to the syslog in Ubuntu.
the following directly on the PHP code:
Copy CodeThe code is as follows:
Openlog ("Event1.0", Log_pid | Log_perror, LOG_LOCAL4);
syslog ($level, "LOG message:". $errinfo);
Closelog ();
The specific use of the above methods please check the PHP API, the specific usage is not here to repeat.
The first parameter of Openlog is the log ID, which is automatically added to the beginning of the log information to indicate what system writes to the log.
Because we want to write the log to Local4.info, the third parameter needs to use LOG_LOCAL4, which represents the device information written to the log.
$level in Syslog is 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.