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

@logfire-sh/core

v1.0.3

Published

logfire.ai - logging core

Downloads

13

Readme

logfire JavaScript client(https://logfire.ai) - Logging core library

ISC License

Looking for a logging solution?
Check out logfire and [logfire clients for JavaScript and Node.js](DOC URL).

@logfire-sh/core

This is an NPM package that provides core logging functionality.

It's used by the Node and browser logging packages.

You typically wouldn't require this package directly, unless you're building a custom logger.

The Base class

The Base class provides core features that are extended by loggers.

For example - you could create a custom logger that implements its own sync method, for getting data over to logfire.ai

import { Base } from '@logfire-sh/core';
import { IlogfireOptions, IlogfireLog } from '@logfire-sh/types';

class CustomLogger extends Base {
  // Constructor must take a logfire.ai source token, and (optional) options
  public constructor(sourceToken: string, options?: Partial<IlogfireOptions>) {
    // Make sure you pass the source token to the parent constructor!
    super(sourceToken, options);

    // Create a custom sync method
    this.setSync(async (logs: IlogfireLog[]) => {
      // Sync the `log` somehow ... `this._sourceToken` contains your logfire source token

      // ....

      // Finally, return the log... which will resolve our initial `.log()` call
      return logs;
    });
  }
}

Logging

Logging to logfire is simple - just call the .log() function with a string message:

// Simple log message (defaults to the 'info' log level)
logfire.log('Hello logfire!');

// Or, add custom context keys to pass along with the log
logfire.log('Once more, with context', {
  httpRequest: {
    'user-agent': {
      browser: 'Chrome',
    },
  },
});

There are four levels of logging, each corresponding to a function:

| Function | Log level | Example | | -------- | ---------------------------------------------------------------------------------------------------------- | ------------------------------------------------------ | | .debug() | Debug - logs to report/diagnose system events | Currently logged in session object, during development | | .info() | Info - data/events where no action is required; for information only | User successfully logged in | | .warn() | Warning - advisory messages that something probably needs fixing, but not serious enough to cause an error | SQL query ran slower than expected | | .error() | Error - something went wrong | Couldn't connect to database |

By default, .log() logs at the 'info' level. You can use the above explicit log levels instead by calling the relevant function with your log message.

All log levels return a Promise that will resolve once the log has been synced with logfire.ai:

// Will resolve when synced with logfire.ai (or reject if there's an error)
logfire.log('some log message').then((log) => {
  // `log` is the transformed log, after going through middleware
});

Middleware

You can add your own middleware functions, which act as transforms on the IlogfireLog log object.

This is useful for augmenting the log prior to syncing with logfire, or even pushing the log to another service.

Here's what a middleware function looks like:

import { IlogfireLog } from '@logfire-sh/types';

// In this example function, we'll add custom 'context' meta to the log
// representing the currently logged in user.
//
// Note: a middleware function is any function that takes an `IlogfireLog`
// and returns a `Promise<IlogfireLog>`
async function addCurrentUser(log: IlogfireLog): Promise<IlogfireLog> {
  return {
    ...log, // <-- copy the existing log
    user: {
      // ... and add our own `context` data
      id: 1000,
      name: 'John',
    },
  };
}

Then just attach to the logfire instance with .use:

logfire.use(addCurrentUser);

You can add any number of pipeline functions to your logger instance, and they'll run in order.

Middleware functions run before the final sync to logfire.ai. Pipeline functions should return a Promise<IlogfireLog>, making it possible to augment logs with asynchronous data from external sources.

Note: If an exception is thrown anywhere in the pipeline chain, the log won't be synced. Wrap an async try/catch block around your call to .log|info|debug|warn|error() or tack on a .catch() to ensure your errors are handled.

Removing middleware

If you wish to remove middleware, pass in the original middleware function to .remove():

// `addCurrentUser` will no longer be used to transform logs
logfire.remove(addCurrentUser);

This will remove the middleware function from all future calls to .log|info|debug|warn|error().

To re-add middleware, pass it to .use()

LICENSE

ISC