Socket
Socket
Sign inDemoInstall

ts-log

Package Overview
Dependencies
0
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    ts-log

Abstract logger TypeScript interface with a dummy logger that does nothing, useful for libraries.


Version published
Weekly downloads
1.9M
decreased by-20.83%
Maintainers
1
Install size
120 kB
Created
Weekly downloads
 

Package description

What is ts-log?

The ts-log npm package provides a simple logging interface for TypeScript applications. It allows developers to create custom loggers and use them throughout their codebase. The package defines a minimal set of logging methods that can be implemented to create a logger compatible with the ts-log interface.

What are ts-log's main functionalities?

Creating a custom logger

This feature allows you to create a custom logger and use various methods like debug, info, warn, and error to log messages at different levels.

{"createLogger": "const { Logger } = require('ts-log');\nconst myLogger = new Logger();\nmyLogger.debug('This is a debug message.');\nmyLogger.info('This is an info message.');\nmyLogger.warn('This is a warning message.');\nmyLogger.error('This is an error message.');"}

Extending the logger

This feature demonstrates how to extend the Logger class to add custom logging methods such as a trace method.

{"extendLogger": "const { Logger } = require('ts-log');\nclass MyLogger extends Logger {\n  trace(message) {\n    // Custom trace method implementation\n    console.log('TRACE: ' + message);\n  }\n}\nconst myLogger = new MyLogger();\nmyLogger.trace('This is a trace message.');"}

Other packages similar to ts-log

Readme

Source

TypeScript Abstract Logger

Travis Coverage Downloads Version License

Abstract logger TypeScript interface along with a dummy logger that does nothing.

Useful for libraries wanting to provide a pluggable logger that does nothing by default (or provide your own default such as bunyan).

  • Matches the built-in console that can be used directly.
  • Also matches bunyan.
  • Provides usage and custom logger example.
  • Written in TypeScript, no need for extra typings.
  • No dependencies, 24 LOC with comments, 100% test coverage.

Installation

This package is distributed via npm

npm install ts-log
yarn add ts-log

Example

import { dummyLogger, Logger } from "ts-log";
import * as fs from "fs";

// example class that uses the logger
class Calculator {
  // accept the logger in the constructor, defaulting to dummy logger that does nothing
  public constructor(private readonly log: Logger = dummyLogger) {}

  public sum(a: number, b: number) {
    const result = a + b;

    // call the logger
    this.log.info(`summing ${a} + ${b} = ${result}`, a, b, result);

    return result;
  }
}

// example custom logger that logs to a file
class FileLogger implements Logger {
  private readonly fd: number;

  public constructor(filename: string) {
    this.fd = fs.openSync(filename, "a");
  }

  public trace(message?: any, ...optionalParams: any[]): void {
    this.append("TRACE", `${message} ${JSON.stringify(optionalParams)}`);
  }

  public debug(message?: any, ...optionalParams: any[]): void {
    this.append("DEBUG", `${message} ${JSON.stringify(optionalParams)}`);
  }

  public info(message?: any, ...optionalParams: any[]): void {
    this.append("INFO ", `${message} ${JSON.stringify(optionalParams)}`);
  }

  public warn(message?: any, ...optionalParams: any[]): void {
    this.append("WARN ", `${message} ${JSON.stringify(optionalParams)}`);
  }

  public error(message?: any, ...optionalParams: any[]): void {
    this.append("ERROR", `${message} ${JSON.stringify(optionalParams)}`);
  }

  private append(type: string, message: string) {
    fs.writeSync(this.fd, `${new Date().toISOString()} ${type} ${message}\n`);
  }
}

// don't define a logger, defaults to dummy logger that does nothing
const calculator1 = new Calculator();

// use the built-in console as the logger
const calculator2 = new Calculator(console);

// use the custom file logger
const calculator3 = new Calculator(new FileLogger("log.txt"));

// run the calculator
calculator1.sum(2, 3);
calculator2.sum(-4, 1);
calculator3.sum(6, 3);

Commands

  • npm start to start the example application.
  • npm run build to build the production version.
  • npm run test to run tests.
  • npm run coverage to gather code coverage.
  • npm run lint to lint the codebase.
  • npm run prettier to run prettier.
  • npm run validate to run all pre-commit checks (prettier, build, lint, test)

Keywords

FAQs

Last updated on 16 Sep 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc