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

@wisual/logger

v1.2.3

Published

This is a logging module for the front-end. At the moment it only implements simple creation of contextual and hierarchical loggers that carry over data through React.js context. It also implements some basic pretty-printing.

Downloads

4

Readme

@wisual/logger

This is a logging module for the front-end. At the moment it only implements simple creation of contextual and hierarchical loggers that carry over data through React.js context. It also implements some basic pretty-printing.

Basic usage

A logger can be created with LoggerFactory. It'll be cached by name & thus getting the logger is very cheap.

import { LoggerFactory } from "@wisual/logger";

const logger = LoggerFactory.getLogger("MyLogger");

logger.info("Hello world");
logger.debug("segfault");
logger.warn("Coulnd't find X");
logger.error("Oh no");

Structured data

You may pass additional data as a parameter & this will be available for the sinks to output in a structured format:

logger.info("Created user", { user, sessionId, tabId });

Hierarchical logger

Loggers are hierarchical. When creating a child, you may set extra context data on to it. Every child will inherits it's parents contexes.

import { LoggerFactory } from "@wisual/logger";

const logger = LoggerFactory.getLogger("root");

const usersServiceLogger = logger.child('UsersService', { dbBackend: 'psql', flushTimeout: 1000 });
const usersCacheLogger = usersServiceLogger.child('UsersCache', { cacheBackend: 'redis' });

usersCacheLogger.info('Cache hits in last 10 seconds', { cacheHits: 10 });
// Data available in the log:
// {
//   logger: "root>UsersService>UsersCache",
//   message: "Cache hits in last 10 seconds",
//   time: "01-03-2021T...",
//   variables: { cacheHits: 10 },
//   context: {
//     dbBackend: 'psql',
//     flushTimeout: 1000,
//     cacheBackend: 'redis',
//   }
// }

React integration

There are hooks provided for react integration.

import React from "react";
import { useLogger } from "@wisual/logger";

function MyComponent({ userId, data }: { userId: string; data: any }) {
  // Creating our logger and properly setting its context
  const logger = useLogger("MyComponent", { userId });

  // Will log on render with proper context / names
  logger.info("rendering");

  // This will use effect to log & log everytime the arguments change
  logger.onInfo("Data has changed", { data });

  // This is wrapping the children with this logger's context provider
  return logger.wrap(<div />);
}

The hierarchy of loggers will be created based on the React tree. A logger may wrap a sub-tree with wrapWithLogger:

function Router({ routeName }) {
    const routerLogger = useLogger("Router", { routeName });
    // ...
    return wrapWithLogger(
        routerLogger,
        <div>...</div>
    );
}

All calls to useLogger within the wrapped sub-tree will inherit the parent context.

Logger sinks

Sinks are implementors of the LoggerSink interface.

Tree sinks are provided:

  • PrettyConsoleSink - Pretty print messages on Node.js
  • PrettyBrowserSink - Pretty print messages on browser consoles, which have support for CSS
  • DelegatingSink - Send messages to multiple sinks

There's no filtering currently implemented.

In addition, you'll find a tauri sink which provides rust/js logging functionality in the crates/plugin-host-gui package.

Configuring the sink

Use LoggerFactory.setSink.