PHP code example of stackify / log4php

1. Go to this page and download the library: Download stackify/log4php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

stackify / log4php example snippets


try {
    $db->connect();
catch (DbException $ex) {
    $logger->error('DB is not available', $ex);
}

<param name="debug" value="1" />

// Sample PHP Config
$config = array(
    'Debug' => true,
    'DebugLogPath' => '/path/to/log.log'
);
// 1. Converting PHP array to JSON String
// - String: {"Debug":true,"DebugLogPath":"/path/to/log.log"}
$jsonString = json_encode($config);
// 2. XML Encode the JSON String
//   - ' is replaced with &apos;
//   - " is replaced with &quot;
//   - & is replaced with &amp;
//   - < is replaced with &lt;
//   - > is replaced with &gt;
// - Encoded String: {&quot;Debug&quot;:true,&quot;DebugLogPath&quot;:&quot;/path/to/log.log&quot;}
// - Note: In case the string has new line ("\n") or a carriage return ("\r") character, you still need to escape it
//   - \n should be replaced with &#10; or &#xA;
//   - \r should be replaced with &#13; or &#xD;
// - Reference: https://stackoverflow.com/a/29924176/14542233
$xmlEncode = htmlspecialchars($jsonString, ENT_XML1 | ENT_QUOTES, 'UTF-8');

// 3. Add it as a value on XML Configuration
<param name="config" value="{&quot;Debug&quot;:true,&quot;DebugLogPath&quot;:&quot;/path/to/log.log&quot;}" />


return array(
    'rootLogger' => array(
        'appenders' => array('stackifyAppender'),
    ),
    'appenders' => array(
        'stackifyAppender' => array(
            'class' => '\Stackify\Log\Log4php\Appender',
            'params' => array(
                'appName' => 'application-name',
                'environmentName' => 'environment-name',
                // Additional configuration
                'config' => array(
                    'Debug' => true,
                    'DebugLogPath' => 'logConfig.log'
                )
            )
        )