mk-simple-logger v0.1.11

MK Simple Logger 
This a small package for node that allow implement a logger, and add same config options, like theming or set custom format.
ℹ️ℹ️ Since versión 0.1.11 the output is formatted for logger's name and log's level
Install
The package is available in npm repositories. npm install
npm install mk-simple-loggeryarn install
yarn add mk-simple-loggerUsage
The main class is SimpleLogger so you need to import from the module
const SimpleLogger = require("mk-simple-logger").SimpleLogger;
// or
const { SimpleLogger } = require('mk-simple-logger');
let logger = new SimpleLogger("mylogger");
logger.info("My Message");
// -> 20/02/2021 @ 20:10:40 - [ info ] - mylogger - My message
// allow string foramtting
logger.critical("My {p} message", { p: "custom" });
// -> 20/02/2021 @ 20:10:40 - [ CRITICAL ] - mylogger - My custom messageMethods
logger.debug("");
logger.info("");
logger.log("");
logger.warn("");
logger.error("");
logger.critical("");Set the log level
SimpleLogger.setLogLevel("debug"); // 'warn'| 'info' | 'log' | 'error' |
'critical'Set custom format
The class allow change the date and the log line format:
SimpleLogger.setFormat("{name} ==> {msg}");
// on log: mylogger ==> My message
SimpleLogger.setDateFormat("{y}/{month}/{day}");
// on write date: 21/03/14Available log fields:
{name} - logger name
{level} - loging level
{date} - log date
{msg} - log messageAvailable date format:
{day} - current day
{weekDay} - day of the week
{month} - the month
{year} - the full year
{y} - the short year
{hour} - the hour
{min} - the minuts
{sec} - the seconds
{mil} - the milisecondsEnable the file logger
SimpleLogger class can manger a file log too, this option need to set
the static option isFile as true and set the logFile:
SimpleLogger.enableFileLog(); // enable file log
SimpleLogger.setFileLog("myapp.log"); //setting the file log
// if you want you can disable the stdout with:
SimpleLogger.disableStdout();Logger can work with two options at the same time, they'r not restrictive.
Load configuration from the env
Simple logger can load same data from the enviroment if it's set. Can load:
LEVELaslogLevelLOG_FILEfor set the log's fileERROR_FILEfor set the errors' file- If
NODE_ENVis set read it and set the log level from this env variable
Global usage
It's posible to use the logger globally, for this prupose from version 0.1.4
has the static method global() that return an static instance of the logger:
const {SimpleLogger} = require('mk-simple-logger');
SimpleLogger.global().log('message'); // can access to all log method
SimpleLogger.global().setName('name'); // can set the girglobal logger nameAll methods all available for the glogal logger.
Express integration
Sice version 0.1.8 can use built logger for express applications:
function logger(?name: string, ?level: string, ?format: string): ExpressMiddlewar⚠️❗ Since 0.1.9 you can set a custom format
Usage in express
const express = require('express');
const app = express();
const {SimpleLogger, logger} = require('mk-simple-logger');
app.use(express.json());
...
app.use(logger("SERVER", "info", "{method} {url} {status} {time}ms"));
...
app.listen(8080);On request:
GET /uri1
logger:
18-02-22 @ 19:43:50 [ INFO ] -> SERVER -> GET /uri1 300 3msThe logger work like the main logger so you can enable the file logger.
Format
Since 0.1.9
The format param let you customize the message body, in this case, like the logger format, we have a prefixed mapped data:
| FIELD | Description |
|---|---|
| {time} | Request time |
| {method} | Request method |
| {url} | Request url |
| {status} | Response status code |
You can play with this paramaters to generate a custom log mesage format.