All files / src/logger factory.ts

97.84% Statements 91/93
95.08% Branches 58/61
100% Functions 30/30
97.84% Lines 91/93

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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271    1x   1x 1x   1x               1x                                                                               1x 34x 34x 34x 34x                                                             1x 15x 15x 15x 15x                                                                 1x 8x 8x 8x 8x                                         1x 110x 110x 45x   110x 40x   110x 13x   110x 12x   110x     110x 110x                                                               1x 178x 178x 178x 178x   178x 532x 408x 408x   124x 124x 124x 532x   178x 532x 532x 124x 124x 532x 532x   178x 178x 256x 256x   178x 126x 126x 126x   178x 178x 178x 178x 178x 178x 178x 178x 178x 178x 178x 178x 178x 178x 178x 178x 178x 178x 178x   178x 178x 178x   178x 180x 180x   178x 1x 1x   178x 178x  
import type { Simplify } from 'type-fest';
 
import { exhaustiveCheck } from '../utils/common/exhaustive-check.ts';
import type { Logger, LogLevel } from './definition.ts';
import { consoleByLevelSink, consoleErrorSink, consoleLogSink } from './emitter/console-sink.ts';
import { createLogger } from './emitter/emitter-logger.ts';
import type { LogFormatter } from './emitter/formatter.ts';
import {
  basicFormatter,
  colorfulFormatter,
  jsonCompactFormatter,
  jsonPrettyFormatter,
  plainFormatter,
} from './emitter/formatter.ts';
import type { BaseLoggerOptions } from './implementation/base-logger.ts';
import { injectPrefixInformation, isPrefixedLogger } from './prefixed-logger.ts';
 
/**
 * The format of the emitted lines. The possible values are:
 *
 * - 'plain': The line is emitted as a plain string.
 * - 'colorful': The line is emitted with ANSI color codes.
 * - 'json-compact': The line is emitted as a JSON string.
 * - 'json-pretty': The line is emitted as a formatted JSON string.
 */
export type LogFormat = 'plain' | 'colorful' | 'json-compact' | 'json-pretty';
 
/**
 * Creates a logger that emits log messages to standard output (console.log) with optional formatting.
 *
 * All log entries, regardless of severity level, are sent to console.log. For routing based on severity, use
 * {@link createConsoleByLevelLogger} instead.
 *
 * @example Basic usage
 *
 * ```ts
 * import { createConsoleLogLogger } from 'emitnlog/logger';
 *
 * const logger = createConsoleLogLogger();
 * logger.info('Application started');
 * logger.error('This also goes to console.log');
 * ```
 *
 * @example With custom level and format
 *
 * ```ts
 * const logger = createConsoleLogLogger('debug', 'json-compact');
 * logger.debug('Debug information', { userId: 123 });
 * ```
 *
 * @param level The minimum log level (default: 'info')
 * @param format The output format (default: 'colorful')
 * @param options Additional logger configuration options
 * @returns A logger that writes all entries to console.log
 */
export const createConsoleLogLogger = (
  level: LogLevel = 'info',
  format: LogFormat = 'colorful',
  options?: BaseLoggerOptions,
): Logger => createLogger(level, consoleLogSink(toLogFormatter(format)), options);
 
/**
 * Creates a logger that emits log messages to standard error (console.error) with optional formatting.
 *
 * All log entries, regardless of severity level, are sent to console.error. This is useful when you want all logs to go
 * to stderr for proper shell redirection or when building CLI tools.
 *
 * @example Basic usage
 *
 * ```ts
 * import { createConsoleErrorLogger } from 'emitnlog/logger';
 *
 * const logger = createConsoleErrorLogger();
 * logger.error('Critical error occurred');
 * logger.info('This info message also goes to stderr');
 * ```
 *
 * @example Production configuration
 *
 * ```ts
 * const logger = createConsoleErrorLogger('error', 'json-compact');
 * logger.info("This won't be logged (below error level)");
 * logger.error('This will be logged as JSON to stderr');
 * ```
 *
 * @param level The minimum log level (default: 'info')
 * @param format The output format (default: 'colorful')
 * @param options Additional logger configuration options
 * @returns A logger that writes all entries to console.error
 */
export const createConsoleErrorLogger = (
  level: LogLevel = 'info',
  format: LogFormat = 'colorful',
  options?: BaseLoggerOptions,
): Logger => createLogger(level, consoleErrorSink(toLogFormatter(format)), options);
 
/**
 * Creates a logger with intelligent console routing based on message severity.
 *
 * This provides the most sensible console behavior for most applications:
 *
 * - Lower severity (trace, debug, info, notice) → console.log
 * - Higher severity (warning, error, critical, alert, emergency) → console.error
 *
 * @example Basic usage
 *
 * ```ts
 * import { createConsoleByLevelLogger } from 'emitnlog/logger';
 *
 * const logger = createConsoleByLevelLogger();
 * logger.info('Normal operation'); // Goes to console.log
 * logger.error('Error occurred'); // Goes to console.error
 * logger.warning('Be careful'); // Goes to console.error
 * ```
 *
 * @example Production configuration
 *
 * ```ts
 * const logger = createConsoleByLevelLogger('warning', 'json-compact');
 * // Only warnings and above are logged, all to console.error as JSON
 * ```
 *
 * @param level The minimum log level (default: 'info')
 * @param format The output format (default: 'colorful')
 * @param options Additional logger configuration options
 * @returns A logger with automatic console routing based on severity
 */
export const createConsoleByLevelLogger = (
  level: LogLevel = 'info',
  format: LogFormat = 'colorful',
  options?: BaseLoggerOptions,
): Logger => createLogger(level, consoleByLevelSink(toLogFormatter(format)), options);
 
/**
 * Converts a format string into a corresponding log formatter function.
 *
 * This utility function maps the string-based format options to their corresponding formatter implementations. Used
 * internally by the factory functions but exported for custom logger implementations.
 *
 * @example
 *
 * ```ts
 * import { toLogFormatter } from 'emitnlog/logger';
 *
 * const formatter = toLogFormatter('json-compact');
 * const formatted = formatter('info', 'Hello world', []);
 * // formatted is a JSON string
 * ```
 *
 * @param format The format type to convert
 * @returns A LogFormatter function that formats entries according to the specified format
 */
export const toLogFormatter = (format: LogFormat): LogFormatter => {
  switch (format) {
    case 'colorful':
      return colorfulFormatter;
 
    case 'plain':
      return plainFormatter;
 
    case 'json-compact':
      return jsonCompactFormatter;
 
    case 'json-pretty':
      return jsonPrettyFormatter;
 
    default:
      exhaustiveCheck(format);
      return basicFormatter;
  }
};
 
type UnionToIntersection<U> = (U extends unknown ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
type ExtendedShape<Ms extends readonly object[]> = UnionToIntersection<Ms[number]>;
type ExtendedLogger<L extends Logger, Ms extends readonly object[]> = Simplify<L & ExtendedShape<Ms>>;
 
/**
 * Extends a logger with additional methods and properties while preserving pending arguments functionality.
 *
 * This function allows you to add custom methods to any logger instance. The extended logger maintains the ability to
 * chain arguments using the `args()` method, ensuring that pending arguments are properly forwarded with each log
 * operation.
 *
 * @example Basic extension
 *
 * ```ts
 * import { asExtendedLogger, createConsoleLogLogger } from 'emitnlog/logger';
 *
 * const baseLogger = createConsoleLogLogger();
 * const extended = asExtendedLogger(baseLogger, {
 *   logWithTimestamp: (message: string) => {
 *     baseLogger.info(`[${new Date().toISOString()}] ${message}`);
 *   },
 * });
 *
 * extended.logWithTimestamp('Custom log message');
 * ```
 *
 * @param logger The base logger to extend
 * @param extensions Objects containing additional methods and properties to add to the logger
 * @returns A new logger with all the original methods plus the provided extensions
 */
export const asExtendedLogger = <L extends Logger, Ms extends readonly object[]>(
  logger: L,
  ...extensions: Ms
): ExtendedLogger<L, Ms> => {
  let pendingArgs: unknown[] = [];
 
  const consumePendingArgs = (): readonly unknown[] | undefined => {
    if (!pendingArgs.length) {
      return undefined;
    }
 
    const args = pendingArgs;
    pendingArgs = [];
    return args;
  };
 
  const runLogOperation = (operation: () => void) => {
    const currentArgs = consumePendingArgs();
    if (currentArgs) {
      logger.args(...currentArgs);
    }
    operation();
  };
 
  let extendedLogger: Logger = {
    get level() {
      return logger.level;
    },
 
    args: (...args) => {
      pendingArgs.push(...args);
      return extendedLogger;
    },
 
    trace: (message, ...args) => runLogOperation(() => logger.trace(message, ...args)),
    t: (strings, ...values) => runLogOperation(() => logger.t(strings, ...values)),
    debug: (message, ...args) => runLogOperation(() => logger.debug(message, ...args)),
    d: (strings, ...values) => runLogOperation(() => logger.d(strings, ...values)),
    info: (message, ...args) => runLogOperation(() => logger.info(message, ...args)),
    i: (strings, ...values) => runLogOperation(() => logger.i(strings, ...values)),
    notice: (message, ...args) => runLogOperation(() => logger.notice(message, ...args)),
    n: (strings, ...values) => runLogOperation(() => logger.n(strings, ...values)),
    warning: (input, ...args) => runLogOperation(() => logger.warning(input, ...args)),
    w: (strings, ...values) => runLogOperation(() => logger.w(strings, ...values)),
    error: (input, ...args) => runLogOperation(() => logger.error(input, ...args)),
    e: (strings, ...values) => runLogOperation(() => logger.e(strings, ...values)),
    critical: (input, ...args) => runLogOperation(() => logger.critical(input, ...args)),
    c: (strings, ...values) => runLogOperation(() => logger.c(strings, ...values)),
    alert: (input, ...args) => runLogOperation(() => logger.alert(input, ...args)),
    a: (strings, ...values) => runLogOperation(() => logger.a(strings, ...values)),
    emergency: (input, ...args) => runLogOperation(() => logger.emergency(input, ...args)),
    em: (strings, ...values) => runLogOperation(() => logger.em(strings, ...values)),
    log: (level, message, ...args) => runLogOperation(() => logger.log(level, message, ...args)),
 
    flush: logger.flush ? () => logger.flush?.() : undefined,
    close: logger.close ? () => logger.close?.() : undefined,
  };
 
  for (const m of extensions) {
    Object.assign(extendedLogger, m);
  }
 
  if (isPrefixedLogger(logger)) {
    extendedLogger = injectPrefixInformation(logger, extendedLogger);
  }
 
  return extendedLogger as ExtendedLogger<L, Ms>;
};