2.4.4 • Published 11 months ago

nlogs v2.4.4

Weekly downloads
5
License
MIT
Repository
github
Last release
11 months ago

nlogs

new custom logger for Node.js

Install

npm i nlogs

or:

yarn add nlogs

Use

const logger = require('nlogs')(module, {/* config */})
const Logger = require('nlogs')
const { NLogger }  = require('nlogs')

const logger1 = Logger(/* filename */, {/* config */})
const logger2 = new NLogger(/* filename */, {/* config */})

Logger methods

Most methods repeat console methods

logger.log('text')
logger.info('text')
logger.debug('text')
logger.warn('text')
logger.error('text')
logger.time('text')
logger.timeLog('text')
logger.timeEnd('text')
logger.dir({ a: 1})
logger.table([
    { a: 1 }
])

format messages: short time file segments label message

 ..22:40:33 nlogs/lib/test.js info   message

Params:

filename

String

const Logger = require('nlogs')

const logger = Logger('/Users/mac/nlogs/lib/file.js')

Object or Module

const Logger = require('nlogs')

const logger = Logger({
    filename: '/Users/mac/nlogs/lib/file.js'
})

or:

const logger = require('nlogs')(module)

import.meta

import Logger from 'nlogs'

const logger = Logger(import.meta)

config

optional argument. An object

default config

Style

Style property is an Array of Strings For styling, the colors module is used. But you can use your own.

prop nametypedefaultdescription
timeStyle['gray', 'bgBlack']style for default build time label
labelStyle['white', 'bold']style for default build methodName label
segmentsSegmentstyle for default build segments (path & moduleName) label
formattersFormatterscustom formatters
enableFileLinkBooleantrueterminal-link
groupedEveryTime'minute' | 'hour' | 'day' | null'hour'if not NULL, each time the indicated timestamp of the log does not match the previous one, an additional message is displayed indicating the full date and time
methodsMethodsconfig for each method
Formatters

formatters are Functions

prop nameargumentsdefaultdescription
stringString, StylesstringFormatterformat string labels with styles
timetimestamp, isShort: BooleantimeFormatter.jsformat time labels
Segments

segments is the converted path to the file relative to the root (at the level of the nearest package.json

Example:

filename: /Users/mac/nlogs/lib/file.js package.json: /Users/mac/nlogs-directory/package.json package name: nlogs

result: nlogs/lib/file.js

Each property describes a style as an array of strings.

prop namedefaultdescription
first['yellow', 'italic', 'dim']first segment (moduleName)
last['cyan', 'italic']filename style
all['grey', 'italic']path sections (from module dir to filename)
delimiter['gray']separator path sections. path.sep from module path
methods

Settings for each of the methods separately.

  • log
  • info
  • debug
  • warn
  • error
  • time (time, timeEnd, timeLog methods)
  • dir
  • table

Some properties, in their absence, are taken from the config

method config

prop nametypedescription
showBooleanSets whether this method works
timeStylestyle for method build time label. If not specified, use default
segmentsSegmentsstyle for method build segments (path & moduleName) label. If not specified, use default
labelStylestyle for method build methodName label. If not specified, use default
showStartTimerBooleanShow message when invoking logger.time()? Only time method
warningPresentLabelBooleanwarning if the label exists when invoking logger.time(). Only time method
warningMissingLabelBooleanwarning if the label does not exist when invoking logger.timeEnd() and logger.timeLog(). Only time method
changePresentLabelBooleanoverwrite the label if one already exists. Only time method
diffTimeColorStylediff time style. Only time method
depthNumberdepth param to dir method

Global configs

const { getConfig, setConfig } = require('nlogs')

logger.time()

logger.time(label) returns tmeEnd Function

logger.time('label')
logger.timeLog('label')
logger.timeEnd('label')

equivalent:

const timeEnd = logger.time('label')
timeEnd.log()
timeEnd()
2.4.1

11 months ago

2.3.2

11 months ago

2.4.3

11 months ago

2.4.2

11 months ago

2.4.4

11 months ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago