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

@saninn/logger

v1.2.0

Published

A configurable wrapper around the console object (without losing the console call position)

Downloads

81

Readme

👨‍💻💻

A configurable wrapper of the console that keeps the log call position.

Made with Typescript, usable as es6 module and iife (with IE10 support).

Build Status codecov Known Vulnerabilities Maintainability Licence

The Problem

You need to control when activate or deactivate your app or website logging but when your put console.log inside another class or function the console call position is lost. Well not anymore!

example

Install

npm install @saninn/logger.

If you want to use it as es6, commonjs or iife download the respective assets in the Releases Page. As alternative you can clone this project, run npm install and npm run build-bundles.

Note: The iife version contains a Polyfill for Javascript Proxy Api.

What can I do?

  • Set a prefix to all your console calls(log, warn, error, etc) so it is easy to filter while debugging;
  • Add a color to each console calls (supported in all browsers but Internet Explorer).
  • Set logger levels to prevent console flood (Debug, Info, Warn, Error and Fatal);
  • Process external loggers (like server side) with the same call as the local log.
  • Process just external loggers.
  • Procces just local loggers.
  • Register external loggers on run time.
  • Remove external loggers on run time.

Basic usage

Typescript

import { SaninnLogger } from '@saninn/logger';
const myLogger = new SaninnLogger('my-logger-prefix');
myLogger.log('this is a log'); // [my-logger-prefix]: this is a log.

Javascript (es6 module)

index.html

<head>
  <script src="main.js" type="module"></script>
</head>

main.js

import { SaninnLogger } from './node_modules/@saninn/logger/dist/@saninn__logger.js';
const myLogger = new SaninnLogger('my-logger-prefix');
myLogger.log('this is a log'); // [my-logger-prefix]: this is a log.

Javascript (es5)

  • First download the Immediately-invoked function expression (iife) from the Releases Page and import it before your javascript entry point

index.html

<head>
  <script src="@saninn__logger.js"></script>
  <script src="main.js"></script>
</head>

main.js

var myLogger = new SaninnLogger('my-logger-prefix');
myLogger.log('this is a log'); // [my-logger-prefix]: this is a log.

API and Documentation

  • enableGlobalLoggerFunctions(): void
  • disableGlobalLoggerFunctions(): void
  • enableLoggerProcessors(): void
  • disableLoggerProcessors(): void
  • addLoggerProcessor(logType: LoggerTypesEnum, loggerProcessor: LoggerProcessor): void
  • removeLoggerProcessor(logType: LoggerTypesEnum, loggerProcessor: LoggerProcessor): void
  • setLoggerLevelTo(level: LogLevelsEnum): void

Important!!

A log level set to INFO will print logger.log(), logger.info() and logger.dir() calls.

See the Documentation in https://logger.saninnsalas.com for full details.

Use cases

You can see the Wiki for ideas and use cases where @saninn/logger can help you!

Full options example

const loggerWithFullConfigAndProcessors = new SaninnLogger({
  useGlobalPreLoggerFunctions: true,
  globalPreLoggerFunctions: {
    dir: (prefix) => {
      console.log(
        'This is a DIR preLoggerFunction that is not the direct console.dir',
        'This is The Prefix:  ' + prefix
      );
    },
    error: (prefix) => {
      console.log(
        'This is a ERROR preLoggerFunction that is not the direct console.error',
        'This is The Prefix:  ' + prefix
      );
    },
    log: (prefix) => {
      console.log(
        'This is a LOG preLoggerFunction that is not the direct console.log',
        'This is The Prefix:  ' + prefix
      );
    },
    warn: (prefix) => {
      console.log(
        'This is a WARN preLoggerFunction that is not the direct console.warn',
        'This is The Prefix:  ' + prefix
      );
    },
  },
  prefix: 'full-config-logger',
  prefixColors: {
    error: 'blue',
    log: 'green',
    warn: 'red',
  },
  printToConsole: true,
  useLoggerProcessors: true,
  loggerProcessors: {
    log: [
      (prefix, args) => {
        console.log('FIRST logger Processor para saninnLogger.log');
        console.log('prefix: ', prefix);
        console.log('args: ', args);
      },
      (prefix, args) => {
        console.log('SECOND logger Processor para saninnLogger.log');
        console.log('prefix: ', prefix);
        console.log('args: ', args);
      },
    ],
  },
});

const dummyObject = {
  a: 1,
  b: 2,
  c: {
    d: 3,
    e: 4,
  },
};

const dummyFunction = function () {
  console.log('dummy function');
};
loggerWithFullConfigAndProcessors.log('log of loggerWithFullConfigAndProcessors', dummyObject, dummyFunction);
loggerWithFullConfigAndProcessors.warn('warn of loggerWithFullConfigAndProcessors');
loggerWithFullConfigAndProcessors.error('error of loggerWithFullConfigAndProcessors');
loggerWithFullConfigAndProcessors.dir('dir of loggerWithFullConfigAndProcessors');

console output

Use with JEST as test runner.

If you use jest as test runner you need to tell jest not to transform @saninn/logger using this in your jest config:

transformIgnorePatterns: ['<rootDir>/node_modules/(?!@saninn|@someOtherPackage)']

License

MIT

Development

This project uses:

Scripts

There are 3 scripts I use together for dev (each in their own console): 'watch', 'serve' and 'test'.

  • 'npm run watch' will look for Typescript changes and compile it to es6.
  • 'npm run serve' will load a local server with './index-es6.html' as entry point. It uses './script.js' as module loader.
  • 'npm run test -- --watch --coverage --silent' will run jest tests on each typescript change.

ToDo

  • Pack all development scripts into just one...
  • Project

Pull requests are welcome

Find me 🏃‍