Skip to content
Manolo Edge edited this page Aug 29, 2020 · 7 revisions

Documentation for loggin.Logger

This document will explain how the Logger class works, how to use it and how to extend it!

Logger is the most important class/object in Loggin'JS as it's the primary interface used, you configure and log through this class.

Loggers can have multiple Notifiers which are the things in charge of "notifying" the Logs to some output channel, i.e: to the console, to a file, etc.... Check the available pre-made Notifiers for the complete list, or if you don't find the one you need you can create a custom one!!.

Each logger has what is called a Severities, used for filtering logs, and outputting the ones that match the current Severity, by default it uses the loggin priority level from standard RFC3164, Loggin'JS does not have any other level options available for now, but could be implemented really easily, check the plugins page for more info :)

Importing

let { Logger } = require('loggin-js');
// Or
let Logger = require('loggin-js/lib/logger');

Interface

export class Logger {
  constructor(options: LoggerOptions);

  /**
   * Get a logger from the registered ones
   */
  static get(opts: string, args: any): Logger;
  static merge(loggers, opts: any): Logger;

  /**
   * Searches and tries to find a logger
   */
  static search(value: any): Logger;

 /**
   * Register a new Logger, with a default Notifier attached, can then be used as any other Logger
   * 
   * @example
   * Logger.register('console', 'Console');
   * ...
   * 
   * logger.logger('console');
   */
  static register(name: string, notifier: string);

  enabled(enabled?: boolean): this;

  user(user?: string): this;
  channel(channel?: boolean): this;
  level(level?: number | string | Severity): this;
  formatter(name?: SupportedFormatters): this;
  color(enable: boolean): this;
  lineNumbers(show: boolean): this;

  canLog(severity: Severity): boolean;

  notifier(...notifier: Notifier[]): this;
  hasNotifier(name: string): boolean;
  getNotifier(name: string): Notifier;
  setNotifiers(notifiers: Notifier[]): this;

  _options: LoggerOptions;

  /**
   * Clone the logger
   */
  clone(options: LoggerOptions): Logger;

  /**
   * Alias for clone
   */
  fork(options: LoggerOptions): Logger;

  /**
   * Log a message to set notifier
   * @param message - message to be logged
   * @param data - some data to log
   * @param options - overwrite options for that specific log
   */
  log(
    message: string,
    data?: any,
    options?: LogOptions
  ): this;

  /**
   * @description Logs with severity set to DEBUG
   * @param message - message to be logged
   * @param data - some data to log
   * @param options - overwrite options for that specific log
   */
  debug(
    message: string,
    data?: any,
    options?: LogOptions
  ): this;

  /**
   * @alias for debug
   */
  default(
    message: string,
    data?: any,
    options?: LogOptions
  ): this;

  /**
   * @description Logs with severity set to WARNING
   * @param message - message to be logged
   * @param data - some data to log
   * @param options - overwrite options for that specific log
   */
  warning(
    message: string,
    data?: any,
    options?: LogOptions
  ): this;

  /**
   * @description Logs with severity set to EMERGENCY
   * @param message - message to be logged
   * @param data - some data to log
   * @param options - overwrite options for that specific log
   */
  emergency(
    message: string,
    data?: any,
    options?: LogOptions
  ): this;

  /**
   * @description Logs with severity set to CRITICAL
   * @param message - message to be logged
   * @param data - some data to log
   * @param options - overwrite options for that specific log
   */
  critical(
    message: string,
    data?: any,
    options?: LogOptions
  ): this;

  /**
   * @description Logs with severity set to ERROR
   * @param message - message to be logged
   * @param data - some data to log
   * @param options - overwrite options for that specific log
   */
  error(
    message: string,
    data?: any,
    options?: LogOptions
  ): this;

  /**
   * @description Logs with severity set to NOTICE
   * @param message - message to be logged
   * @param data - some data to log
   * @param options - overwrite options for that specific log
   */
  notice(
    message: string,
    data?: any,
    options?: LogOptions
  ): this;


  /**
   * @description Logs with severity set to INFO
   * @param message - message to be logged
   * @param data - some data to log
   * @param options - overwrite options for that specific log
   */
  info(
    message: string,
    data?: any,
    options?: LogOptions
  ): this;

  /**
   * @description Logs with severity set to SILLY
   * @param message - message to be logged
   * @param data - some data to log
   * @param options - overwrite options for that specific log
   */
  silly(
    message: string,
    data?: any,
    options?: LogOptions
  ): this;

}

Clone this wiki locally