Errors and Logs: Configure PHP Applications

You can capture errors and logs for PHP applications in a number of ways. Using the Stackify Library, you can either use a plug-in for an existing Monolog or log4php framework, or you can log to us directly via the Stackify API.  Specific configuration instructions for each framework plug-in or using the API can be found on the Stackify Github page.  Below will go through the process of how you can capture your logs from your PHP Apps by sending your syslogs to Retrace.

PHP Logging with Syslog

By Setting Up Logging With Syslog, you will be able to see logs from your PHP apps. One thing to take note of is that there needs to be a Retrace Agent running on the server where the PHP Apps are being hosted for logging to syslog to work. The PHP App will pass logging messages to syslog which will then be captured by Retrace’s Logs Dashboard. One major drawback of using syslog with Retrace is that you will not be able to view errors in the Errors Dashboard because exception details will not be captured in syslog. You will need to use Stackify’s PHP library in your app to take advantage of the full capabilities of Retrace. You can read more about the PHP library below.

Using Native Syslogs

Using Native Syslogs

You can capture syslogs directly without using any logging libraries such as Monolog, Log4Php, and others. In order to do this, you will use the openlog function. Note that openlog must be called before any calls of syslogs. Once you have this configured in your PHP App, you will now have log messages sent to syslog. To learn how to get the syslog messages feed into Retrace, go to the Setting Up Logging With Syslog article.

Viewing Logs in the Logs Dashboard

While configuring your PHP App to send to syslog, the App name needs to match the App name in Retrace for the messages to appear in the Logs module of the App Dashboard.

 

Using Other Logging Libraries to Write to Syslog

You also have the option to use standalone libraries to capture your syslogs. These libraries include Monolog , Zend Log, and Apache Log4php. Read below to see how these libraries can be configured to send logs to syslog.

Monolog Configuration

You can configure Monolog to write to syslog by using a SyslogHandler. The code snippets below show how to import the SyslogHandler class, create a Logger instance, and then call the SyslogHandler:

Reference the SyslogHandler class:

use Monolog\Logger;
use Monolog\Handler\SyslogHandler;Create an instance of Logger:
$logger = new Logger('logger_name');

Call the SyslogHandler class:

$logger->pushHandler(new SyslogHandler('application_name'));

You can use as many handlers together as you want as long as you include the SyslogHandler.

Zend Configuration

You can configure Zend to write to syslog by adding a Syslog writer manually in your code. The code snippets below show how to import Syslog, create a Logger instance, and call syslog:

Reference the syslog writer:

use ZendLogLogger;
use ZendLogWriterSyslog;

Create an instance of Logger:

$logger = new Logger();

Call the Syslog Writer

// Syslog writer parameters must include 'application'
$syslogParams = array(
	'application' => 'application_name',
);
// Create syslog writer with these parameters and add writer
$logger->addWriter(new Syslog($syslogParams));

You can use as many handlers together as you want as long as you include the Syslog writer.

Log4php Configuration

You can configure Log4php by creating a new LoggerAppenderSyslog and setting the custom “ident” parameter in configuration file. This parameter should match your application name. Below is an example of the Log4php configuration file with two different appenders listed. One of the appenders is syslog initialized by the application name. Please note that log4php supports other configuration formats (INI, PHP, programmatic, etc.). You can see the documentation for Log4php for more details.

<configuration >
    <appender name="fileAppender" class="LoggerAppenderFile">
        <param name="file" value="data/app.log" />
    </appender>
    <appender name="stackifyAppender" class="LoggerAppenderSyslog">
        <param name="ident" value="application_name" />
    </appender>
    <root>
        <level value="TRACE" />
        <appender_ref ref="fileAppender" />
        <appender_ref ref="stackifyAppender" />
    </root>
</configuration>

You will also need to reference the configuration file in your code. See below for an example of how to do this:

Logger::configure('config/log4php.xml');
$logger = Logger::getLogger('logger_name');
$logger->trace('Trace message');
$logger->warn('Warning message');
$logger->fatal('Fatal message');

Logging with Stackify’s Errors and Logs PHP Libraries

Logging with Stackify's Errors and Logs PHP Libraries

 

Feel free to contact us at [email protected] for any help getting errors and logs set up with PHP.