datalust / pino-seq

A stream to send Pino events to Seq
Apache License 2.0
11 stars 10 forks source link

Support for sqelf #8

Closed andymac4182 closed 2 years ago

andymac4182 commented 4 years ago

Hi,

I am looking at setting this up for an application and using gelf as a way to capture all logs from the container and capture the pino logs along with them. Using pino by default it logged a blank message template with a property of msg and the rest of the properties from the log line as properties of the log message.

After copying PinoSeqStream into my app and updating it to write to console and use the CLEF property names matching https://github.com/datalust/sqelf/blob/6b9682dda3735294057f654e0ca6ddfafc480db7/sqelf/src/process/clef.rs#L28-L57 I started to get all the right logs and properties.

Should we look at a writable as part of this library.

Unfortunately some messages are written to console by other libraries that I don't have control over in json format that don't get read correctly.

image

This might be a potentially better fix to offer configuration or a more lax set of options in https://github.com/datalust/sqelf

class PinoSeqStream extends stream.Writable {
  constructor() {
    super();
  }

  _write(message, _enc, cb) {
    if (message) {
      try {
        const eventCopy = JSON.parse(message);

        const { time, level, msg, err, error, stack, ...props } = eventCopy;

        // Get the properties from the error
        const { message: errMessage, stack: errStack, ...errorProps } = err || error || {};

        const forSeq = {
          "@t": new Date(time),
          "@l": LEVEL_NAMES[level],
          "@mt": msg || errMessage,
          "@x": stack || errStack,
          ...errorProps,
          ...props
        };

        try {
          console.log(JSON.stringify(forSeq));
        } catch (errorMessage) {
          console.error(errorMessage);
        }
      } catch (err) {
        const msg = String(message);
        console.error(msg);
      }
    }
    cb();
  }
}
nblumhardt commented 4 years ago

Thanks @andymac4182! That's interesting - a pino stream that writes CLEF to the console would open up some deployment options 🤔

It would also be interesting to look at field mappings in the GELF input, and that solution would probably have a few more uses... Will give it some thought!

nblumhardt commented 2 years ago

This should now be resolved by https://github.com/datalust/sqelf/pull/104

andymac4182 commented 2 years ago

:D woo hoo! I think I remember why I logged this 😛