Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transport Writeable Stream sending log data #1118

Closed
AlexanderKaran opened this issue Sep 6, 2021 · 5 comments · Fixed by #1174
Closed

Transport Writeable Stream sending log data #1118

AlexanderKaran opened this issue Sep 6, 2021 · 5 comments · Fixed by #1174

Comments

@AlexanderKaran
Copy link

AlexanderKaran commented Sep 6, 2021

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
Copy link
Member

mcollina commented Sep 6, 2021

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
Copy link
Author

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
Copy link
Member

mcollina commented Sep 7, 2021

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

@simoneb
Copy link
Contributor

simoneb commented Oct 19, 2021

Addressed in #1174

@github-actions
Copy link

github-actions bot commented Feb 2, 2022

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants