0.5.0 • Published 5 years ago
schlog-dev v0.5.0
Schlog
Simple Node.js logger
Installation
Install using yarn:
yarn add schlog
Or npm:
npm install schlog
Basic usage
const logger = require("schlog")
logger.setLogLevel(logger.getLogLevelByName("warn"))
// or set LOG_LEVEL environment variable to any of the default levels
// (by name or number)
// defaults to info (2)
logger.warn("Test")
// [21:52:58] WARN Test
logger.info("Test")
// nothing (since level is set to warn)
Default log levels: error
, warn
, info
, debug
or 0-4
error
and warn
will output to stderr while info
and debug
will output to stdout.
Log functions return the same thing they output to the console. Either the log line or nothing if the log level is set higher than the level called.
Example customization
const chalk = require("chalk")
const logger = require("schlog")
const LogLevel = require("schlog").LogLevel
const LogScope = require("schlog").LogScope
const logLevel = new LogLevel("fancy", chalk.magenta.underline, 4, LogScope.STDOUT)
logger.setLogLevel(logLevel)
// will only output with levels priorities 4 or lower
logger.setTimeFormat("MMMM Do YYYY, h:mm:ss a")
logger.log(logLevel, "Test")
// [March 21st 2019, 11:53:11 pm] FANCY Test
logger.setPrintTimestamps(false)
logger.log(logLevel, "Test")
// FANCY Test
logger.setPrintJson(true)
logger.log(logLevel, "Test")
// {"time":"21:52:58","level":{"name":"fancy","priority":4,"scope":"stdout"},"message":"Test"}