Skip to content

rearjs/rear-logger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Name Color
log white
none white
debug magenta
info blue
success green
hint yellow
warn yellow
warning yellow
error red
quit red
GET bold_green
POST bold_yellow
PUT bold_blue
DELETE bold_red
OPTIONS bold_cyan

Logger Options

Name Type Default Description
enabled [bool] true Enable or disable the logger output
showName [bool] false Prefix logger's name to the logged output
nameColor [Array] Define logger's name color
showLevelName [bool] true Print the log level in logged output
showTimeLabel [bool] false Print the current time in the logged output
showDiffLabel [bool] false Print the diff time from last logged messaged
formatters ?object {} Accept additional text formatters
codeMap [object] Define additional code map (i.e. emoji-codes)
disableCodeMap [bool] false Define code-map conversion (i.e. emoji-codes)
levels ?object {} Define key/value level name and color pairs
stdout ?function Custom stdout
stderr ?function Custom stderr

API

constructor (name: string, props: RearLoggerProps)

Create a new logger with given name and options.

Parameters

name
The logger name. When showName property is set to true, the name is prefixed to the logged message.
props
Custom logger properties.

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

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

Parameters

message
Message to be logged
args
Additional arguments

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

level
The level name as defined in the levels options.
message
Message to be logged.
args
Additional arguments.

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

assert
Assertion to be test for truthyness.
message
A warning message to be logged. If an assertion is specified, the message is printed only when the assertion pass.
args
Additional arguments.

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

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

Parameters

message
A message to be logged or an Error object.
args
Additional arguments.

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

message
Message to be logged
args
Additional arguments

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

message
A message to be logged.
args
Additional arguments.

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

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

Parameters

opts
Optional read options
message
The question to be prompted
args
Style format arguments

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

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

Parameters

opts
Optional read options
message
The question to be prompted
args
Style format arguments

Returns

Promise
Resolve with the user answer or reject

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

lines
Number of lines to be rewritten.
clear
Define if the lines being rewritten must be cleared. Default to true.

hideCursor (): void

Hide the cursor in the console.

showCursor (): void

Restore cursor visibility in the console.