dbhowell / pino-cloudwatch

AWS CloudWatch Logs transport for pino
MIT License
38 stars 13 forks source link

how read time or in general a property and trasform it #14

Closed MassimoCappellano closed 4 years ago

MassimoCappellano commented 4 years ago

Sorry, I'm new to stream and nodejs

I would like to modify time of pino log to format in a more readable way:

Maybe modifying this snippet of code:


var pinoCloudWatch = require('pino-cloudwatch');
var split = require('split2');
var pump = require('pump');

pump(process.stdin, split(), pinoCloudWatch({ group: 'test' }));

read the time from the log stream, format, and then send to pinoCloudWatch's stream.

MassimoCappellano commented 4 years ago

I've done in this way:

#!/usr/bin/env node

var pump = require('pump');
var split = require('split2');

const dateformat = require('dateformat');

const DATE_FORMAT = 'yyyy-mm-dd HH:MM:ss.l o';

var toFormatDate = function () {
    var reverse = new (require('stream').Transform)()

    reverse._transform = function (chunk, enc, callback) {
      var record = chunk.toString();

      if(record.startsWith('{')) {
        try {
          var obj = JSON.parse(record);
          var timestamp = obj.time;
          var instant = new Date(timestamp);
          var timestampFormatted = dateformat(instant, DATE_FORMAT);
          obj.time = timestampFormatted;
          record = JSON.stringify(obj);
        } catch(err) {
          console.log(err);
        }
      }

      reverse.push(record);
      callback()
    }

    return reverse
  }

  var pinoCloudWatch = require('pino-cloudwatch');
  var split = require('split2');
  var pump = require('pump');

  pump(process.stdin, split(), toFormatDate(), pinoCloudWatch({ group: 'TEST' }));