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

eslint-plugin-forbid-log-strings

v1.0.0

Published

ESLint Plugin to forbid certain strings in logger statements

Downloads

71

Readme

eslint-plugin-forbid-log-strings 🖥️

Version

Forbid certain strings in logger statements.

Can be used with the built-in console - or with any custom logger.

Examples

Example #1 - simple case

You might configure the plugin to show an error if a console statement includes a hardcoded 'debug' string:

Plugin Configuration:

// .eslintrc.js

module.exports = {
  // ...
  plugins: [
    "forbid-log-strings",
    // ... maybe other plugins here ...
  ],
  rules: {
    "forbid-log-strings/check": [
      "error",
      [
        {
          forbiddenPatterns: ["/debug/i"], // <--- can be a string or a RegExp, both have to be defined as strings though (with quotes)
        },
      ],
    ],
    // ... maybe other rules here ...
  },
};

→ Result:

// some javascript or typescript file

console.log("some text..."); // <--- ✅ ok
console.log("debug", "some text..."); // <--- ❌ throws error
console.log("DEBUG", "some text..."); // <--- ❌ throws error
console.log("debug some text..."); // <--- ❌ throws error
console.log("DEBUG some text..."); // <--- ❌ throws error

Example #2 - use a custom logger + exclude log level 'error'

The following example uses cheese-log as a custom logger.

Plugin Configuration:

// .eslintrc.js

module.exports = {
  // ...
  plugins: [
    "forbid-log-strings",
    // ... maybe other plugins here ...
  ],
  rules: {
    "forbid-log-strings/check": [
      "error",
      [
        {
          forbiddenPatterns: ["/debug/i"],
          customLoggers: ["cheese"], // <--- a cheese-log method call would look like cheese.log("hello world!"), therefore we set "cheese" as the object name
          excludeLevels: ["error"],
        },
      ],
    ],
    // ... maybe other rules here ...
  },
};

→ Result:

// some javascript or typescript file

cheese.log("some text..."); // <--- ✅ ok
cheese.error("debug", "some text..."); // <--- ✅ also ok, because we exluded level 'error'
cheese.log("debug", "some text..."); // <--- ❌ throws error
console.log("debug", "some text..."); // <--- ✅ throws no error, since we are looking at the cheese-log logger, not console!

Installation

Via npm:

npm i eslint-plugin-forbid-log-strings --save-dev

Via yarn:

yarn add -D eslint-plugin-forbid-log-strings

Configuration Details

The plugin can be configured with 1 or more configurations.

Every configuration knows the following properties:

| property name | required | allowed values | | ----------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | forbiddenPatterns | yes | Array consisting of patterns in the shape of strings and/or Regular Expressions. If you want to provide a RegExp also provide it as a string (see examples above) - the plugin will automatically detect whether it is a RegExp or a plain string value. | | logic | Yes, if the length of forbiddenPatterns is >= 2. (Make sure to not provide this property if the length === 1, otherwise the plugin will throw a general error) | Must be one of "AND", "OR" and "XOR". It defines which logic is being used when multiple forbiddenPatterns are provided. | | customLoggers | no | Array of custom loggers. Make sure to provide the object name on which the logger methods are called. For example, for the cheese-log logger the statements would look like cheese.debug("hello world!"), etc. - therefore the provided value in the plugin configuration must be ["cheese"]. You can provide 1 or more custom loggers. | | excludedLevels | no | Array of log levels that should be ignored. For instance ["info", "warn", "debug"]. |

If you enjoy using this...