logor
© 2016, Onur Yıldırım (@onury). MIT License.
Simple logging utility with levels, for Node.js.
This also brings some nice formatting, colors and JSON capabilities to your logs.
npm i logor
Usage
var log = ; log;
Output Example
Log Levels and Methods
Level | Method | Color | Details | |
---|---|---|---|---|
ERROR |
0 |
.error() |
Red | Error logs. Always output to the console. |
WARN |
1 |
.warn() |
Yellow | Warning logs. |
INFO |
2 |
.info() |
Cyan | Informational logs. |
.log() |
None | Regardless the config, output has no colors, date/time or level info. Clean text. | ||
.json() |
Cyan | Safely JSON stringifies the arguments. Handles circular references. | ||
.jsonf() |
Cyan | Same as .json() method, but with formatting. |
||
DEBUG |
3 |
.debug() |
Magenta | Debug logs. |
.trace() |
None | Prints the message to stderr with stack trace to the current position in the code. | ||
VERBOSE |
4 |
.verbose() |
Gray | Silly/verbose logs. |
Configuration
Pass an options
object to log.config()
method, with the following properties:
Option | Type | Default | Description |
---|---|---|---|
level |
Number |
4 |
Logging level. Levels are prioritized from 0 to 4 (highest to lowest). See Log Levels. |
colors |
Boolean |
true |
Whether colors should be enabled. |
format |
String|Function |
'{*}' |
Format of the log output. Takes placeholders: DATE , TIME , LEVEL and * . See Formatting Output.
|
Set all options at once:
log;
Set log level:
loglevel = 2;// When you set level to `2` (`INFO`),// levels with greater values will not output any logs.log;
Enable/disable all colors:
logcolors = false;log;// —> Log without colors
Formatting Output
{}
around a placeholder indicates that colors should be applied for that component. e.g. magenta if this is a debug log. Use ()
instead, to gray-out that component; or []
for no colors.
{color}
» (gray)
» [none]
Placeholder | Details |
---|---|
* |
Actual argument(s) being logged. If omitted, it will be appended at the end. |
DATE |
Short ISO date. |
TIME |
Time in HH:mm:ss format. |
LEVEL |
Name of the current logging level. |
Logs with date/time:
logformat = '{DATE} {TIME}\t{*}';log;// —> 2016-08-18 15:30:43 Log with full date/time
Logs with level info:
logformat = '{TIME}\t{LEVEL}\t{*}';log;// —> 15:30:43 INFO Log with time and level
logformat = '(TIME)\t{LEVEL}\t[*]';log;// —> 15:30:43 INFO This is the default color of the console.
JSON Logs
Use log.json()
to output a log with stringified argument(s).
Use log.jsonf()
to also format with indents.
Use log.s()
to stringify an individual or multiple values.
Use log.sf()
to also format with indents.
This also handles circular references.
log;log;log;log;
Change-Log
v1.0.1 (2016-09-05)
- Return self from
.config()
method.
v1.0.0 (2016-08-20)
- initial release.
License
MIT.