Better Logger
Easy and lightweight console logging for your JavaScript and TypeScript projects. Control log levels in development vs production, integrate with third-party log tools and debug Vue applications.
Features
- Log levels that follow and extend browser console log levels
success
, critical
and run
methods for more secure and informative logging- Nice looking and fully configurable output
- Custom formatter for Vue
- Full TypeScript support
- Good test coverage
Installation
pnpm add @slipmatio/logger
Basic Usage
The easiest way to use this logger is to use the useLogger
or useVueLogger
helpers. The helpers are configured to print only errors in production and print all but debug messages in development mode unless called with debug=true
. You can also set the logger name
attribute. Both arguments are optional:
import { useLogger } from '@slipmatio/logger'
const logger = useLogger()
Then just use logger
wherever you want:
logger.log('Hello World!')
logger.run()
Vue Logger
Vue logger uses a special logger function that pretty prints ref, reactive, and computed objects for you so instead of unreadable garble you can read the logs easily.
import { ref, computed } from 'vue'
import { useVueLogger } from '@slipmatio/logger'
const logger = useVueLogger('vuelogger')
const num = ref(1)
const timesTwo = computed(() => num.value * 2)
console.log('proxy objects', num, timesTwo)
logger.log('proxy objects', num, timesTwo)
Automatic Imports
Tip: use something like unplugin-auto-import with your bundler to automagically import your logger instance wherever you need it.
Setting Up Manually
You can also instantiate and import the logger manually. It works as you'd expect:
import { Logger, LogLevel } from '@slipmatio/logger'
const logger = new Logger({
logLevel: process.env.NODE_ENV !== 'production' ? LogLevel.INFO : LogLevel.ERROR,
name: 'mylogger',
logFn: myLogFn,
})
Status
This project was originally born around 2016 and has been copy-pasted in various formats from project to project. As it has been helpful in so many projects, I finally decided to clean it up, convert to TypeScript and release as open source. Some parts of the code are a bit ugly, but it works well enough for most production needs. Contributions welcome!
Elsewhere
Follow @uninen on Twitter