jbroadway/analog

Analog use gmdate

melbichon opened this issue · 7 comments

Gmdate() is used to print current date.
Why don't you use date() ?
Can you add a way to configure this ?

My thinking was to provide the date in a consistent way. I always use GMT/UTC for storing dates, and adjust the output to the current user's timezone as necessary for display.

Yes but for File Handler for example, we can't adjust output.
Can you do something for this ?

I'll have to look at it more carefully to abstract the date function used, but for now a quick solution would be to create your own logging handler based on one of the existing ones, like this:

<?php

namespace myapp\Logging;

/**
 * Append to the specified log file. Does the same thing as the default
 * handling.
 *
 * Usage:
 *
 *     $log_file = 'log.txt';
 *     Analog::handler (myapp\Logging\FileWithLocalDates::init ($log_file));
 *     
 *     Analog::log ('Log me');
 *
 * Note: Uses Analog::$format for the appending format.
 */
class FileWithLocalDates {
    public static function init ($file) {
        return function ($info, $buffered = false) use ($file) {
            $f = fopen ($file, 'a+');
            if (! $f) {
                throw new \LogicException ('Could not open file for writing');
            }

            if (! flock ($f, LOCK_EX)) {
                throw new \RuntimeException ('Could not lock file');
            }

            // convert to local time
            $time = strtotime ($info['date']);
            $info['date'] = date ('Y-m-d H:i:s', $time);

            fwrite ($f, ($buffered)
                ? $info
                : vsprintf (\Analog\Analog::$format, $info));
            flock ($f, LOCK_UN);
            fclose ($f);
        };
    }
}

This should correct for local time before logging to a file. The only things changed from the File handler are the namespace, class name, and the conversion to local time. Hope that helps!

lux commented

This should be able to be solved via the Analog::$timezone setting now:

$dt = new \DateTime ('now', new \DateTimeZone (self::$timezone));

I would like to suggest a new method in analog/lib/Analog/Analog.php :

public static function setTimeZone ($timezone) {
self::$timezone = $timezone;
}

@lucasmichel I'm into that. Would you like to make a PR for it?

@lucasmichel I'm into that. Would you like to make a PR for it?

yes, as soon as i have some time here i do.