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?

5
  • Did you try to use Logger::configure('/path/to/log4php.xml'); instead of require? Commented Nov 21, 2012 at 3:34
  • Just tried. No luck. the log still does not create. Commented Nov 21, 2012 at 3: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? Commented Nov 21, 2012 at 3:52
  • bingo. Apache error log says permission problem. now fixed. thanks! please put it to Answer. Commented Nov 21, 2012 at 5: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: <param name="file" value="app/storage/logs/log.ERROR.log" /> 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? Commented Apr 16, 2015 at 6:04

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is not working for me from a command line script. Getting no error either. I see no log file being created.
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 :)

2 Comments

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 Its only an example. But thanks for ur advice

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.