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

@avine/ns-logger

v1.0.5

Published

Logger with namespace support for node and browser

Downloads

7

Readme

ns-logger

Logger with namespace support for node and browser

Build Status

Usage

Get logger

By default, only warn and error severity levels are displayed in the console.

import { getLogger } from '@avine/ns-logger';

const logger = getLogger('MyNamespace');

logger.trace('Trace hidden');
logger.log('Log hidden');
logger.warn('Warn visible');
logger.error('Error visible');

Console output:

[MyNamespace] Warn visible
[MyNamespace] Error visible

As you can see, the logs are prefixed by the namespace.

Check enabled severity levels

Determine whether a severity level is enabled, by checking the enabled readonly property.

import { getLogger } from '@avine/ns-logger';

const logger = getLogger('MyNamespace');

console.log('Trace:', logger.trace.enabled);
console.log('Log:', logger.log.enabled);
console.log('Warn:', logger.warn.enabled);
console.log('Error:', logger.error.enabled);

Console output:

Trace: false
Log: false
Warn: true
Error: true

Change severity level of logger instance

To change the severity level of logger instance programmatically, set its level property using the Level enum:

export enum Level {
  Trace,  // (= 0)
  Log,    // (= 1)
  Warn,   // (= 2)
  Error,  // (= 3)
  Silent  // (= 4)
}

Here's how to use it:

import { getLogger, Level } from '@avine/ns-logger';

const logger = getLogger('MyNamespace');

logger.level = Level.Log; // Using Level enum

logger.trace('Trace hidden');
logger.log('Log NOW visible!');
logger.warn('Warn visible');
logger.error('Error visible');

logger.level = 4; // Using literal number

logger.trace('Trace hidden...');
logger.log('Log hidden...');
logger.warn('Warn hidden...');
logger.error('Error hidden...');

Console output:

[MyNamespace] Log NOW visible!
[MyNamespace] Warn visible
[MyNamespace] Error visible

Change default severity level

NsLogger keeps track of instantiated loggers. Existing loggers are NOT affected by new default level setting. Only fresh created loggers are affected.

import { getLogger, setDefaultLevel, Level } from '@avine/ns-logger';

const a = getLogger('NamespaceA'); // a.level === Level.Warn

setDefaultLevel(Level.Log);

const b = getLogger('NamespaceB'); // b.level === Level.Log

setDefaultLevel(Level.Error);

const aAlias = getLogger('NamespaceA'); // a.level is still Level.Warn
const bAlias = getLogger('NamespaceB'); // b.level is still Level.Log
const c = getLogger('NamespaceC'); // Only c.level is Level.Error

aAlias.warn('aAlias === a ?', aAlias === a);
bAlias.warn('bAlias === b ?', bAlias === b);

a.warn('level:', a.level);
b.log('level:', b.level);
c.error('level:', c.level);

Console output:

[NamespaceA] aAlias === a ? true
[NamespaceB] bAlias === b ? true
[NamespaceA] level: 2
[NamespaceB] level: 1
[NamespaceC] level: 3

Configure severity level of loggers from state object

To configure the severity level of loggers to be instantiated in a declarative way, use the state.level object.

interface ILevelState {
  [namespace: string]: Level;
}

The namespace key has the following pattern [Module]:[Feature].

You can use the symbol * as a wildcard to target all features of a module like this: [Module]:*.

In the same way, you can use this symbol to target all modules and features (this is like overwriting the default severity level declaratively).

import { getLogger, state } from '@avine/ns-logger';

state.level = {
  'ModuleA:Feature1': 0,
  'ModuleA:Feature2': 1,
  'ModuleA:*': 2, // Wildcard for all features of a module
  'ModuleB': 3,
  '*': 4, // Wildcard for all modules and features
};

console.log(
  getLogger('ModuleA:Feature1').level,
  getLogger('ModuleA:Feature2').level,
  getLogger('ModuleA:Feature3').level, // Matches 'ModuleA:*'
  getLogger('ModuleB').level,
  getLogger('ModuleC').level, // Matches '*'
);

Console output:

0 1 2 3 4

Configure severity level of loggers from localStorage

In the brower, you can set the state.level object using localStorage.NsLogger.

The NsLogger property has following pattern: [Module]:[Feature] = [Level]; ...

To get the same result as above, enter the following line in the browser console and reload the page:

localStorage.NsLogger = 'ModuleA:Feature1 = 0; ModuleA:Feature2 = 1; ModuleA:* = 2; ModuleB = 3; * = 4;';

Browser support

NsLogger supports all major browsers (including IE11).

You can use the script https://unpkg.com/@avine/ns-logger/ns-logger.js that exposes the package as the global variable NsLogger.

<script src="https://unpkg.com/@avine/ns-logger/ns-logger.js"></script>

<script>
  const logger = NsLogger.getLogger('MyNamespace');
  logger.warn('Cool!'); // [MyNamespace] Cool!
</script>

You can also import the package in your code and bundle your application with webpack for example or any other bundler of your choice.

import { getLogger } from '@avine/ns-logger';

// Your code...

For a live preview, check out this demo in your favorite browser.

Plugins

Chalk-plugin

This plugin is designed for use in node (not in the browser) and uses Chalk package to style the logs prefix depending on the severity level.

import '@avine/ns-logger/chalk-plugin';
import { getLogger } from '@avine/ns-logger';

const logger = getLogger('MyNamespace');
logger.error('Message...');

Console output:

[MyNamespace] Message...

The string [MyNamespace] will appear in red color in the console (trust me :-).

Contribute

NsLogger is written in TypeScript, and that's the fun part.

git clone https://github.com/avine/ns-logger.git

cd ./ns-logger

npm install

npm run all # npm run lint && npm test && npm run build

npm start # this will launch a demo in your favorite browser

License

MIT @ Avine