如何更改温斯顿时间戳中的时区?节点

How to change timezone in Winston timestamp ? Node js

如何在此代码中更改时区?我尝试了一些代码,但对我来说并不成功。我为此写了一些函数。喜欢;

const timezoned = () => {
    return new Date().toLocaleString('en-US', {
      timeZone: 'Europe/Istanbul'
    });

这是我的代码;

const winston  = require('winston');
const { format, level, prettyPrint } = require('winston');
require('winston-daily-rotate-file');
 
  var transport = new (winston.transports.DailyRotateFile)({
    filename: 'application-%DATE%.log',
    datePattern: 'YYYY-MM-DD-HH',
    zippedArchive: true,
    maxSize: '1g',
    format:format.combine(format.timestamp(),format.prettyPrint()),
    level: 'info'
  });
 
  transport.on('rotate', function(oldFilename, newFilename) {
    // do something fun
  });
 
  var logger = winston.createLogger({
    transports: [
      transport
    ]
  });
 
  };
  module.exports.logger = logger;
 

您可以将格式值传递给时间戳格式化程序,这可以是字符串或函数。

详情在这里:https://github.com/winstonjs/logform#timestamp

所以我们可以使用您的时区功能,时间将在 Europe/Istanbul 时间,尽管格式为美国(由于 'en-US' 区域设置,您显然可以根据需要更改它)。

const winston  = require('winston');
const { format, level, prettyPrint } = require('winston');

const timezoned = () => {
    return new Date().toLocaleString('en-US', {
        timeZone: 'Europe/Istanbul'
    });
}

require('winston-daily-rotate-file');

var transport = new (winston.transports.DailyRotateFile)({
    filename: 'application-%DATE%.log',
    datePattern: 'YYYY-MM-DD-HH',
    zippedArchive: true,
    maxSize: '1g',
    format:format.combine(format.timestamp({ format: timezoned }),format.prettyPrint()),
    level: 'info'
});

transport.on('rotate', function(oldFilename, newFilename) {
    // do something fun
});

var logger = winston.createLogger({
    transports: [
    transport
    ]
});

module.exports.logger = logger;

输出将如下所示:

{
    message: 'Log test',
    level: 'info',
    timestamp: '7/16/2020, 12:34:40 PM'
}