npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

billet

v1.0.2

Published

Library to simplify logging to any platform from your frontend

Downloads

4

Readme

Billet 📒

A seamless way to integrate a logging pipeline from your frontend to your backend. Currently supports Google Cloud Logging with Express.

The problem I'm trying to solve here

Whether you work at a small startup or a large company, there is going to be a need to store or access the logs from your client's device at some point or the other. It could be to debug an error or it could be to understand user behaviour via a trail of logs.

Either ways, you end up depending on exception catching software like Sentry that work well for, well, exception catching and everything related to that session. But what if what you're looking at is not an exception or if the exception is a known one and is handled? You lose out on all the useful logs for it unless you have logging setup.

To do so, you usually need to setup a backend ingestion service and a client SDK/lib to send over logs. This library solves the grunt work required to do both of those things.

Good to haves

A list of things that are not included in the package right now but will be picked up later:

  • Entropy based log filtering to hide sensitive data from logs.
  • Add more here if you can think of the tons of things that are missed.

Setting it up

First install the library to both your backend and frontend repositories.

Note: Make sure to always have the same version for billet on both your frontend and backend, Otherwise there can be mismatches in the structure of data expected and returned.

npm i billet

On the frontend

import billet from "billet/client";

billet.init({ endpoint: "", interceptNativeConsoleLogs: true });

Here:

  • endpoint is the URL you want to send your logs to, it could be an endpoint to a log ingestion pipeline or an endpoint on your backend server that is created using the backend SDK of Billet.
  • interceptNativeConsoleLogs is a boolean (By default: true) value that will determine whether you want to intercept logs from your frontend app producted by native console functions, namely: console.log, console.debug, console.warn and console.error.

Logging

If you've opted for intercepting native console functions, you don't have to do anything else, logs from your application will be automatically intercepted and sent to your backend server.

If you want to opt for a manual approach you can use:

billet.Logger.info("Info log");
billet.Logger.log("Normal Info log with", "varying", { types }, [of], data);
billet.Logger.error("Error log with: " + error.message);
billet.Logger.warn("Warning");
billet.Logger.debug("Just debugging, how y'all doing?");

User Identification

You can tag user properties to your logs for tracking:

billet.setUser({
	displayName: "user name",
	uid: "<uuid>",
	email: "[email protected]",
});

// If they log out
billet.setUser(null);

Additional property-based metadata

billet.setProperty("isSuperAdmin", true);
billet.setProperty("release-id", "1.0.5-beta");

These properties will be sent along with your logs to the server.

On the backend

GCP + Express

I'm assuming you have a basic understanding of what Google Cloud Logging is and what Google Cloud Service Accounts are. If not you can refer to the following:

And once you have your Express server setup, you can power it up to accept logs from your frontend with the following snippet:

app.use(
	"/log", // Any route you want.
	loggingControllerGenerator({
		serviceAccount: JSON.parse(process.env.YOUR_GCP_LOGGING_SERVICE_ACCOUNT),
		logName:
			"a name to group your logs under (for example: frontend-logs-stream)",
		serviceName: "a name to attach with the logs (for example: frontend)",
		onError: (error) => {
			// Do something with the error
		},
	})
);

The @google-cloud/logging library is used with support for automatic retries on failures and log write batching to ensure consistent delivery.

Once done, simply host your server and add its endpoint to the frontend.

Contributions

Features, bug fixes and contributions are welcome. ☀

I have only added support for GCP logging so far but the number of services available out there are uncountable, so if you have a specific requirement, feel free to add it in.

Simply create an issue for support or fork the repo and raise a pull request for the changes you want to add to the library.