@gabrieljoelc/rappy-log

2.0.2 • Public • Published

Rappy Log

Wraps console log methods so you can set the current log level to only log what you want in the current environment. This is useful for debugging in something like React Native.

Getting started

Add it to your app:

npm install rappy-log

Usage:

import { getCurrentLogLevel, setLogLevel } from 'rappy-log';

const levels = [
  'none',
  'error',
  'info',
  'debug',
  'trace',
];

for (let logCount = 0; logCount < levels.length; logCount++) {
  let msg = `should log ${logCount} time${logCount > 1 ? 's' : ''}`;
  setLogLevel(levels[logCount]);
  console.log('current level:', getCurrentLogLevel());
  console.error(msg);
  console.info(msg);
  console.debug(msg);
  console.trace(msg);
}

Outputs:

current level: none
current level: error
ERROR - should log 1 time
current level: info
INFO - should log 2 times
INFO - should log 2 times
current level: debug
DEBUG - should log 3 times
DEBUG - should log 3 times
DEBUG - should log 3 times
current level: trace
TRACE - should log 4 times
TRACE - should log 4 times
TRACE - should log 4 times
TRACE - should log 4 times

Environment variable

You can also set the log level with an environment variable (see (12-factor)[https://12factor.net/config] for more information).

If you had this app:

# app.js
import { setLogLevel } from 'rappy-log';

setLogLevel(); // if no level parameter is passed, tries to get level from environment

// dope freestyling starts to drop
console.error('And money on the wood');
console.debug('We made a promise and it\'s understood');

Now start the app:

LOG_LEVEL=error node app.js
> And money on the wood

Readme

Keywords

Package Sidebar

Install

npm i @gabrieljoelc/rappy-log

Weekly Downloads

3

Version

2.0.2

License

MIT

Unpacked Size

11.2 kB

Total Files

8

Last publish

Collaborators

  • gabrieljoelc