A logger for Rear projects.
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);
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 |
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 |
Create a new logger with given name
and options.
- name
- The logger name. When
showName
property is set totrue
, the name is prefixed to the logged message. - props
- Custom logger properties.
Print a message
directly to the stdout much like a standard console.log
would do.
- message
- Message to be logged
- args
- Additional arguments
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')
- level
- The level name as defined in the levels options.
- message
- Message to be logged.
- args
- Additional arguments.
Format and print a warning message. When an assert
is provided, the message
is conditionally printed based on the truthyness of the assertion.
- 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.
Format and print an Error's message or a given message
to the stderr.
- message
- A message to be logged or an Error object.
- args
- Additional arguments.
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.
- message
- Message to be logged
- args
- Additional arguments
Format and print the given message
as highlighted. An
highlighted message is bold and do not print time information.
- message
- A message to be logged.
- args
- Additional arguments.
Async prompt user for input in the console and resolve with the user answer.
- opts
- Optional read options
- message
- The question to be prompted
- args
- Style format arguments
Same as prompt
, but prefix a question
level header to the message.
- opts
- Optional read options
- message
- The question to be prompted
- args
- Style format arguments
- Promise
- Resolve with the user answer or reject
Clear the console
Clear the current line.
Move the cursor up for the given number of lines.
- lines
- Number of lines to be rewritten.
- clear
- Define if the lines being rewritten must be cleared. Default to true.
Hide the cursor in the console.
Restore cursor visibility in the console.