log4php:无法创建日志文件

时间:2012-11-21 03:25:57

标签: php log4php

我使用log4php来编写文件日志。这是配置文件:

<?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>

以下是PHP中的初始化代码:

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

文件夹权限设置为777(它是Ubuntu Linux服务器),但日志文件未创建。如何调试log4php?

2 个答案:

答案 0 :(得分:2)

使用config调用的正确方法是:

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

正如Shivan Raptor所说,首先要检查日志是否存在权限问题。

答案 1 :(得分:0)

尝试一次

起初看起来很复杂,但事实并非如此。

<强> 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>

注意:不要忘记为文件夹授予777权限:)