Parse php how to write logs to syslog

  • 2020-06-23 00:05:33
  • OfStack

In order to facilitate the operation and maintenance of projects, we often need to write the system log to system syslog. Here we introduce the operation of php to syslog under linux:
Configure syslog in linux
In linux, facility(devices) have the following types:
AUTH General security/authorization messages
AUTHPRIV private security/authorization messages
CRON timer process
The DAEMON daemon
KERN kernel messages
LOCAL0... LOCAL7 native application, not supported on windows
LPR line printer
MAIL Mail Service
NEWS News Service
SYSLOG is a message generated internally by syslogd
USER general user level information
UUCP UUCP subsystem
Log in linux system, enter /etc directory, and enter:

vim syslog.conf

Open the configuration file for syslog
Here you can see all of the configuration information for syslog, which defines the criteria used to store logs for each log type mentioned in the previous section, such as:
daemon.* -/var/log/daemon.log
Defines where the logs generated by daemon are stored, where daemon is the log type and "*" represents where all levels of logs are placed. Format for:
facility. level - log file path, such as - / var log/daemon log
level include:
emerg - This system is not available
alert - Conditions that need to be modified immediately
crit - Error conditions that prevent some tool or subsystem functionality from being implemented
err - Error conditions that prevent the implementation of part of the functionality of the tool or some subsystem
warning - Warning information
notice - General conditions of importance
info - Messages that provide information
debug - does not contain additional information about function conditions or problems
none - No important level, usually used for troubleshooting
* All levels except none
Below we define a logging rule for our own device in the configuration file: local4.info -/var/log/
Then execute commands/etc/init d/sysklogd restart or/etc/init d/sysklogd reload gives effect to the configuration of the new, we can test under the new rules of the log:
1. Enter the command ES98en-ES99en local4.info "my test log"
tail /var/log/ ES109en_ES110en.log
You can see the log information you wrote:
Note: IN ES114en.conf, local4.info represents that all info levels and above are logged here
ok, so far we have set up the log we need in ubuntu, now we use syslog in php to write the log to syslog in ubuntu.
The php code is shown below:

openlog("Event1.0", LOG_PID | LOG_PERROR, LOG_LOCAL4);
syslog($level, "LOG MESSAGE: " . $errinfo);
closelog();

For specific usage of each of the above methods, please check API of PHP, which will not be repeated here.
The first parameter of openlog is the log id, which is automatically added at the beginning of the log information to indicate what system was writing the log.
Since we want to write the log to ES136en4.info, the third parameter USES LOG_LOCAL4, which represents the device information being written to the log.
In syslog, $level 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.

Related articles: