All files / logger environment-logger.ts

50% Statements 3/6
100% Branches 0/0
0% Functions 0/1
40% Lines 2/5

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    18x                                                                                                                                                                               18x          
import type { Logger } from './definition.ts';
import type { EnvironmentLoggerOptions } from './environment-common.ts';
import { createLoggerFromEnv, decodeEnv, toEnv } from './environment-common.ts';
 
/**
 * Returns the logger to use based on the environment variables.
 *
 * The environment variables are:
 *
 * ```
 * EMITNLOG_LOGGER: The logger to use.
 * The possible values are
 *   - `console`: The console logger.
 *   - `console-error`: The console error logger.
 *   - `file:<path>`: The file logger with the (required) file path information (Node.js only)
 *
 * EMITNLOG_LEVEL: The level to use.
 * The possible values are
 *   - `trace`
 *   - `debug`
 *   - `info`
 *   - `notice`
 *   - `warning`
 *   - `error`
 *   - `critical`
 *   - `alert`
 *   - `emergency`
 *
 * EMITNLOG_FORMAT: The format to use.
 * The possible values are
 *   - `plain`
 *   - `colorful`
 *   - `json`
 *   - `unformatted-json`
 * ```
 *
 * If a environment variable is not set, the associated value in `options` is used.
 *
 * @example
 *
 * ```typescript
 * import { fromEnv } from 'emitnlog/logger/environment';
 *
 * // Basic usage - uses environment variables if set, otherwise returns OFF_LOGGER
 * const logger = fromEnv();
 * ```
 *
 * @example
 *
 * ```typescript
 * import { fromEnv } from 'emitnlog/logger/environment';
 *
 * // With fallback options when environment variables are not set
 * const logger = fromEnv({
 *   // Used if EMITNLOG_LEVEL is not set
 *   level: 'debug',
 *
 *   // Used if EMITNLOG_FORMAT is not set
 *   format: 'plain',
 * });
 * ```
 *
 * @example
 *
 * ```typescript
 * import { fromEnv } from 'emitnlog/logger/environment';
 *
 * // With a custom fallback logger
 * const logger = fromEnv({
 *   // Used if EMITNLOG_LOGGER is not set
 *   fallbackLogger: (level, format) => new CustomLogger(level, format),
 * });
 * ```
 *
 * @example
 *
 * ```typescript
 * import { fromEnv } from 'emitnlog/logger/environment';
 *
 * // Using console logger with info level and colorful format
 * process.env.EMITNLOG_LOGGER = 'console';
 * process.env.EMITNLOG_LEVEL = 'info';
 * process.env.EMITNLOG_FORMAT = 'colorful';
 * const logger = fromEnv();
 * logger.info('Hello, world!'); // Will output with colors to console
 * ```
 *
 * @param options The options to use.
 * @returns The logger to use.
 */
export const fromEnv = (options?: EnvironmentLoggerOptions): Logger => {
  const env = toEnv();
  const decodedEnv = decodeEnv(env, options);
  return createLoggerFromEnv(decodedEnv, options);
};