npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dorianb/logger-js

v0.7.6

Published

A simple logger library for JS and NodeJS

Downloads

37

Readme

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 &[email protected] 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)

| Param | Type | Description | | --- | --- | --- | | opts | OptionsObject | logger default values |

Example

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

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

Chainable

| Param | Type | Description | | --- | --- | --- | | filename | string | file where the log is written | | level | number | string | level of the log | | message | string | content 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

| Param | Type | Default | Description | | --- | --- | --- | --- | | info | string | | content of the log | | [filename] | string | "options.filename" | filename without path |

Example

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

Logger.debug(debug, [filename]) ↩︎

Chainable

| Param | Type | Default | Description | | --- | --- | --- | --- | | debug | string | | content of the log | | [filename] | string | "options.filename" | filename without path |

Example

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

Logger.warn(warning, [filename]) ↩︎

Chainable

| Param | Type | Default | Description | | --- | --- | --- | --- | | warning | string | | content of the log | | [filename] | string | "options.filename" | filename without path |

Example

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

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

Chainable

| Param | Type | Default | Description | | --- | --- | --- | --- | | error | string | | content of the log | | [filename] | string | "options.filename" | filename without path | | [opts] | object | {} | 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

| Param | Type | Default | Description | | --- | --- | --- | --- | | error | string | | content of the log | | [filename] | string | "options.filename" | filename without path |

Example

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

Logger.clear([...filename]) ↩︎

Chainable

| Param | Type | Default | Description | | --- | --- | --- | --- | | [...filename] | string | "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]

| Param | Type | Description | | --- | --- | --- | | level | string | number | the 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]

| Param | Type | Description | | --- | --- | --- | | newLevel | string | The 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)

| Param | Type | Description | | --- | --- | --- | | event | string | 'log' | 'error' | | callback | function | |

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

| Name | Type | Default | Description | | --- | --- | --- | --- | | [filename] | string | "'logs.log'" | The name of the default log file | | [folder] | string | "'./logs/'" | The folder where logs files will be located (sorry for the name, couldn't find more descriptive) | | [extension] | string | "'.log'" | The extension to use for logs files | | [useMoment] | boolean | false | Use moment-js to format the dates. Allow timezone options but has a performance cost | | [timezone] | string | "'Europe/Berlin'" | The moment timezone for the date | Full list available at: https://momentjs.com/timezone | | [console_logs] | boolean | false | Use console.log to displays logs instead of writing it in a log file | | [displayLevel] | string | number | 0 | The level below a log is not displayed | | [showPID] | boolean | false | Display the PID of the process in the log | | [showHostname] | boolean | false | Display the hostname in the log | | [align] | string | "left" | Where should the level be aligned ('left'|'center'|'right') |


2020 © Dorian Beauchesne