2

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?

Raptor
  • 53,206
  • 45
  • 230
  • 366
  • Did you try to use `Logger::configure('/path/to/log4php.xml');` instead of require? – arma Nov 21 '12 at 03:34
  • Just tried. No luck. the log still does not create. – Raptor Nov 21 '12 at 03:50
  • thats super strange i just downloaded log4php and used your code with replacement of that `Logger::configure('/path/to/log4php.xml');` insead of require just before calling `$logger = Logger::getLogger(basename(__FILE__));` and i got log written successfully. What does error_logs say? – arma Nov 21 '12 at 03:52
  • bingo. Apache error log says permission problem. now fixed. thanks! please put it to Answer. – Raptor Nov 21 '12 at 05:03
  • I'm using Apache log4PHP in my PHP Laravel application and following is how I initialize the logger: `Logger::configure(base_path().'/config.xml');` and in config.xml: `` My application is deployed on Amazon EC2 and I grant the permission to storage directory as follows: `sudo chmod -R 777 app/storage` But still the log file is not created. How can I check that what's the issue? – Sibtain Norain Apr 16 '15 at 06:04

2 Answers2

2

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.

arma
  • 4,084
  • 10
  • 48
  • 63
  • This is not working for me from a command line script. Getting no error either. I see no log file being created. – Volatil3 Apr 20 '16 at 10:58
0

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 :)

Chaitanya Bhojne
  • 156
  • 4
  • 10
  • Thanks for your answers, but few errors found. Never never never use 777 permission (you don't want to create a security flaw, right?). Also with respect to object-oriented approach, never put a class in your main logic file. Use a separate file to store your class. Conclusion: re-consider your logic. – Raptor Sep 19 '17 at 08:56
  • @Raptor Its only an example. But thanks for ur advice – Chaitanya Bhojne Sep 19 '17 at 09:20