tags:

views:

200

answers:

3

I need to write the local time information , the exception message to a php log file. I don't know why I can't get the format correct. The script I use is this:

  error_log('['.date("F j, Y, g:i a").']'.$msg." <br>", 3,  $phperrorPath);

There are two problem with this:

  1. The date is not written in local time, instead it's default to the GMT+0 time; I want to write the machine time information to the log
  2. After the file is written, it's not break to another line.

How to modify this code?

+1  A: 

Set your local time using date_default_timezone_set:

date_default_timezone_set('UTC');

Adding the e and O format parameters to the date call will give you the timezone identifier and GMT offset:

date("F j, Y, g:i a e O")

Add a "\n" to the end of the string:

//I've left in the HTML line break
error_log('['.date("F j, Y, g:i a e O").']'.$msg."<br /> \n", 3,  $phperrorPath);

Lines will not be concatenated when using "3" as the second argument to error_log, and using "0" will write the output to the default location specified in php.ini. "2" is no longer an option, so I think manually concatenating the line break is the way to go. See:

http://php.net/manual/en/function.error-log.php

Also, take a look at localtime

karim79
I tried your code, no luck: this is the output: [July 2, 2009, 8:54 am UTC +0000]hello<br /> [July 2, 2009, 8:54 am UTC +0000]hello<br /> . There is neither line break, nor the time is correct ( not local time)
Ngu Soon Hui
Did you set to your timezone?
karim79
Ah, is it not possible to get the machine time zone?
Ngu Soon Hui
Try this date("F j, Y, g:i a e O")
karim79
+1  A: 

As for "2" I suspect that is because you are using the HTML formatting line break
instead of the file line break \n. Maybe that's worth a try.

Manrico Corazzi
+1  A: 

Try using:

date_default_timezone_set('MYT');

That will set the script's timezone to Kuala Lumpur, Malaysia (that's your profile's country).

Then use:

error_log('['.date("F j, Y, g:i a e O").']'.$msg."<br />\r\n",3,$phperrorPath);

I don't know if you want to leave the <br /> HTML tag there, so I left it there.

Pedro Cunha
Should you need to change the timezone, a list of the available timezones that you can use as parameter for the date_default_timezone_set() function is located at http://www.php.net/manual/en/timezones.php
Pedro Cunha