0.1.4 • Published 1 year ago

@tapraise/logger v0.1.4

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

@tapraise/logger

Advanced logging with:

Installation

npm install --save @tapraise/logger

Examples

Namespace prefixes

When there is a lot being logged to the console, having namespace prefixes allows you to easier scan for specific subjects.

Example:

import { logger } from '@tapraise/logger';

const logger = new Logger({
    namespace: 'security',
});

logger.debug('debugging!'); // will log '[security] debugging!'
logger.warn('look out!'); // will log '[security] look out!'

Also able to apply or update after construction:

import { logger } from '@tapraise/logger';

const logger = new Logger();
logger.setNamespace('security');

Queue-ing

Allows you to queue logs and flush to console only when needed. For instance when you only want to log the output when an error occurs, to provide the context needed to fix it.

Example:

import { logger } from '@tapraise/logger';

const logger = new Logger({
    isQueued: true,
});

try {
    const someInput = {
        // ...
    };
    logger.debug('input:', someInput); // does not log to console yet

    executeTaskThatMightThrow(someInput);

    // resets the logger and with it the queue
    logger.reset();
} catch (error) {
    logger.error('Something went wrong: ', error);
    logger.info('context:');
    logger.flush(); // flushes all queued logs to the console
}

Also able to start queue-ing later on:

import { logger } from '@tapraise/logger';

const logger = new Logger();

logger.debug('Something'); // will be logged to the console right away
logger.queue();
logger.debug('This line is queued');
logger.flush(); // now the previous line is also logged to the console

Custom formatters

To make the output in your console scannable, so you can find the output you need easily, use formatters.

import { logger, SectionFormatter } from '@tapraise/logger';

const logger = new Logger({
    formatters: {
        // Register the formatter in the registry with an identifier
        section: new SectionFormatter(),
    },
});

// now use formatter. Make sure the first argument you log is the formatter identifier, otherwise
// it will not be applied
logger.info('section', 'Some section title');

It is also possible to add formatters on the fly:

import { Logger, SectionLogger } from '@tapraise/logger';

const logger = new Logger();
logger.setFormatter('section', new SectionLogger());

It is also possible to add your own, custom formatters:

import { Logger, FormattedArgs, FormatterInterface } from '@tapraise/logger';

class CustomFormatter extends FormatterInterface {
    format(args: any[]): FormattedArgs {
        // return an array to represent each line, and within that an array for arguments to log on that line, like:

        return [['.....'], args, ['.....']];
    }
}

const logger = new Logger({
    formatters: {
        custom: new CustomFormatter(),
    },
});

Centralized enabling and disabling

To not have to check your environment throughout your code, when logging, you can now centralize checking if logging should actually be done:

const logger = new Logger({
    enabled: process.env.production === false,
});

Build in formatters:

  • SectionFormatter → formats section header
  • ErrorFormatter → formats error messages nice and red!
  • NoteFormatter → formats notes in muted color
  • JsonFormatter → formats JSON strings to be better read- and scan-able, by using indenting
  • SuccessFormatter → formats all in green as success message(s)

Todo

  • Instead of just enable or not, add more fine grained solution where you can define which levels should be logged
  • Add listener system that allows you to hook into the system on specific level logs
  • Some more custom formatters, based upon what we did in other projects
  • Apply color support for browser. Perhaps using CSS styling?
  • Be able to apply a bunch of formatters at once, like a plugin?
0.1.4

1 year ago

0.1.3

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago