All files / logger/src transports.ts

100% Statements 17/17
100% Branches 11/11
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 76 77 78 79 80 81 82 83 84 85                                                745x 745x 745x 3725x 3715x     4789x       10x   745x             745x 4799x         4799x                     65x     1x 1x                       65x     73x 4206x      
import type { JsonRpcMessage } from '@metamask/kernel-utils';
import type { DuplexStream } from '@metamask/streams';
 
import { logLevels } from './constants.ts';
import { lser } from './stream.ts';
import { hasTags } from './tags.ts';
import type { Transport, LogArgs, LogLevel, LogMethod } from './types.ts';
 
type ConsoleTransportOptions = {
  level?: LogLevel;
  tags?: boolean;
};
 
/**
 * The console transport for the logger.
 *
 * @param options - Options for the console transport.
 * @param options.level - The logging level for this instance (default: `'debug'`).
 * @param options.tags - Whether to include tags in the output (default: `false`).
 * @returns A transport function that writes to the console.
 */
export function makeConsoleTransport(
  options: ConsoleTransportOptions = {},
): Transport {
  const { level = 'debug', tags = false } = options;
  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'),
  };
  return (entry) => {
    const args = [
      ...(hasTags(tags, entry) ? [entry.tags] : []),
      ...(entry.message ? [entry.message] : []),
      ...(entry.data ?? []),
    ] as LogArgs;
    filteredConsole[entry.level](...args);
  };
}
 
/**
 * 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);
  };
};