Parse php to write logs into syslog

Source: Internet
Author: User
Tags openlog
This article provides a detailed analysis of how to write logs into syslogs in php. For more information, see, we often need to write system logs to the system syslog. next we will introduce The following php syslog operations:
Configure syslog in linux
In linux, facility (device) has the following types:
AUTH common security/Authorization message
AUTHPRIV private security/Authorization message
CRON timer process
DAEMON
KERN kernel message
LOCAL0... LOCAL7 local application, not supported on windows
LPR row printer
MAIL service
NEWS Service
Messages generated by syslogd
USER-level information
UUCP subsystem
Log on to the linux system, enter the/etc directory, and enter:
The code is as follows:
Vim syslog. conf

Open the syslog configuration file
Here, you can see all the configuration information of syslog, which defines the conditions used for each log type stored in the previous section, such:
Daemon. *-/var/log/daemon. log
Defines the storage location of the logs generated by daemon, where daemon is the log type, "*" means to put all levels of logs to this file. Format:
Facility. Level-path for saving the log file, such as-/var/log/daemon. log
Level includes:
Emerg-this system is unavailable
Alert-conditions for immediate modification
Crit-false conditions that prevent implementation of certain tools or subsystems
Err-error conditions for implementing feature blocking tools or some subsystems
Warning-warning information
Notice-general conditions of importance
Info-messages that provide information
Debug-other information that does not contain function conditions or problems
None-no importance level, usually used for troubleshooting
* All levels except none
In the configuration file, we define a logging rule for our own device: local4.info-/var/log/
Run the/etc/init. d/sysklogd restart or/etc/init. d/sysklogd reload command to make the new configuration take effect. You can test the new log rules:
1. enter the command logger-p local4.info "my test log"
2. run the tail/var/log/event_log.log command.
You can view the log information you have written:
Note:In syslog. conf, local4.info indicates that all info-level and above logs are recorded here.
OK. Now we have set the required logs in ubuntu. next we will use syslog in php to write the logs to syslog in ubuntu.
The following php code is directly used:
The code is as follows:
Openlog ("Event1.0", LOG_PID | LOG_PERROR, LOG_LOCAL4 );
Syslog ($ level, "log message:". $ errinfo );
Closelog ();

For the specific usage of the above methods, see The php api. the specific usage is not described here.
The first parameter of openlog is the log ID, which is automatically added at the beginning of the log information to indicate which system logs are written.
Because we want to write logs to local4.info, the third parameter must use LOG_LOCAL4, which indicates the device information to write logs.
$ 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.