All files / src/logger/emitter console-sink.ts

100% Statements 27/27
64.7% Branches 11/17
100% Functions 3/3
100% Lines 27/27

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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 1111x 1x   1x   1x                                             1x 39x 62x   62x 62x                                             1x 19x 29x   29x 29x                                                             1x 15x 47x   47x   20x 47x   7x 27x   10x 10x   10x 10x 47x  
import { emptyArray } from '../../utils/common/singleton.ts';
import { shouldEmitEntry } from '../implementation/level-utils.ts';
import type { LogFormatter } from './formatter.ts';
import { plainFormatter } from './formatter.ts';
import type { LogSink } from './sink.ts';
import { asLogSink } from './sink.ts';
 
/**
 * Creates a log sink that writes all entries to console.log.
 *
 * This sink sends all log messages to standard output, regardless of their severity level. It's useful when you want
 * all logs to go to stdout for shell redirection or processing.
 *
 * @example Basic usage
 *
 * ```ts
 * import { emitter } from 'emitnlog/logger';
 *
 * const sink = emitter.consoleLogSink();
 * const logger = emitter.createLogger('info', sink);
 *
 * logger.i`This goes to console.log`;
 * logger.e`This also goes to console.log`;
 * ```
 *
 * @param formatter The formatter to use for log entries (default: plainFormatter)
 * @returns A LogSink that writes to console.log
 */
export const consoleLogSink = (formatter: LogFormatter = plainFormatter): LogSink =>
  asLogSink((level, message, args) => {
    const line = formatter(level, message, args);
    // eslint-disable-next-line no-undef, no-console
    console.log(line, ...(args ?? emptyArray()));
  });
 
/**
 * Creates a log sink that writes all entries to console.error.
 *
 * This sink sends all log messages to standard error, regardless of their severity level. It's useful for CLI tools or
 * applications where you want all logging to go to stderr while keeping stdout clean for actual program output.
 *
 * @example Basic usage
 *
 * ```ts
 * import { emitter } from 'emitnlog/logger';
 *
 * const sink = emitter.consoleErrorSink();
 * const logger = emitter.createLogger('info', sink);
 *
 * logger.i`This goes to console.error`;
 * logger.e`This also goes to console.error`;
 * ```
 *
 * @param formatter The formatter to use for log entries (default: plainFormatter)
 * @returns A LogSink that writes to console.error
 */
export const consoleErrorSink = (formatter: LogFormatter = plainFormatter): LogSink =>
  asLogSink((level, message, args) => {
    const line = formatter(level, message, args);
    // eslint-disable-next-line no-undef, no-console
    console.error(line, ...(args ?? emptyArray()));
  });
 
/**
 * Creates a log sink that routes entries to different console methods based on severity.
 *
 * This sink provides intelligent routing to give the most appropriate console output:
 *
 * - Trace, debug → console.debug
 * - Info, notice → console.log
 * - Warning → console.warn
 * - Error, critical, alert, emergency → console.error
 *
 * This is the most sensible default for most applications as it allows terminal emulators and development tools to
 * apply appropriate styling and filtering.
 *
 * @example Basic usage
 *
 * ```ts
 * import { emitter } from 'emitnlog/logger';
 *
 * const sink = emitter.consoleByLevelSink();
 * const logger = emitter.createLogger('info', sink);
 *
 * logger.i`Goes to console.log`;
 * logger.w`Goes to console.warn`;
 * logger.e`Goes to console.error`;
 * ```
 *
 * @param formatter The formatter to use for log entries (default: plainFormatter)
 * @returns A LogSink that routes to appropriate console methods
 */
export const consoleByLevelSink = (formatter: LogFormatter = plainFormatter): LogSink =>
  asLogSink((level, message, args) => {
    const line = formatter(level, message, args);
 
    if (shouldEmitEntry('error', level)) {
      // eslint-disable-next-line no-undef, no-console
      console.error(line, ...(args ?? emptyArray()));
    } else if (shouldEmitEntry('warning', level)) {
      // eslint-disable-next-line no-undef, no-console
      console.warn(line, ...(args ?? emptyArray()));
    } else if (shouldEmitEntry('info', level)) {
      // eslint-disable-next-line no-undef, no-console
      console.log(line, ...(args ?? emptyArray()));
    } else {
      // eslint-disable-next-line no-undef, no-console
      console.debug(line, ...(args ?? emptyArray()));
    }
  });