0.7.6 • Published 2 years ago

@dorianb/logger-js v0.7.6

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Logger library

npm version npm downloads

logger-js is a npm logger library for NodeJS

This logger is primarly designed for a backend usage, to handle the logs of a node server, or for a bot. It's lighter and easier to use than other libraries and does not require any configuration (at least for a small/medium project). It's particulary fast and require almost no running time.

The logger will write the logs by default in the logs/ directory to the root of your project. You can change the location and the name of this folder. The default log file is logs.log.

This project is part of the vener.fr project, to collect the errors and different information of the server (express).

Installation

To install the package, just run :

npm install --save @dorianb/logger-js

Then in the .js file :

const Logger = require('@dorianb/logger-js')

Logger.info('Server is starting on port 9000')
Logger.debug('172 clients are currently connected')
Logger.warn('Unsafe call from client @7655671')
Logger.error('Socket &757@127.0.0.1 doesn\'t exist')
Logger.fatal('Internet connection lost')

Documentation

Classes

Typedefs

Logger

Logger.options ⇒ OptionsObject

Getter: Return the options of the logger
Example

const loggerOptions = Logger.options

Logger.version ⇒ string

Returns: string - the version number of the logger
Getter: Version getter
Example

const version = Logger.version

Logger.levels ⇒ LevelsObject

Getter: Levels object getter - All the levels of the logger
Example

const levels = Logger.levels

Logger.setOptions(opts)

ParamTypeDescription
optsOptionsObjectlogger default values

Example

Logger.setOptions({filename: 'production.log'})

Logger.log(filename, level, message) ↩︎

Chainable

ParamTypeDescription
filenamestringfile where the log is written
levelnumber | stringlevel of the log
messagestringcontent of the log

Example

Logger.log('network.log', 'WARN', 'Socket disconnected')
Logger.log('network.log', 2, 'Socket disconnected')
// --> [10-06-2020 06:43:51] - WARN - Socket disconnected

Logger.info(info, filename) ↩︎

Chainable

ParamTypeDefaultDescription
infostringcontent of the log
filenamestring"options.filename"filename without path

Example

Logger.info('Server has started')
Logger.info('Server has started', 'server.log')

Logger.debug(debug, filename) ↩︎

Chainable

ParamTypeDefaultDescription
debugstringcontent of the log
filenamestring"options.filename"filename without path

Example

Logger.debug(`Client ID = ${clientID}`)
Logger.debug(`Client ID = ${clientID}`, 'clients.log')

Logger.warn(warning, filename) ↩︎

Chainable

ParamTypeDefaultDescription
warningstringcontent of the log
filenamestring"options.filename"filename without path

Example

Logger.warn(`Database disconnected`)
Logger.warn(`Database disconnected`, 'connections.log')

Logger.error(error, filename, opts) ↩︎

Chainable

ParamTypeDefaultDescription
errorstringcontent of the log
filenamestring"options.filename"filename without path
optsobject{}options

Example

Logger.error(`Connection to 127.0.0.1:2000 refused`)
Logger.error(`Connection to 127.0.0.1:2000 refused`, 'logs.log')

Logger.fatal(error, filename) ↩︎

Chainable

ParamTypeDefaultDescription
errorstringcontent of the log
filenamestring"options.filename"filename without path

Example

Logger.fatal(`Division by zero`)
Logger.fatal(`Division by zero`, 'big_errors.log')

Logger.clear(...filename) ↩︎

Chainable

ParamTypeDefaultDescription
...filenamestring"options.filename"The filenames of the files to clear or 'all' if all the files should be cleaned

Example

Logger.clear() // clear the default file (options.filename)
Logger.clear('client.log')
Logger.clear('client.log', 'connections.log', 'logs.log')
Logger.clear('all')

Logger.getLevel(level) ⇒ array

Returns: array - index, label

ParamTypeDescription
levelstring | numberthe index or the label of the level

Example

const testLevel = Logger.getLevel('warn') // --> ["2", "WARN"]
const testLevel = Logger.getLevel(2)      // --> ["2", "WARN"]

Logger.addLevel(newLevel) ⇒ array

Returns: array - the level array : index, label

ParamTypeDescription
newLevelstringThe label of the new level

Example

const [importantLevel, importantLabel] = Logger.addLevel('Important')
Logger.log('logs.log', importantLevel, 'Important message which will be display on top of all other levels')

Logger.on(event, callback)

ParamTypeDescription
eventstring'log''error'
callbackfunction

Example

Logger.on('log', log => console.log(log))
Logger.on('error', handleErrorsFunction)

LevelsObject : Object

A dictionary of the logger levels indexed by priority

Example

levels = {
     0: 'INFO',
     1: 'DEBUG',
     2: 'WARNING',
     3: 'ERROR',
     4: 'FATAL'
 }

OptionsObject : Object

Properties

NameTypeDefaultDescription
filenamestring"'logs.log'"The name of the default log file
folderstring"'./logs/'"The folder where logs files will be located (sorry for the name, couldn't find more descriptive)
extensionstring"'.log'"The extension to use for logs files
useMomentbooleanfalseUse moment-js to format the dates. Allow timezone options but has a performance cost
timezonestring"'Europe/Berlin'"The moment timezone for the dateFull list available at: https://momentjs.com/timezone
console_logsbooleanfalseUse console.log to displays logs instead of writing it in a log file
displayLevelstring | number0The level below a log is not displayed
showPIDbooleanfalseDisplay the PID of the process in the log
showHostnamebooleanfalseDisplay the hostname in the log
alignstring"left"Where should the level be aligned ('left''center''right')

2020 © Dorian Beauchesne

0.7.6

2 years ago

0.7.5

3 years ago

0.7.4

3 years ago

0.7.2

3 years ago

0.6.8

3 years ago

0.6.7

3 years ago

0.6.6

3 years ago

0.6.5

3 years ago

0.6.4

4 years ago

0.6.3

4 years ago

0.6.2

4 years ago

0.6.1

4 years ago

0.6.0

4 years ago

0.5.9

4 years ago

0.5.8

4 years ago

0.5.7

4 years ago

0.5.6

4 years ago

0.5.5

4 years ago

0.5.4

4 years ago

0.5.3

4 years ago

0.5.2

4 years ago

0.5.1

4 years ago

0.5.0

4 years ago

0.4.4

4 years ago

0.4.1

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.3.3

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago