1.0.0 • Published 6 years ago

rear-logger v1.0.0

Weekly downloads
12
License
MIT
Repository
github
Last release
6 years ago

rear-logger

Build Status

A logger for Rear projects.

How it works

Create a logger with name and options, then start logging on the predefined levels: success, info, warn, debug and error.

Level names can be added or customized by providing a key/value map with level name and color string in the levels option. You can also specify both terminal and browser colors by providing an Array with both values as shown below.

const createLogger = require('rear-logger');

const name = 'MyAwesomeLogger';
const options = {
  showName: true,
  showDiffLabel: true,
  levels: {
    hint: ['cyan', 'color: cyan'] // or just 'cyan'
  }
};

const logger = createLogger(name, options);
logger.hint('Logger "%s" created with options: %O', name, options);

Default Levels

NameColor
logwhite
nonewhite
debugmagenta
infoblue
successgreen
hintyellow
warnyellow
warningyellow
errorred
quitred
GETbold_green
POSTbold_yellow
PUTbold_blue
DELETEbold_red
OPTIONSbold_cyan

Logger Options

NameTypeDefaultDescription
enabledbooltrueEnable or disable the logger output
showNameboolfalsePrefix logger's name to the logged output
nameColorArrayDefine logger's name color
showLevelNamebooltruePrint the log level in logged output
showTimeLabelboolfalsePrint the current time in the logged output
showDiffLabelboolfalsePrint the diff time from last logged messaged
formatters?object{}Accept additional text formatters
codeMapobjectDefine additional code map (i.e. emoji-codes)
disableCodeMapboolfalseDefine code-map conversion (i.e. emoji-codes)
levels?object{}Define key/value level name and color pairs
stdout?functionCustom stdout
stderr?functionCustom stderr

API

constructor (name: string, props: RearLoggerProps)

Create a new logger with given name and options.

Parameters

raw (message: string, ...args: Array): void

Print a message directly to the stdout much like a standard console.log would do.

Parameters

message (level: string, message: string, ...args: Array): void

Format and print a message for the given level. Note: usually is more convenient to call the logger's level function as in logger.info('Hello world')

Parameters

warn (assert?: boolean, message: string, ...args: Array): void

Format and print a warning message. When an assert is provided, the message is conditionally printed based on the truthyness of the assertion.

Parameters

error (message: string|Error, ...args: Array): void

Format and print an Error's message or a given message to the stderr.

Parameters

debug (message: string, ...args: Array): void

Format and print a debug message. The DEBUG environment variable is used to show or hide this message based on space or comma-delimited names.

The character may be used as a wildcard. For example: `DEBUG=myLogger:`

For example:

export DEBUG="firstLogger:*"
const firstLogger = require('logger')('firstLogger:section');
const secondLogger = require('logger')('secondLogger:section');

firstLogger.debug('This message will be printed to stdout');
secondLogger.debug('This message will NOT be printed to stdout');

Note: Set the DEBUG variable in the localStorage if you are using the library from a browser.

Parameters

highlight (message: string, ...args: Array): void

Format and print the given message as highlighted. An highlighted message is bold and do not print time information.

Parameters

async prompt (opts?: Object, message: string, ...args: Array): Promise

Async prompt user for input in the console and resolve with the user answer.

Parameters

async question (opts?: Object, message: string, ...args: Array): Promise

Same as prompt, but prefix a question level header to the message.

Parameters

Returns

clear (): void

Clear the console

clearLine (): void

Clear the current line.

rewriteLine (lines: number, clear?: boolean): void

Move the cursor up for the given number of lines.

Parameters

hideCursor (): void

Hide the cursor in the console.

showCursor (): void

Restore cursor visibility in the console.