PHP Logging & PHP Syslog Management
We’ll show you how to setup your PHP logging, which is to send syslog to Rsyslog and then forward it to Loggly. The advantage of Rsyslog is that it can send TCP events without blocking your application, can optionally encrypt the data, and even queue data to add robustness to network failure. This guide was tested with Ubuntu 12.04.3-LTS, PHP v5.3.10, Apache 2.2.2 and the default log configuration and directories. For alternatives, please see the PHP Logging Advanced Options section.
PHP Log Setup
1. Configure Syslog Daemon
curl -O https://www.loggly.com/install/configure-linux.sh sudo bash configure-linux.sh -a SUBDOMAIN -u USERNAME
- SUBDOMAIN: your account subdomain that you created when you signed up for Loggly
- USERNAME: your Loggly username
2. PHP Configuration to Use Syslog
Open the php.ini file, in this case for apache:
sudo vim /etc/php5/apache2/php.ini
Configure it to log over syslog by uncommenting this line:
#forward log events to syslog error_log = syslog
Restart Apache so the changes take effect
sudo service apache2 restart
3. Try Sending Logs
Open a PHP file in your Apache document root to test adding some log commands
sudo vim /var/www/test.php
Add some sample code to produce two lines of output in the syslog
<?php openlog('php', LOG_CONS | LOG_NDELAY | LOG_PID, LOG_USER | LOG_PERROR); syslog(LOG_ERR, 'Error!'); syslog(LOG_INFO, 'Hello World!'); closelog(); ?>
4. Verify Events
Search Loggly for this event. If it doesn’t work, see the troubleshooting section below.
syslog.appName:"php" or using the tags you have used.
Click on one of the logs to show a list of php log fields (see screenshot below). If you don’t see them, please check that you are using one of our automatically parsed formats.
Advanced PHP Logging Options
- PHP Monolog – The popular monolog library can also send to Loggly.
- File Monitoring – You can also configure PHP to log to file by uncommenting “error_log = php_errors.log”, then monitor that file using Rsyslog. This gives you a local backup.
- Loggly Library Catalog – Check to see if there are any programming libraries available for PHP. The advantage is that you can send directly to Loggly’s HTTP/S endpoint without using Rsyslog.
- Search or post your own PHP logging, PHP syslog examples, and PHP error log questions in the community forum.
PHP Logging Troubleshooting
If you don’t see any data show up in the verification step, then check for these common problems.
- Wait a few minutes in case indexing needs to catch up
- Make sure you restarted Apache after making the changes
- Check to see if PHP’s logs are getting to Rsyslog by examining the system log at /var/log/syslog
- Run “sudo tcpdump -nnvvXS dst port 514” to verify UDP events are being sent to localhost
Check Your Syslog Daemon:
Still Not Working?
- Search or post your own PHP logs record questions, PHP examples, PHP references and setup, or PHP tutorials in the community forum.