Question

I use log4php to write file logs. Here is the configuration file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns="http://logging.apache.org/log4php/">
    <appender name="default" class="LoggerAppenderDailyFile">
        <layout class="LoggerLayoutPattern">
            <param name="conversionPattern" value="%date %logger %-5level %msg%n" />
        </layout>
        <param name="file" value="/path/to/log/register-%s.log" />
        <param name="datePattern" value="Y-m-d" />
        <param name="append" value="true" />
    </appender>
    <root>
        <level value="info" />
        <appender_ref ref="default" />
    </root>
</configuration>

And here is the initialization codes in PHP :

require_once('/path/to/log4php/Logger.php');
require_once('/path/to/log4php.xml');
$logger = Logger::getLogger(basename(__FILE__));
$logger->info('Testing');

the folder permission is set to 777 ( it's a Ubuntu Linux server ), but the log file didn't create. How do I debug the log4php?

Was it helpful?

Solution

Proper way of calling with config would be:

require_once('/path/to/log4php/Logger.php');
Logger::configure('/path/to/log4php.xml');
$logger = Logger::getLogger(basename(__FILE__));
$logger->info('Testing');

As Shivan Raptor stated, first thing to do is to check logs for permission problems.

OTHER TIPS

Try this once

It looks complicated at first, but it isn't.

main.php

require_once('/path/to/log4php/Logger.php');
Logger::configure('log4php.xml');

Class Log
{
    private $log;

    public function __construct()
    {
        $this->log = Logger::getLogger(__CLASS__);
    }
    public function myLog()
    {
        //this prints your data.
        $this->log->info("Hello, this is log here");
    }
}

//simply call the function
$log=new Log();
$log->myLog();

log4php.xml

<?xml version="1.0" encoding="UTF-8"?>
<log4php:configuration xmlns:log4php="http://logging.apache.org/log4php/">
    <appender name="fileappender" class="LoggerAppenderFile">
        <layout class="LoggerLayoutSimple" />
        <param name="file" value="/path/to/log/register-%s.log" />
        <!-- <param name="append" value="true" /> -->
    </appender>
    <logger name="Log">
        <appender_ref ref="fileappender" />
    </logger>
</log4php:configuration>

Note : Don't forget to give 777 permission to folders :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top