UNPKG

@athenna/logger

Version:

The Athenna logging solution. Log in stdout, files and buckets.

59 lines (58 loc) 2.18 kB
/** * @athenna/logger * * (c) João Lenon <lenon@athenna.io> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ import { Formatter } from '#src/formatters/Formatter'; import { CliFormatter } from '#src/formatters/CliFormatter'; import { JsonFormatter } from '#src/formatters/JsonFormatter'; import { NoneFormatter } from '#src/formatters/NoneFormatter'; import { SimpleFormatter } from '#src/formatters/SimpleFormatter'; import { MessageFormatter } from '#src/formatters/MessageFormatter'; import { RequestFormatter } from '#src/formatters/RequestFormatter'; import { FormatterExistException } from '#src/exceptions/FormatterExistException'; import { NotFoundFormatterException } from '#src/exceptions/NotFoundFormatterException'; export class FormatterFactory { /** * Formatters of FormatterFactory. */ static { this.formatters = new Map() .set('cli', { Formatter: CliFormatter }) .set('json', { Formatter: JsonFormatter }) .set('none', { Formatter: NoneFormatter }) .set('simple', { Formatter: SimpleFormatter }) .set('message', { Formatter: MessageFormatter }) .set('request', { Formatter: RequestFormatter }); } /** * Return an array with all available formatters. */ static availableFormatters() { const availableFormatters = []; for (const [key] of this.formatters.entries()) { availableFormatters.push(key); } return availableFormatters; } /** * Fabricate a new instance of a formatter. */ static fabricate(formatterName) { const formatterObject = this.formatters.get(formatterName); if (!formatterObject) { throw new NotFoundFormatterException(formatterName); } return new formatterObject.Formatter(); } /** * Creates a new formatter implementation. */ static createFormatter(name, formatter) { if (this.formatters.has(name)) { throw new FormatterExistException(name); } this.formatters.set(name, { Formatter: formatter }); } }