All files / src/classes CoreLogChannel.ts

100% Statements 21/21
68.75% Branches 11/16
100% Functions 10/10
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79                                        24x 24x 1x 23x 21x   2x         1x       11x 10x         1037x 1037x         13x 13x         13x 13x         6x 6x         25x 25x         9x       4x      
import type { IHaveLogger, ILogChannel } from '../lib';
import CoreLogger, { LogLevel } from './CoreLogger';
import CoreError from './CoreError';
 
/**
 * Provides a channel‑specific wrapper around a {@link CoreLogger}.
 *
 * Each channel is identified by a string name.  The class forwards all
 * logging calls to an underlying {@link CoreLogger} instance if it
 * exists and if the desired log level is enabled.  If no logger is
 * attached, all logging methods become no‑ops.
 *
 * @implements {ILogChannel}
 */
export default class CoreLogChannel implements ILogChannel {
  logger: CoreLogger | null;
 
  protected channel: string;
 
  constructor(channel: string, target: CoreLogger | IHaveLogger | null) {
    this.channel = channel;
    if (target instanceof CoreLogger) {
      this.logger = target;
    } else if (target?.getLogger) {
      this.logger = target.getLogger();
    } else {
      this.logger = null;
    }
  }
 
  setLogger(logger: CoreLogger) {
    this.logger = logger;
  }
 
  log(...ags: unknown[]): void {
    if (this.logger?.isOnLevel(LogLevel.INFO)) {
      this.logger.log(this.channel, ...ags);
    }
  }
 
  debug(...ags: unknown[]): void {
    Eif (this.logger?.isOnLevel(LogLevel.DEBUG)) {
      this.logger.debug(this.channel, ...ags);
    }
  }
 
  info(...ags: unknown[]): void {
    Eif (this.logger?.isOnLevel(LogLevel.INFO)) {
      this.logger.info(this.channel, ...ags);
    }
  }
 
  error(...ags: unknown[]): void {
    Eif (this.logger?.isOnLevel(LogLevel.ERROR)) {
      this.logger.error(this.channel, ...ags);
    }
  }
 
  warn(...ags: unknown[]): void {
    Eif (this.logger?.isOnLevel(LogLevel.WARN)) {
      this.logger.warn(this.channel, ...ags);
    }
  }
 
  verbose(...ags: unknown[]): void {
    Eif (this.logger?.isOnLevel(LogLevel.VERBOSE)) {
      this.logger.verbose(this.channel, ...ags);
    }
  }
 
  lError(message: string): Error {
    return new CoreError(message, this);
  }
 
  getName(): string {
    return this.channel;
  }
}