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

@rockpack/logger

v1.9.0

Published

This module can help you build error tracking & crash reporting system for your React application.

Downloads

215

Readme

@rockpack/logger

@rockpack/logger is a React component and logging system that allows you to record all actions before a critical error occurs so that this information can be analyzed later.

@rockpack/logger this module is part of the Rockpack project. See more details on the official site.

Articles

Using

  1. Installation:
# NPM
npm install @rockpack/logger --save

# YARN
yarn add @rockpack/logger
  1. For the logger system to work correctly, we need to wrap our application in a component
import React, { useCallback, useContext } from 'react';
import logger, { LoggerContainer, useLoggerApi } from '@rockpack/logger';

const App = () => {
  const { getStackData, triggerError } = useLoggerApi();
  ...
}

export default function () {
  const loggerCtx = useContext(LoggerContext);
  const showMessage = useCallback((level, message, important) => {
    if (important) {
      alert(message);
    }
  });

  return <LoggerContainer
    sessionID={window.sessionID}
    limit={75} // Stack length limit. On overflow, the first element will be removed
    getCurrentDate={() => {
      // Critical error date
      return dayjs()
        .format('YYYY-MM-DD HH:mm:ss');
    }}
    stdout={showMessage} // Display some errors as a tooltip for users
    onError={stackData => {
      // Send a stack of actions before the error to the backend (or otherwise process it)
      sendToServer(stack);
    }}
    onPrepareStack={stack => {
      // Allows you to add additional information to the stack
      stack.language = window.navigator.language;
    }}>
    <App/>
  </LoggerContainer>
}
  1. You need to add the logger to any of the methods you want to cover our logging system.

The @rockpack/logger module comes with a logger that is linked to

Suppose we have a component

import React, { useState } from 'react';

export default function Toggle(props) {
  const [toggleState, setToggleState] = useState('off');

  function toggle() {
    setToggleState(toggleState === 'off' ? 'on' : 'off');
  }

  return <div className={`switch ${toggleState}`} onClick={toggle}/>;
}

To properly log it, we need to modify the toggle method

import React, { useState } from 'react';
import logger from '@rockpack/logger';

export default function Toggle(props) {
  const [toggleState, setToggleState] = useState('off');

  function toggle() {
    let state = toggleState === 'off' ? 'on' : 'off';
    logger.info(`React.Toggle|Toggle component changed state ${state}`);
    setToggleState(state);
  }

  return <div className={`switch ${toggleState}`} onClick={toggle}/>;
}

We have added a logger in which the information is divided into 2 parts. React.Toggle shows us that this action happened at the level of React, the Toggle component, and then we have a verbal explanation of the action and the current state that came to this component.

If a critical error occurs in the system, we will have a BSOD with a detailed description of the user's actions. It will also be possible to send this stack to the error analysis system or ElasticSearch in order to quickly catch errors that occurred among our users.

- When logging applications, you need to put logs in the most confusing and complex parts of the code, so you will understand what happened at this stage.

- We can also use the “componentDidCatch” method, which was introduced in React 16, in case an error occurs.

Properties

  • <LoggerContainer /> properties:

| Prop | Type | Description | | --- | --- | --- | | active | Boolean[true] | Enable / disable logging. It is recommended to disable logging during the testing phase. | | bsodActive | Boolean[true] | Enable / disable BSOD output. It is recommended to disable for Production | | sessionID | Number | If you need to associate logging with Backend calls - a single session for Frontend and Backend will allow you to do this | | bsod | ReactElement[Component] | You can set your BSOD Component | | limit | Number[25] | Stack length limit. On overflow, the first element will be removed | | getCurrentDate | Function | Date format when an error occurred. Default - new Date().toLocaleString() | | onError | Function | window.onbeforeunload callback. In this callback, you can handle the stack or send it to the Backend | | onPrepareStack | Function | Allows you to add additional information to the stack before calling onError. For example, you can add the current localization of the application, the theme selected by the user, the name of the user who got the error, etc. | | stdout | Function | This method allows you to display the log method for the user (which was called with the second parameter "true"). For example, calling logger.error ('Ups ...', true) in the stdout method would output alert (message); |

  • logger methods:

The logger provided with @rockpack/logger has methods:

logger.log('log text here!');
logger.info('Some extra log information');
logger.warn('Warning! Warning!');
logger.debug('I\'m a debug message!');
logger.error('Ups...');

If we add true as the second parameter, the message passed to this log method will be passed to stdout . This will display some useful messages for the user, for example, in tooltip or alert.

The MIT License

MIT