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

worker-logger

v1.4.0

Published

Creates dynamically a web worker logger when it gets instantiated

Downloads

15

Readme

worker-logger

This library reduce the CPU cost of logging web app. As everyone knows, javascript runs on a single event loop on the browser which gets interrupted by async calls such as setTimeout, setInterval, XHR, fetch and native events. So, delegating the work to a web worker mitigates the problem.

The worker-logger makes use of a synchronized queue which follows the FIFO principle. Then, by consequence, it only hijacks one connection from the pool for XHR, not impacting the app experience significatly.

Usage

Assuming that you want to use something like Rapid7 InsightOps to log and get derived metrics for your web app. It is likely to perform several api/xhr calls sending the logs. If they were running on the main logger, users would experience some sluggishness. Using the worker-logger is possible to migrate it due to the synchronized queue implemented and mentioned before.

Here follows an example for react/redux app.

Creating a token log

Go to InsightOps and create an account. After setup your password and access the main page. On the navigation bar, click on "Add Data" and go for the quick add. In the quick add slide-in panel, define your log as token and create it. This will give you back a token. Copy it, we are going to use it soon.

Defining you function and executing as worker

insightOpsLogger.js

export default function rapid7Logging(token) {
  const url = `https://webhook.logentries.com/noformat/logs/${token}`;
  global.onmessage = e =>
    fetch(url, {
      body: e.data,
      headers: { "Content-Type": "application/json" },
      method: "POST"
    });
}

logger.js

import WorkerLogger from "worker-logger";
import insightOpsLogger from "./insightOpsLogger";

const logger = new WorkerLogger({
  logger: insightOpsLogger,
  args: ["INSERT YOUR TOKEN HERE"]
});

export default logger;

store.js

import logger from "./logger";
// ...
export default createStore(
  reducers,
  applyMiddleware(logger.createMiddleware())
);
// ...

You can also time functions which will provide data for further performace analyses. E.g.

my-reducer.js

import logger from "./logger";

const setName = logger.time(function setName(state, action) {
  return { name: action.name };
});

const setAge = logger.time(function setAge(state, action) {
  return { name: action.name };
});

function reducePerson(state, action) {
  switch (action.type) {
    case "SET_NAME":
      setName(state, action);
      break;
    case "SET_AGE":
      setAge(state, action);
      break;
    default:
      return state;
      break;
  }
}

NOTE: You can also time sagas (generators) by using timeEffect.

import { call, put } from "redux-saga";
import actions from "./my-actions";

function* onSetPerson(action) {
  put(actions.setName(action.name));
  put(actions.setAge(action.age));
}

const timedSetPerson = logger.timeEffect(call, onSetPerson);

function* watchSetPerson() {
  yield* takeLatest(fixedConstants.SET_PERSON, timedSetPerson);
}

Analytic View

In the insight ops view, you are going to get roughly these data.

02 Apr 2018 22:51:30.916 { "instanceId": "999fa24f-2245-4257-97c7-9cf302b2e461", "level": "INFO", "event": { "track": "1856d410-7b0b-4049-9ab4-5bae3f730e7d", "data": { "type": "crypto:cryptoStock:CHANGE_SELECTED_CURRENCIES", "selectedCurrencies": [ "BRL" ] } } }

02 Apr 2018 22:51:30.974 { "instanceId": "999fa24f-2245-4257-97c7-9cf302b2e461", "level": "INFO", "event": { "track": "bc33aed5-a3fd-4b4d-8ebf-285076043f79", "data": { "type": "function", "name": "reduceChangeCurrencies", "ticksEllapsed": 0 } } }

02 Apr 2018 22:51:32.613 { "instanceId": "999fa24f-2245-4257-97c7-9cf302b2e461", "level": "INFO", "event": { "track": "0be30376-cb94-413d-b17e-bc3d46b70bcc", "data": { "type": "crypto:cryptoStock:CHANGE_SELECTED_CURRENCIES", "selectedCurrencies": [ "BRL" ] } } }

Test

npm test