log4php和时区

Does anyone know if it is possible to set timezone in the log4php library configuration?

I did not see any information regarding this in the official docs, but log4j has this implemented.

Right now, I am relying on php's *date_default_timezone_set* function to do the trick, but I wanted to leave log4php to handle this on its own... I wonder if there is a to-do list for this or we are supposed to rely on the built-in function by ourselves.

Here is the code I have:

date_default_timezone_set("America/New_York");
require_once (dirname(__FILE__) . '/lib/log4php/Logger.php');
Logger::configure(
    array(
        'appenders' => array(
            'default' => array(
                'class' => 'LoggerAppenderRollingFile',
                'layout' => array(
                    'class' => 'LoggerLayoutPattern',
                    'params' => array(
                        'conversionPattern' => '%d{Y-m-d H:i:s.u} [%t] %-5p - %m%n'
                    )    
                ),
                'params' => array(
                    'file' => '/var/log/myapp/myapp.' . date('Y-m-d') . '.log',
                    'maxFileSize' => '1MB',
                    'maxBackupIndex' => 10,
                ),
            ),
        ),
        'rootLogger' => array(
            'appenders' => array('default'),
        ),
    )
);  

$logger = Logger::getLogger('myapp');

for( $i=0; $i<5000; $i++ ) {
    $logger->info("This is a test [${i}].");
}

In case this code serves someone else with similar issue.

Be safe,

A defined default timezone belongs to a fully working php application.

Apart from that, you are not very clear in stating what you expect log4php to do. The project has everything from mailing list to issue tracker - you are welcome to send your wishes.

I had a similar problem. You can log the date and time for UTC/GMT in the log4php by changing one line of code in the module.

Here is how I did. Go to the module and find the file LoggerPatternConverterDate.php.

cd log4php/pattern/
vim LoggerPatternConverterDate.php

Find the private function date($format, $utimestamp) (Line 84 for me) and change the line of code that returns.

This: return date(preg_replace('`(?<!\\\\)u`', $ms, $format), $timestamp);

Becomes: return gmdate(preg_replace('`(?<!\\\\)u`', $ms, $format), $timestamp);

Also, find the file: log4php/appenders/LoggerAppenderDailyFile.php and change the following line:

This: return date($this->datePattern, $timestamp); Becomes: return gmdate($this->datePattern, $timestamp);

NOTE: The only thing changed is the function used to format the date string. date() depends on timezone that you set using date_default_timezone_set whereas gmdate() formats the date and time in UTC/GMT irrespective of default timezone.