leaf-logger

Easy and simple console formatting / logging

Usage no npm install needed!

<script type="module">
  import leafLogger from 'https://cdn.skypack.dev/leaf-logger';
</script>

README

example_0

Leaf-Logger

Easy and simple console formatting / logging

Javascript

FeaturesInstallationUsageBuild withLicense

Features

  • Easy logging
  • Async & Sync functions
  • Supports error, info, debug and warning messages
  • Debug mode (Displays object with message)
  • Custom theming
  • Custom time formatting
  • Custom file logging

Installation

Simply run

# With NPM
$ npm i leaf-logger
# Or with YARN
$ yarn add leaf-logger

Usage

! Version 4 is a full rework of leaf-logger, and breaks everything from the previous versions !

Instanciation

The logger is easily instanciable.

var leafLogger = require('leaf-logger')
const Logger = new leafLogger(options?)

Providing options to constructor is optional. By default, they are set to:

const defaultOptions = {
  debug: false, // Shows & logs advanced debug data that you've provided
  showTime: false, // Shows time before message
  timeFormat: 'HH:MM:ss', // Default time format
  fileLogging: false, // Enable/Disable file logging
  loggingFilePath: __dirname + path.sep + 'logs.log', // Default file path (Needs to be change by your own !)
  theme: {
    error: 'red', // Error message prefix color
    info: 'cyan', // Info..
    debug: 'grey', // Debug..
    warn: 'yellow' // Warning..
  }
}

If you want to set this options later, you can use:

Logger.setOptions(options)

Theme colors are based on chalk colors, available here

Time formatting is based on dateformat masks, available here

Methods

example_1

There are 4 available methods, displaying message in console like above:

Logger.error('This is an error')
Logger.info('This is an information')
Logger.warn('This is a warning')
Logger.debug('This is a debug')

Used like this, these methods returns a Promise, so you can chain Logging -> What you want to do next using then() function on it.

If you need logging to be synchronous, you can call the same methods with "Sync" at the end, like:

Logger.errorSync('This is an error')
Logger.infoSync('This is an information')
Logger.warnSync('This is a warning')
Logger.debugSync('This is a debug')

Debug mode

example_1

Basically, debug mode just displays more informations like above. Here is the example:

Logger.setOptions({
  debug: true
})
Logger.info('Page has been requested', { url: '/hello' })

Debug mode is available on all the methods.

File logging

And finally, if you want to log everything that happened in a log file, you just have to change the options, for example:

Logger.setOptions({
  fileLogging: true, // Enable file logging
  loggingFilePath: __dirname + 'my/path/log.txt' // Give file path
})

If the logging file doesn't exist, Leaf-Logger will create it for you.

Built with

  • chalk - 🖍 Terminal string styling done right
  • dateformat - A node.js package for Steven Levithan's excellent dateFormat() function.
  • strip-ansi - Strip ANSI escape codes from a string
  • jest - Delightful JavaScript Testing.

License

This project is under MIT License - see LICENSE.md for further details.