4

How can I change date format of the Zend_Log?

Now there is date with timestamp added in front of every new log entry:
"2013-01-28T16:47:54+01:00 ... some log message ..."

But I would like to format this date like:
"Y-m-d H:i:s ... some log message ..."

My code looks like this:

class Game_Logger {

    public function __construct($val, $txt = null) {
        $writer = new Zend_Log_Writer_Stream(APPLICATION_PATH . '/../log/log.log');
        $logger = new Zend_Log($writer);
        if (is_array($val)) {
            $output = Zend_Debug::dump($val, null, false);
        } else {
            $output = $val;
        }
        if($txt){
            $output = $txt.' '.$output;
        }
        $logger->info($output);
    }

}
Bartek Kosa
  • 842
  • 1
  • 14
  • 25

2 Answers2

6

Probably this will resolve your problem:

$logger->setTimestampFormat("H:i:s");
but something tells me you already figured it out ;).

Robert
  • 316
  • 1
  • 12
  • Of course you need to update format to this one you actually want. – Robert Feb 25 '13 at 16:07
  • Yes, this indeed resolved my problem. I didn't have time to figure it out and it wasn't a blocker so I just waited for someone to answer my question. Thank you. – Bartek Kosa Mar 01 '13 at 10:28
1

For Zend Framework 2 and 3, below code might be help

    $logger = new \Zend\Log\Logger();

    $formatter = new  \Zend\Log\Formatter\Simple();
    $formatter->setDateTimeFormat('Y-m-d'); // as per your choice

    $writer = new \Zend\Log\Writer\Stream('php://output');
    $writer->setFormatter($formatter);
    $logger->addWriter($writer);
Gautam Rai
  • 2,445
  • 2
  • 21
  • 31