All files / logger environment-common.ts

100% Statements 54/54
97.82% Branches 45/46
100% Functions 5/5
100% Lines 51/51

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 14618x 18x     18x 18x 18x   18x 18x 18x                                                       18x 51x   18x 52x   18x   53x   51x                 2x                   18x         53x 53x     53x 51x 51x 37x 11x 11x 10x 10x     1x         26x   14x   4x         51x 51x 18x 33x   5x         51x 51x 15x 11x 4x   4x             53x     18x       43x 30x 23x     7x 3x       4x     17x    
import { ConsoleErrorLogger } from './console-error-logger.ts';
import { ConsoleLogger } from './console-logger.ts';
import type { Logger, LogLevel } from './definition.ts';
import type { EmitterFormat } from './emitter.ts';
import { isEmitterFormat } from './emitter.ts';
import { isLogLevel } from './level-utils.ts';
import { OFF_LOGGER } from './off-logger.ts';
 
const ENV_LOGGER = 'EMITNLOG_LOGGER';
const ENV_LEVEL = 'EMITNLOG_LEVEL';
const ENV_FORMAT = 'EMITNLOG_FORMAT';
 
/**
 * The options for the `fromEnv` function.
 */
export type EnvironmentLoggerOptions = {
  /**
   * The level to use if the environment variable `EMITNLOG_LEVEL` is not set.
   */
  readonly level?: LogLevel;
 
  /**
   * The format to use if the environment variable `EMITNLOG_FORMAT` is not set.
   */
  readonly format?: EmitterFormat;
 
  /**
   * Returns the fallback logger to use if the environment variable `ENV_LOGGER` is not set.
   *
   * @param level The level to use set, which is `EMITNLOG_LEVEL`, `options.level`, or undefined.
   * @param format The format to use, which is `EMITNLOG_FORMAT`, `options.format`, or undefined.
   * @returns The fallback logger to use or undefined.
   */
  readonly fallbackLogger?: (level?: LogLevel, format?: EmitterFormat) => Logger | undefined;
};
 
type EnvLogger = 'console' | 'console-error' | `file:${string}`;
 
const isEnvLogger = (value: unknown): value is EnvLogger =>
  value === 'console' || value === 'console-error' || (typeof value === 'string' && value.startsWith('file:'));
 
const isEnvHolder = (value: unknown): value is { readonly env: Record<string, string | undefined> } =>
  !!value && typeof value === 'object' && 'env' in value && !!value.env && typeof value.env === 'object';
 
export const toEnv = (): Record<string, string | undefined> | undefined => {
  // eslint-disable-next-line no-undef
  if (typeof process !== 'undefined' && isEnvHolder(process)) {
    // eslint-disable-next-line no-undef
    return process.env;
  }
 
  // This is not working with jest: time for vitest?
  // const meta = import.meta as unknown as { readonly env: Record<string, string | undefined> };
  // if (typeof meta !== 'undefined' && isEnvHolder(meta)) {
  //   return meta.env;
  // }
 
  return undefined;
};
 
type DecodedEnv = {
  readonly envLogger?: EnvLogger;
  readonly envLevel?: LogLevel;
  readonly envFormat?: EmitterFormat;
  readonly envFile?: string;
};
 
export const decodeEnv = (
  env: Record<string, string | undefined> | undefined,
  options?: EnvironmentLoggerOptions,
): DecodedEnv | undefined => {
  let envLogger: EnvLogger | undefined;
  let envLevel: LogLevel | undefined = options?.level;
  let envFormat: EmitterFormat | undefined = options?.format;
  let envFile: string | undefined;
 
  if (env) {
    const envLoggerValue = env[ENV_LOGGER];
    if (isEnvLogger(envLoggerValue)) {
      if (envLoggerValue.startsWith('file:')) {
        const file = envLoggerValue.slice(5);
        if (file) {
          envLogger = envLoggerValue;
          envFile = file;
        } else {
          // eslint-disable-next-line no-undef, no-console
          console.warn(
            `The value of the environment variable '${ENV_LOGGER}' must provide a file path: '${envLoggerValue}'.\nConsult the emitnlog documentation for the list of valid loggers.`,
          );
        }
      } else {
        envLogger = envLoggerValue;
      }
    } else if (envLoggerValue) {
      // eslint-disable-next-line no-undef, no-console
      console.warn(
        `The value of the environment variable '${ENV_LOGGER}' is not a valid logger: '${envLoggerValue}'.\nConsult the emitnlog documentation for the list of valid loggers.`,
      );
    }
 
    const envLevelValue = env[ENV_LEVEL];
    if (isLogLevel(envLevelValue)) {
      envLevel = envLevelValue;
    } else if (envLevelValue) {
      // eslint-disable-next-line no-undef, no-console
      console.warn(
        `The value of the environment variable '${ENV_LEVEL}' is not a valid level: '${envLevelValue}'.\nConsult the emitnlog documentation for the list of valid levels.`,
      );
    }
 
    const envFormatValue = env[ENV_FORMAT];
    if (envFormatValue) {
      if (isEmitterFormat(envFormatValue)) {
        envFormat = envFormatValue;
      } else Eif (envFormatValue) {
        // eslint-disable-next-line no-undef, no-console
        console.warn(
          `The value of the environment variable '${ENV_FORMAT}' is not a valid format: '${envFormatValue}'.\nConsult the emitnlog documentation for the list of valid formats.`,
        );
      }
    }
  }
 
  return envLogger || envLevel || envFormat ? { envLogger, envLevel, envFormat, envFile } : undefined;
};
 
export const createLoggerFromEnv = (
  decodedEnv: DecodedEnv | undefined,
  options: EnvironmentLoggerOptions | undefined,
): Logger => {
  if (decodedEnv) {
    if (decodedEnv.envLogger === 'console') {
      return new ConsoleLogger(decodedEnv.envLevel, decodedEnv.envFormat);
    }
 
    if (decodedEnv.envLogger === 'console-error') {
      return new ConsoleErrorLogger(decodedEnv.envLevel, decodedEnv.envFormat);
    }
 
    // eslint-disable-next-line no-undef, no-console
    console.warn(`The file logger is only supported in Node.js.`);
  }
 
  return options?.fallbackLogger?.(decodedEnv?.envLevel, decodedEnv?.envFormat) ?? OFF_LOGGER;
};