rear-logger v1.0.0
rear-logger
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
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.
7 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago