1. loglevel-colored-level-prefix
loglevel plugin that adds colored level prefix (node only)
loglevel-colored-level-prefix
Package: loglevel-colored-level-prefix
Created by: kentcdodds
Last modified: Sun, 19 Jun 2022 13:56:34 GMT
Version: 1.0.0
License: MIT
Downloads: 3,204,199
Repository: https://github.com/kentcdodds/loglevel-colored-level-prefix

Install

npm install loglevel-colored-level-prefix
yarn add loglevel-colored-level-prefix

loglevel-colored-level-prefix

loglevel plugin that adds colored level prefix (node only)

Build Status
Code Coverage
Dependencies
version
downloads
MIT License

All Contributors
PRs Welcome
Donate
Code of Conduct
Roadmap
Examples

Watch on GitHub
Star on GitHub
Tweet

The problem

loglevel is great, and I find that I often want the log output to
be formatted the same way every time. Also I don't really like some of the
implementation of loglevel (specifically the fact that it uses some browser
APIs for some things.

This solution

This exposes a function to get a logger (singleton) with colored prefixes for
the level. Note that this only works in Node because it uses chalk.

Installation

This module is distributed via npm which is bundled with node and should
be installed as one of your project's dependencies:

npm install --save loglevel-colored-level-prefix

Usage

 const getLogger = require('loglevel-colored-level-prefix')
const options = {prefix: 'your-prefix', level: 'trace'}
const logger = getLogger(options)
logger.trace('WOW! What the stack trace!?')
// Trace: your-prefix [TRACE]: WOW! What the stack trace!?
//     at Logger.trace (<full-path>/node_modules/loglevel-colored-level-prefix/dist/index.js:54:24)
//     at ... etc
logger.debug('sup debug?')
// your-prefix [DEBUG]: sup debug?
logger.info('Hey info')
// your-prefix [INFO]: Hey info
logger.warn('Hi warn')
// your-prefix [WARN]: Hi warn
logger.error('Hello error')
// your-prefix [ERROR]: Hello error

Let's look at what that actually looks like...

options

prefix

?String - Whatever you want your prefix to be. Normally this is the tool
that you're logging for. The getLogger function will return the same instance
of the logger based on the given prefix.

level

?String - What you want the initial level to be set to. This defaults to:
process.env.LOG_LEVEL || 'warn'. Possible options are (in order of verbosity):
trace, debug, info, warn, error.

returns

An instance of a loglevel logger. Learn more about that API from the
loglevel docs.

Inspiration

I wrote this because I wanted to use the plugin I created for
prettier-eslint in
prettier-eslint-cli. And I'll probably use it in other
projects/tools as well.

Other Solutions

I'm unaware of other plugins for loglevel that do what this one does. But
there are many logging solutions out there...

Contributors

Thanks goes to these people (emoji key):


Kent C. Dodds

💻 📖 🚇 ⚠️

This project follows the all-contributors specification. Contributions of any kind welcome!

LICENSE

MIT

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code