Winston: Wrong Timezone

Created on 21 Jul 2014  路  6Comments  路  Source: winstonjs/winston

My system timezone is GMT+7, but winston logs messages with different timezone.

Most helpful comment

Sorry for commenting to an old issue, but I just recognized that the local timezone is used when passing a custom format to the timestamp (without creating a new datetime object).

So doing:

const options = {
  file: {
    format: timestamp({ format: 'YYYY-MM-DD HH:mm:ss.SSSZZ' }),
  }
};

shows the string in the console and the timestamp field in a log file in the correct timezone.

When just including the timestamp like this

const options = {
  file: {
    format: timestamp(),
  }
};

UTC time is used (you can tell by looking at the last _Z_ in the timestamp which stands for +00:00, see this medium post for further information on timezones and formats).

So code example 1 results in a string like 2020-05-13T23:00:07.618+0200 (GMT+2), while code example 2 appears like 2020-05-13T21:00:07.618Z (UTC).

I'm currently using winston v3.2.1

All 6 comments

Same here.

It seems that winston only support GMT (UTC +0)

If you want to change the timezone just use a custom timestamp function. See: https://github.com/flatiron/winston/issues/413#issuecomment-67208860

@indexzero Thank you very much.

Sorry for commenting to an old issue, but I just recognized that the local timezone is used when passing a custom format to the timestamp (without creating a new datetime object).

So doing:

const options = {
  file: {
    format: timestamp({ format: 'YYYY-MM-DD HH:mm:ss.SSSZZ' }),
  }
};

shows the string in the console and the timestamp field in a log file in the correct timezone.

When just including the timestamp like this

const options = {
  file: {
    format: timestamp(),
  }
};

UTC time is used (you can tell by looking at the last _Z_ in the timestamp which stands for +00:00, see this medium post for further information on timezones and formats).

So code example 1 results in a string like 2020-05-13T23:00:07.618+0200 (GMT+2), while code example 2 appears like 2020-05-13T21:00:07.618Z (UTC).

I'm currently using winston v3.2.1

You're right, the local timezone is used only when I set the timestamp format for all transports. In Nestjs:

  createWinstonModuleOptions(): winston.LoggerOptions {
    const datePattern = process.env.LOG_DATE_PATTERN;
    return {
      level: 'debug',
      format: winston.format.timestamp({ format: datePattern }),
      transports: [
        new winston.transports.Console({
          format: nestWinstonModuleUtilities.format.nestLike(),
        }),
        new DailyRotateFile({
          dirname: process.env.LOG_OUTPUT_DIR,
          filename: 'application-%DATE%',
          extension: '.log',
          createSymlink: true,
          format: winston.format.json(),
        }),
      ],
    };
  }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

sinai-doron picture sinai-doron  路  3Comments

pocesar picture pocesar  路  3Comments

xungwoo picture xungwoo  路  3Comments

mohanen picture mohanen  路  4Comments

JaehyunLee-B2LiNK picture JaehyunLee-B2LiNK  路  3Comments