All files / logger/src transports.ts

100% Statements 17/17
100% Branches 9/9
100% Functions 9/9
100% Lines 17/17

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                            660x 660x 3300x 3290x     4640x       10x   660x             660x 4650x         4650x   660x                   70x     1x 1x                       70x     71x 4156x      
import type { JsonRpcMessage } from '@metamask/kernel-utils';
import type { DuplexStream } from '@metamask/streams';
 
import { logLevels } from './constants.ts';
import { lser } from './stream.ts';
import type { Transport, LogArgs, LogLevel, LogMethod } from './types.ts';
 
/**
 * The console transport for the logger.
 *
 * @param level - The logging level for this instance.
 * @returns A transport function that writes to the console.
 */
export function makeConsoleTransport(level: LogLevel = 'debug'): Transport {
  const baseLevelIdx = logLevels[level];
  const logFn = (method: LogLevel): LogMethod => {
    if (baseLevelIdx <= logLevels[method]) {
      return (...args: unknown[]) => {
        // Ultimately, a console somewhere is an acceptable terminal for logging
        // eslint-disable-next-line no-console
        console[method](...args);
      };
    }
    // eslint-disable-next-line no-empty-function
    return harden(() => {}) as LogMethod;
  };
  const filteredConsole = {
    debug: logFn('debug'),
    info: logFn('info'),
    log: logFn('log'),
    warn: logFn('warn'),
    error: logFn('error'),
  };
  const consoleTransport: Transport = (entry) => {
    const args = [
      ...(entry.tags.length > 0 ? [entry.tags] : []),
      ...(entry.message ? [entry.message] : []),
      ...(entry.data ?? []),
    ] as LogArgs;
    filteredConsole[entry.level](...args);
  };
  return consoleTransport;
}
 
/**
 * The stream transport for the logger. Expects the stream is listening for
 * log entries.
 *
 * @param stream - The stream to write the log entry to.
 * @returns A transport function that writes to the stream.
 */
export const makeStreamTransport = (
  stream: DuplexStream<JsonRpcMessage>,
): Transport => {
  return (entry) => {
    stream
      .write({
        method: 'notify',
        params: ['logger', lser(entry)],
        jsonrpc: '2.0',
      })
      // This is a last resort, but it's better than nothing
      // eslint-disable-next-line no-console
      .catch(console.debug);
  };
};
 
export const makeArrayTransport = (
  target: Parameters<Transport>[0][],
): Transport => {
  return (entry) => {
    target.push(entry);
  };
};