winstonjs / winston

A logger for just about everything.
http://github.com/winstonjs/winston
MIT License
22.65k stars 1.8k forks source link

How to change timestamp field name in json #1075

Open Algram opened 7 years ago

Algram commented 7 years ago

Hello,

I would like to change the field name from timestamp to @timestamp in the JSON-File. How can I do that with Winston?

Thank you for your help!

universalmind303 commented 4 years ago

I know this is a super old question, but you can create your own formatter

const timeStampFormatter = format((info, opts = {}) => {
    if (opts.format) {
      info['@timestamp'] = typeof opts.format === 'function'
        ? opts.format()
        : fecha.format(new Date(), opts.format);
    }

    if (!info.timestamp) {
      info['@timestamp'] = new Date().toISOString();
    }

    if (opts.alias) {
      info[opts.alias] = info.timestamp;
    }

    return info;
  });

then use it when creating your logger


const logger = createLogger({
    format: format.combine(
        timeStampFormatter({
            format: 'YYYY-MM-DD HH:mm:ss'
        }),
       // other formatters
    ),
    // other options
});