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

fs-error-logger

v1.0.9

Published

Writes errors into files in both JSON and XML.

Downloads

21

Readme

NPM

Build StatuscodecovDependency StatusCode ClimateKnown VulnerabilitiesInline docscontributions welcome

What

fs-error-logger is a small module that allows you to write JSON and XML files for errors. It was written for nodejs by using its fs library, but if you want you can inject a different dependency in it and use it instead.

Why

Most people, when catching an error, use console.log to display it and they're done. This however, is commonly seen as a bad practice because:

  1. When having several errors, you will get your console spammed until you eventually loose track of errors or are too spammed to see them.
  2. It is seen as a good practice to remove console.log calls from production code.
  3. console.log calls will fail in some environments, forcing you to use dummy stubs instead.

Instead, of using console.log you should:

  1. Actually try to fix the error
  2. If that is not possible, log it or show it in a consistent, time persistent way.

fs-error-logger was made to fulfill the second need. By saving your logs to disk, you are able to later retrieve and evaluate them. Furthermore, you also have access to the logs in two formats: JSON and XML - both human readable.

How

Every time you log an error usingfs-error-logger you decide where to write it, in what format and its ID. This means that your logs will always be unique and that you wont lose a single one.

If you don't want to do any setup, you can use the default options which will server you just as well.

If you have any questions you can ask in the issues page:

Feel free to check the fs-error-logger project page for additional information as well.

Install

npm install fs-error-logger --save

API

  • logXML
  • logJSON
  • setOutputFolder
  • getOutputFolder
  • setIdFn
  • getIdFn

Examples

Files created by the logJSON and logXML functions have the following format:

  • ErrorType_ID.Format

So for example, an error of type Error using the default Date.now function as an ID would produce the file Error_12231413.json or Error_12231413.xml, depending on the format.

An error of type TypeError would generate TypeError_12231413.json and so on...

The first part of the file is the error.name property, while the second is an unique id returned by the idFn which you can parametrize. Following are examples on how you can create logs.

Create a logger and log an error in JSON:

const loggerFactory = require( "fs-error-logger" );
const logger = loggerFactory();

//creates a file named 'Error_12512451.json' in the current folder
logger.logJSON( new Error() )
    .then( () => console.log("File written successfully!") )
    .catch( console.log );

Create a logger that logs an error in XML:

const loggerFactory = require( "fs-error-logger" );
const logger = loggerFactory();

//creates a file named 'Error_12512451.xml' in the current folder
logger.logXML( new Error() )
    .then( () => console.log("File written successfully!") )
    .catch( console.log );

Create a logger that writes to the 'errors/' folder:

const loggerFactory = require( "fs-error-logger" );
const logger = loggerFactory({outputFolder: "./errors/"});

//creates a file named 'Error_12512451.json' in the current folder
logger.logJSON( new Error() )
    .then( () => console.log("File written successfully!") )
    .catch( console.log );

Create a logger that uses as uuidv1 as file ID:

const loggerFactory = require( "fs-error-logger" );
const uuidv1 = require("uuid/v1");

const logger = loggerFactory({idFn: uuidv1});

//creates a file named 'Error_12512451.json' in the current folder
logger.logJSON( new Error() )
    .then( () => console.log("File written successfully!") )
    .catch( console.log );

Create a logger with default options and then change the output folder:

const loggerFactory = require( "fs-error-logger" );
const logger = loggerFactory();

logger.setOutputFolder("./errors/");

//creates a file named 'Error_12512451.json' in the "./errors/" folder
logger.logJSON( new Error() )
    .then( () => console.log("File written successfully!") )
    .catch( console.log );