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

triviality-logger

v0.1.4

Published

Typescript loggers with an interface that support composition

Downloads

25

Readme

Table of Contents

Licence Build Status npm version coverage

Installation

To install the stable version:

yarn add triviality-logger

This assumes you are using yarn as your package manager.

or

npm install triviality-logger

Triviality logger

Exposes TypeScript Logger interface compatible with:

  • Web and node
  • ts-log interface
  • node-bunyan node-bunyan
  • Compatible with triviality ServiceContainer. Pre-configured logger modules, ready to use in your Triviality modules.
  • Add composition functionality for combing loggers

LoggerInterface

Besides the normal log functions each logger has it's log function. The log function that support logging based on LogLevel. Can be used for easy composition.

export enum LogLevel {
  trace,
  debug,
  info,
  warn,
  error,
}

export interface LoggerInterface {
  trace(message?: any, ...optionalParams: any[]): void;
  debug(message?: any, ...optionalParams: any[]): void;
  info(message?: any, ...optionalParams: any[]): void;
  warn(message?: any, ...optionalParams: any[]): void;
  error(message?: any, ...optionalParams: any[]): void;

  log(level: LogLevel, message: any, ...optionalParams: any[]): void;
}

Example log level

import { ConsoleLogger } from 'triviality-logger';
import { LogLevel } from 'triviality-logger';

const logger = new ConsoleLogger(console);
logger.log(LogLevel.info, 'Hallo', 'World');
./node_modules/.bin/ts-node example/logLevel.ts 
Hallo World

Loggers

console logger

import { ConsoleLogger } from 'triviality-logger';

const logger = new ConsoleLogger(console);
logger.info('Hallo', 'World');
logger.info('Bye %s', 'World');
./node_modules/.bin/ts-node example/consoleLogger.ts 
Hallo World
Bye World

process logger

import { ProcessLogger } from 'triviality-logger';

const logger = new ProcessLogger(process);
logger.info('Hallo', 'World');
./node_modules/.bin/ts-node example/processLogger.ts 
Hallo World

prefix logger

import { ConsoleLogger } from 'triviality-logger';
import { PrefixLogger } from 'triviality-logger';

const logger = new ConsoleLogger(console);
const withPrefix = new PrefixLogger(logger, 'Hallo: ');
withPrefix.info('World');
./node_modules/.bin/ts-node example/prefixLogger.ts 
Hallo: World

log from LogLevel logger

import { ConsoleLogger } from 'triviality-logger';
import { FromLogLevelLogger } from 'triviality-logger';
import { LogLevel } from 'triviality-logger';

const logger = new ConsoleLogger(console);
const witPrefix = new FromLogLevelLogger(logger, LogLevel.info);
witPrefix.trace('This will be ignored');
witPrefix.info('Hallo!');
./node_modules/.bin/ts-node example/fromLogLevelLogger.ts 
Hallo!

ts-log logger

With this you can also wrap node-bunyan

import { TsLogLogger } from 'triviality-logger';
import { Logger } from 'ts-log';

const tsLogger: Logger = console;
const logger = new TsLogLogger(tsLogger);
logger.info('Hallo', 'World');
./node_modules/.bin/ts-node example/tsLogLogger.ts 
Hallo World

null logger

import { NullLogger } from 'triviality-logger';

const logger = new NullLogger();
logger.info('Hallo', 'Void');

collection of loggers

Combine loggers into a single one.

import { CollectionLogger } from 'triviality-logger';
import { ConsoleLogger } from 'triviality-logger';
import { PrefixLogger } from 'triviality-logger';

const consoleLogger = new ConsoleLogger(console);
const logger = new CollectionLogger([
  new PrefixLogger(consoleLogger, 'Hallo '),
  new PrefixLogger(consoleLogger, 'Bye '),
]);
logger.info('World');
./node_modules/.bin/ts-node example/collectionLogger.ts 
Hallo World
Bye World

Abstract logger class

You can extends one of the abstract logger, so you only need to implement some of the log function.

import { LoggerInterface, LogLevel } from './LoggerInterface';

export abstract class AbstractLogLevelLogger implements LoggerInterface {
  public trace(message?: any, ...optionalParams: any[]): void {
    this.log(LogLevel.trace, message, ...optionalParams);
  }

  public debug(message?: any, ...optionalParams: any[]): void {
    this.log(LogLevel.debug, message, ...optionalParams);
  }

  public info(message?: any, ...optionalParams: any[]): void {
    this.log(LogLevel.info, message, ...optionalParams);
  }

  public warn(message?: any, ...optionalParams: any[]): void {
    this.log(LogLevel.warn, message, ...optionalParams);
  }

  public error(message?: any, ...optionalParams: any[]): void {
    this.log(LogLevel.error, message, ...optionalParams);
  }

  public abstract log(type: LogLevel, message?: any, ...optionalParams: any[]): void;

}
import { LoggerInterface, LogLevel } from './LoggerInterface';

export abstract class AbstractFunctionLogger implements LoggerInterface {

  public abstract trace(message?: any, ...optionalParams: any[]): void;
  public abstract debug(message?: any, ...optionalParams: any[]): void;
  public abstract info(message?: any, ...optionalParams: any[]): void;
  public abstract warn(message?: any, ...optionalParams: any[]): void;
  public abstract error(message?: any, ...optionalParams: any[]): void;

  public log(level: LogLevel, message?: any, ...optionalParams: any[]): void {
    switch (level) {
      case LogLevel.trace:
        this.trace(message, ...optionalParams);
        break;
      case LogLevel.debug:
        this.debug(message, ...optionalParams);
        break;
      case LogLevel.info:
        this.info(message, ...optionalParams);
        break;
      case LogLevel.warn:
        this.warn(message, ...optionalParams);
        break;
      case LogLevel.error:
        this.error(message, ...optionalParams);
        break;
      default:
        throw new Error(`Log level "${level}" not supported`);
    }

  }

}

Jest test logger

Logger with jest spies for each particular log function.

import { LoggerInterface } from './LoggerInterface';
import { AbstractFunctionLogger } from './AbstractFunctionLogger';

export class JestTestLogger extends AbstractFunctionLogger implements LoggerInterface {

  public trace = jest.fn();
  public info = jest.fn();
  public warn = jest.fn();
  public debug = jest.fn();
  public error = jest.fn();

  public mockReset() {
    this.trace.mockReset();
    this.info.mockReset();
    this.warn.mockReset();
    this.debug.mockReset();
    this.error.mockReset();
  }

}

triviality modules

Logger reference module, so you can reference your module to a non-concrete implementation.

import { Module } from 'triviality';
import { LoggerInterface } from '../LoggerInterface';

abstract class LoggerModule implements Module {

  public abstract logger(): LoggerInterface;

}

export { LoggerModule };

For example you reference the logger module like:

import { Container, Module } from 'triviality';
import { LoggerModule } from 'triviality-logger';
import { HalloService } from './HalloService';

export class MyModule implements Module {

  constructor(private container: Container<LoggerModule>) {

  }

  public halloService() {
    return new HalloService(this.container.logger());
  }

}

And build the container like:

import { ContainerFactory } from 'triviality';
import { DefaultLoggerModule } from 'triviality-logger';
import { MyModule } from './Module/MyModule';

ContainerFactory
  .create()
  .add(DefaultLoggerModule)
  .add(MyModule)
  .build()
  .then((container) => {
    container.halloService().printHallo('Jane');
  });
./node_modules/.bin/ts-node example/defaultLoggerModule.ts 
01/03/2019 8:36:21 PM:Hallo Jane

Thanks

Special thanks to:

  • Eric Pinxteren

Reads

triviality

node-bunyan

ts-log

log4js

typescript-logging

typescript-logging-developer-extension