pinojs / pino

🌲 super fast, all natural json logger
http://getpino.io
MIT License
14.21k stars 875 forks source link

Transport Writeable Stream sending log data #1118

Closed AlexanderKaran closed 3 years ago

AlexanderKaran commented 3 years ago

Hi Everyone

Brand new to pino and been creating a transport to send data to Cloudwatch-logs. Doing this as the old package has not been updated for a while and is now legacy.

The code works fine, I guess I am asking if what I am doing is correct. I have created a transport that inside its writeable stream formats the data for Cloudwatch and then sends it on its way. Is there a better way?

Here is the code. createClient() just returns the AWS connection.

const stream = async (options) => {
    const client = createClient();
    let token = undefined;
    const myTransportStream = new Writable({
        async write(chunk, enc, cb) {
            const formatted = chunk
                .toString()
                .replace(/\r?\n|\r/g, "")
                .replace(/}/g, "},");
            const data = JSON.parse(
                `[${formatted.substring(0, formatted.length - 1)}]`,
            );
            const command = new PutLogEventsCommand({
                logGroupName: "api",
                logStreamName: `executive-${Config.env}-${Config.location}`,
                logEvents: data.map((log) => ({
                    message: log.msg,
                    timestamp: log.time,
                })),
                sequenceToken: token,
            });
            const response = await client.send(command);
            token = response.nextSequenceToken;
            cb();
        },
    });
    return myTransportStream;
};
mcollina commented 3 years ago

I would recommend to create a new package.

I would recommend to not use a Writable but rather use async iterators, https://github.com/pinojs/pino-abstract-transport, and https://github.com/mcollina/hwp.

AlexanderKaran commented 3 years ago

Thank you, will give it a crack this week.

Would be great to point to these to libraries in the docs in the transport section.

mcollina commented 3 years ago

I've tagged this as a good first issue for documentation.

simoneb commented 3 years ago

Addressed in https://github.com/pinojs/pino/pull/1174

github-actions[bot] commented 2 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.