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 | 4x 56x 56x 56x | import type { Logger } from '../definition.ts';
import type { EnvironmentLoggerOptions } from '../environment/shared.ts';
import { createLoggerFromEnv, decodeEnv, toEnv } from '../environment/shared.ts';
import { createFileLogger } from './factory.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-log`: The console log logger.
* - `console-error`: The console error logger.
* - `console-level`: The console by level logger.
* - `file:<path>`: The file logger with the (required) file path information (NodeJS only)
* - `file:date:<path>`: Same as 'file:<path>' however the local date is prefixed to the file name (NodeJS 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`
* - `ndjson`
* - `json-pretty`
* ```
*
* If a environment variable is not set, the associated value in `options` is used.
*
* @example
*
* ```ts
* import { fromEnv } from 'emitnlog/logger';
*
* // Basic usage - uses environment variables if set, otherwise returns OFF_LOGGER
* const logger = fromEnv();
* ```
*
* @example
*
* ```ts
* import { fromEnv } from 'emitnlog/logger';
*
* // 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
*
* ```ts
* import { fromEnv } from 'emitnlog/logger';
*
* // With a custom fallback logger
* const logger = fromEnv({
* // Used if EMITNLOG_LOGGER is not set
* fallbackLogger: (level, format) => new CustomLogger(level, format),
* });
* ```
*
* @example
*
* ```ts
* import { fromEnv } from 'emitnlog/logger';
*
* // Using console logger with info level and colorful format
* process.env.EMITNLOG_LOGGER = 'console-log';
* process.env.EMITNLOG_LEVEL = 'info';
* process.env.EMITNLOG_FORMAT = 'colorful';
* const logger = fromEnv();
* logger.i`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 decodedEnv?.envFile
? createFileLogger(decodedEnv.envFile, {
datePrefix: decodedEnv.envDatePrefix,
level: decodedEnv.envLevel,
format: decodedEnv.envFormat,
})
: createLoggerFromEnv(decodedEnv, options);
};
|