UNPKG

@augment-vir/common

Version:

A collection of augments, helpers types, functions, and classes for any JavaScript environment.

60 lines (59 loc) 1.82 kB
import { mapEnumToObject } from '../object/map-enum.js'; import { mergeDefinedProperties } from '../object/merge-defined-properties.js'; import { defaultLogColorConfig, LogColorKey, LogOutputType } from './log-colors.js'; import { toLogString } from './log-string.js'; /** * Default implementation of {@link LoggerOptions}. * * @category Log : Util * @category Package : @augment-vir/common * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common) */ export const defaultLoggerOptions = { colorConfig: defaultLogColorConfig, omitColors: false, }; /** * A default {@link Logger} that simply does nothing. * * @category Log * @category Package : @augment-vir/common * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common) */ export const emptyLog = createLogger({ [LogOutputType.Error]() { }, [LogOutputType.Standard]() { }, }); /** * Creates a custom {@link Logger}. * * @category Log * @category Package : @augment-vir/common * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common) */ export function createLogger(logWriters, optionsOverride) { const options = mergeDefinedProperties(defaultLoggerOptions, optionsOverride); function writeLog(params) { logWriters[options.colorConfig[params.colorKey].logType](toLogString({ ...params, options, })); } const loggerLogs = mapEnumToObject(LogColorKey, (colorKey) => { return (...args) => writeLog({ args, colorKey, }); }); return { ...loggerLogs, if(condition) { if (condition) { return loggerLogs; } else { return emptyLog; } }, }; }