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

@dreipol/vue-log

v0.0.8

Published

This npm package provides a system of log enhancements for vue applications.

Downloads

154

Readme

vue-log

This npm package provides a system of log enhancements for vue applications.

Build Status NPM version NPM downloads MIT License

Purpose

vue-log enables you to implement multiple log levels with enhanced and coloured messages based on your environment. For example, you can log coloured debug messages to the console, discerning between sibling vue components in a list. In production you can send the same messages to your error reporting framework (for example Sentry/Raven) as breadcrumbs in case of an error occuring.

Quickstart

  1. Install the module

    npm install -S @dreipol/vue-log
  2. Register the plugin to your Vue instance

    import VueLog from '@dreipol/vue-log';
    Vue.use(VueLog);
  3. Start logging!

    // As a global instance
    const Log = Vue.log();
    
    function isPrimary(color) {
        if (!color) {
            Log.error(`Uh oh, no color was provided. That doesn't look right...`);
        }
    
        return ['red', 'green', 'blue'].indexOf(color) > -1;
    }
    
    // In a component: my-favourite-color
    export default {
        props: {
            color: String
        },
        mounted() {
            this.$log.debug('Component mounted!');
    
            if (isPrimary(this.color)) {
                this.$log.info('Favourite color is a primary color');
            } else {
                this.$log.warn(`Favourite color is no primary color, but that's ok... We don't judge!`);
            }
        }
    }

Config

You have multiple options to add a config either globally or when creating a new Logger.

Global config

When installing the plugin you may add a second parameter to extend the original presets. The new config will be used in two different ways:

  • As a preset whenever you create a new log instance explicitly (by using Vue.log)
  • As the config when creating a log instance implicitly (by using this.$log in a vue component)
    Vue.use(VueLog, { /* vue-log config */ });

Local config

For every log instance that you create explicitly (by using Vue.log), you can add a config object that will extend the global config.

    const Log = Vue.log({ /* vue-log config */ });

Config object

For a detailed description of the config object, please see the abstract-log documentation.

Environments

Switching between development and production code can be done like in many similar situations:

    Vue.use(VueLog, process.env.NODE_ENV === 'production' ? productionConfig : developmentConfig);

Examples

Logging to the console

Console output should work out of the box with the default preset.

Custom log messages

You can greatly customize your log messages by extending the log config object:

    Vue.use(VueLog, {
        logger: window.console,
        levels: [
            {
                name: 'log',
                fn: window.console.log,
                isCriticalError: false
            },
            {
                name: 'error',
                fn: window.console.error,
                isCriticalError: true
            },
        ],
        middlewares: [
            (result, { level, config }) => {
                const { isCriticalError } = level;

                if (isCriticalError) {
                    result.unshift('WARNING!!!');
                }

                return result;
            }
        ],
    });

Filter log levels

To filter below a certain threshold, you can use the filter property:

    const LOG_THRESHOLD = 'info';

    Vue.use(VueLog, {
        filter({ config, level }) {
            const logIndex = config.levels.findIndex(l => l.name === level.name);
            const thresholdIndex = config.levels.findIndex(l => l.name === LOG_THRESHOLD);

            return logIndex >= thresholdIndex;
        }
    });

Sentry

Sentry is a real-time crash reporting service. Its frontend reporting library is called Raven. When a javascript error occurs, Raven will report it to Sentry. You can add valuable debug information by storing what happened before the error occured:

    const { captureBreadcrumb } = window.Raven || {};
    const RAVEN_LEVEL_MAPPING = { debug: 'debug', info: 'info', warn: 'warning', error: 'error' };
    
    Vue.use(VueLog, {
        logger: window.Raven,
        proxy: false,
        levels: [
            { name: 'debug', fn: captureBreadcrumb },
            { name: 'info', fn: captureBreadcrumb },
            { name: 'warn', fn: captureBreadcrumb },
            { name: 'error', fn: captureBreadcrumb },
        ],
        middlewares: [
            (result, { level, config, statements }) => {
                result.push([
                    {
                        message: statements.toString ? statements.toString() : '',
                        category: 'vue-log',
                        level: RAVEN_LEVEL_MAPPING[level.name],
                        data: JSON.parse(JSON.stringify(config.context))
                    },
                ]);

                return result;
            }
        ]
    });