UNPKG

@metamask/logger

Version:

A lightweight logging package using @metamask/streams

50 lines 1.59 kB
import { consoleTransport } from "./transports.mjs"; /** * The default options for the logger. */ export const DEFAULT_OPTIONS = { transports: [], level: 'info', tags: [], }; /** * Parses the options for the logger. * * @param options - The options for the logger. * @returns The parsed options. */ export const parseOptions = (options) => { // The default case catches whatever is not explicitly handled below. // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check switch (typeof options) { case 'object': return options; case 'string': return { tags: [options], transports: [consoleTransport] }; case 'undefined': return { transports: [consoleTransport] }; default: throw new Error('Invalid logger options'); } }; /** * Returns a copy of an array containing only its unique values. * * @param array - The array to filter. * @returns The array, without duplicate values. */ export const unique = (array) => { return array.filter((element, index, self) => self.indexOf(element) === index); }; /** * Merges multiple logger options into a single options object. * * @param options - The options to merge. * @returns The merged options. */ export const mergeOptions = (...options) => options.reduce((acc, option) => ({ transports: unique([...acc.transports, ...(option.transports ?? [])]), level: option.level ?? acc.level, tags: unique([...acc.tags, ...(option.tags ?? [])]), }), DEFAULT_OPTIONS); //# sourceMappingURL=options.mjs.map