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

babel-plugin-js-logger

v1.0.17

Published

Babel plugin to enable js-logger in your entire project efficiently.

Downloads

10,547

Readme

babel-plugin-js-logger

Babel plugin to enable js-logger in your entire project efficiently.

npm downloads total npm version npm license

This Babel plugin enables js-logger in your entire project by placing const logger = require('js-logger').get('project:example:path'); in front of every transpiled JavaScript file. Works with Babel standalone, e.g. within a backend, or together with Webpack, e.g. within a frontend.

Have a look at our logger-example project. It is a self-contained showcase on how to use js-logger within your backend and frontend code efficiently.

Installation

Install js-logger and the plugin module via

npm install js-logger --save
npm install babel-plugin-js-logger --save-dev

or

yarn add js-logger
yarn add babel-plugin-js-logger --dev

Configuration

Enable Plugin

Add babel-plugin-js-logger to your .babelrc, e.g. like this:

{
  "presets": ["es2015", "stage-0", "react"],
  "plugins": ["transform-runtime", "transform-decorators-legacy", "js-logger"]
}

Initialize Logger

Initialize js-logger in your main routine with require('js-logger').useDefaults();. See js-logger/readme for more.

Be aware:

In case you use import instead of require to import your modules, you will have to outsource the initialization routine into a separate file which has to be imported at the very first. Reason: import statements are moved to the top of every file automatically since this is required by the standard and Babel complies to it. Otherwise, you will run your code to initialize js-logger a bit later in the process than you truly intend to do and you might miss a few logging messages.

I use this pattern in my projects:

  1. main.js: Import your logger-init.js at the very first in your main entry point.
import './logger-init.js';
import ExampleModule from './example-module.js';
...
  1. logger-init.js: Run your initialization code for js-logger here.
require('js-logger').useDefaults();

Options

The plugin provides the following options to tweak the behavior of the plugin during code generation.

| Option | Values | Default | Description | | :--- | :--- | :--- | :--- | | variable | Valid JS identifier | "logger" | Name of the logger variable | | module | Valid NodeJS module name | "js-logger" | Name of the logger module | | factory | Valid JS identifier | "get" | Name of the logger factory method (called on the module) | | format | { project: Boolean, level: Integer, separator: String, extensions: Array<String> } | { project: true, level: -1, separator: ':', extensions: [ '.js', '.jsx' ] } | Parametrizes the logger name to be generated (see below) |

Logger name formatting

The format options parametrize the logger name to be generated.

| Option | Values | Description | | :--- | :--- | :--- | | project | true | Include the project name provided in the package.json | | project | false | Do not include the project name | | level | 0 | Use the full path starting from the project root (= directory where package.json was found) | | level | < 0 | Use the full path starting from the nth level below the project root (very useful to omit the name of the src directory used in projects) | | level | > 0 | Use the last n levels of the path | | separator | e.g. : | Use : as a namespace separator | | extensions | e.g. [ '.js', '.jsx' ] | Strip .js and .jsx extensions from path |

Configuration Example

A .babelrc configuration example which uses dots as separators and omits the project name.

{
  "presets": [ "es2015", "stage-0", "react" ],
  "plugins": [ "transform-runtime", "transform-decorators-legacy",
    [ "js-logger", { "format": { "separator": ".", "project": false } } ]
  ]
}

Usage

Once you configured the plugin, you can use the logger in every JS file easily via the logger variable. See js-logger/readme for more.

Example

The file my-project/src/some/sub/module.js

logger.debug("some debug message");

would produce the output

[my-project:some:sub:module] some debug message

if used with default configuration and a my-project/package.json providing the project name my-project.