Parse php to write logs into syslog

Source: Internet
Author: User
Tags openlog syslog

During the project, we often need to write system logs to the system syslog for convenient O & M. 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:
Copy codeThe 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:
Copy codeThe 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.