1.0.17 • Published 7 years ago

babel-plugin-js-logger v1.0.17

Weekly downloads
2,735
License
MIT
Repository
github
Last release
7 years ago

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';
...

2) 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.

OptionValuesDefaultDescription
variableValid JS identifier"logger"Name of the logger variable
moduleValid NodeJS module name"js-logger"Name of the logger module
factoryValid 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.

OptionValuesDescription
projecttrueInclude the project name provided in the package.json
projectfalseDo not include the project name
level0Use the full path starting from the project root (= directory where package.json was found)
level< 0Use 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> 0Use the last n levels of the path
separatore.g. :Use : as a namespace separator
extensionse.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.

1.0.17

7 years ago

1.0.16

7 years ago

1.0.15

7 years ago

1.0.14

7 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago