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

@bubblehunt/logger

v1.4.1

Published

Helper for writing logs using transports

Downloads

18

Readme

Logger

Example

import Logger from '@bubblehunt/logger';

// Simple
// --------------------------------------------------------
const simpleLogger = new Logger;

simpleLogger.info( 'Writes to stdout', 1, { a: [ 123 ] } );


// Full
// --------------------------------------------------------
import FileTransport from '@bubblehunt/logger/transports/FileTransport';

const logPath = __dirname + 'out';

const logger = new Logger({
    // Minimal level which would be printed.
    // Levels strictness from min to max:
    //   'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal' | 'none'
    level: 'info',
    // Same as level but for stdout specially
    stdoutLevel: 'debug',
    // Whether to process.exit( 1 ) on fatal error
    stopOnFatal: true,
    // Log process label
    hostname: 'Main',
    // Separates each message in stdout
    stdoutMsgSeparator: '----------------------',
    // Sets or disables stdout: Logger | Transport | false
    // default: StdoutTransport
    stdout: undefined,
    // Every message will be written to transport if level matches
    transports: [
        new FileTransport({
            level: 'trace',
            // If not strict then all upper levels will also be printed
            strict: false,
            // here we explicitly set print type
            printType: 'json',
            // File will be created right away even if no data was written
            createOnFirstWrite: false,
            filePath: `${ logPath }/0_all.json`,
        }),
        new FileTransport({
            level: 'trace',
            filePath: `${ logPath }/1_trace.json`,
        }),
        new FileTransport({
            level: 'debug',
            filePath: `${ logPath }/2_debug.json`,
        }),
        new FileTransport({
            level: 'info',
            filePath: `${ logPath }/3_info.json`,
        }),
        new FileTransport({
            level: 'warn',
            filePath: `${ logPath }/4_warn.json`,
        }),
        new FileTransport({
            level: 'error',
            filePath: `${ logPath }/5_error.json`,
        }),
        new FileTransport({
            level: 'fatal',
            filePath: `${ logPath }/6_fatal.json`,
        }),
    ],
    printConfig: {
        // Whether to use colors in stdout
        colors: false,
        // Object print depth
        depth: 5,
        // First elements quantity to print
        maxArrayLength: 30,
    },
    // arguments to JSON.stringify if printType: 'json'
    jsonStringifyArgs: [],
});

logger.info( 'writes to first transport, with level "info" and to stdout' );

logger.trace( { a: 1 }, 1 ); // nothing: requires level "debug" or higher

logger.stdout.info( 'writes only to stdout' );


// Make child with new options
// --------------------------------------------------------
const logger2 =
        logger.child({
            hostname: 'Child-1',
            level: 'trace',
        });

logger2.trace( 'writes to two first transports with level "trace"' );


// Make child with new options
// --------------------------------------------------------
const logger3 =
        logger2.child({
            hostname: 'Child-2',
            stdoutLevel: 'trace',
        });

logger3.trace( 'writes to two first transports with level "trace" and to stdout' );


// Make child with new options
// --------------------------------------------------------
const logger4 =
        logger3.child({
            hostname: 'Child-3',
            level: 'error',
            stdoutLevel: 'error',
        });

logger4.info( 'no writes' );

// if "info" is ignored then "state" can be used to log smth important
logger4.state( 'writes to first transport and stdout' );

Transport

Example: create Transport

/* @flow */

// =============================================================================
// Imports
// =============================================================================
import {
    Transport,
    type TransportConfigType,
    type HandlerType,
} from '@bubblehunt/logger/transports/Transport';

// Flow types
// --------------------------------------------------------
type DefaultConfigType = {};

type MyTransportConfigType =
    & TransportConfigType
    & DefaultConfigType;


// =============================================================================
// Constants
// =============================================================================
const DefaultConfig = {
    // Required message level to call handler
    // level?: LevelType,

    // Called if current Transport class has no method 'handler'
    // handler?: HandlerType,

    // Messages format
    // printType?: 'json' | 'simple-cli' | 'simple',

    // if true ( default ) then msg will be printed only if level is equal to transport's
    // if false it is printed when level is equal or greater
    // strict?: boolean,
};


// =============================================================================
// MyTransport
// =============================================================================
export class MyTransport extends Transport<MyTransportConfigType> {

    constructor( config?: TransportConfigType ) {
        super({ ...DefaultConfig, ...config });
    }

    handler: HandlerType = ( logStr, options ) => {
        console.log( logStr, options );
    };

    end = () => {};
}

export default MyTransport;