diff --git a/src/config.ts b/src/config.ts index 594d01d..d33cfa4 100644 --- a/src/config.ts +++ b/src/config.ts @@ -121,8 +121,8 @@ export function basicConfig(options: BasicConfigOptions) { const filename = options.filename ?? null; const stream = options.stream ?? null; const filemode = options.filemode ?? 'a'; - const dateformat = options.filemode ?? null; - const style = options.filemode ?? '%'; + const dateformat = options.datefmt ?? null; + const style = options.style ?? '%'; const level = options.level ?? null; if (!Object.keys(STYLES).includes(style)) { @@ -139,64 +139,54 @@ export function basicConfig(options: BasicConfigOptions) { } } - if (handlers.length == 0) { - if (handlers === null && stream && filename) { + if (MANAGER.root.handlers.length > 0 && !force) { + return; + } + + if (handlers.length > 0) { + if (stream || filename) { + throw new ValueError( + "'stream' or 'filename' should not be specified together " + + "with 'handlers'" + ); + } + } + else { + if (stream && filename) { throw new ValueError( "'stream' and 'filename' should not be specified together" ); } - else if (stream || filename) { - throw new ValueError( - "'stream' or 'filename' should not be specified together" + - "with 'handlers'" - ); + if (filename) { + if (filemode.match('b')) { errors = undefined } + else { encoding = 'utf-8' } + + handlers = [new FileHandler({ + filename: filename, + filemode: filemode, + encoding: encoding, + errors: errors + })]; } - - if (handlers === null) { - var h: Handler; - - if (filename) { - if (filemode.match('b')) { errors = undefined } - else { encoding = 'utf-8' } - - h = new FileHandler({ - filename: filename, - filemode: filemode, - 'encoding': encoding, - errors: errors - }); - } - - else { h = new StreamHandler(stream) } - - handlers = [h]; - } - - for (var i = 0; i < handlers.length; i += 1) { - let h = handlers[i]; - - if (h.formatter === null) { - h.formatter = new Formatter({ - fmt: options.format ?? STYLES[style][1], - datefmt: dateformat, - style: style - }); - } - - MANAGER.root.addHandler(h); - } - - if (level !== null) { MANAGER.root.setLevel(level) } - - if (options) { - // runtime interface guard, please let me stay. 🥺 - // the interface does not allow for additional members, but the - // runtime environment has no concept of interfaces. We can stick to - // the original implementation - const keys = Object.keys(options).join(', '); - - throw new ValueError(`Unrecognised argument(s): ${keys}`); + else { + handlers = [new StreamHandler(stream)]; } } + + for (let i = 0; i < handlers.length; i += 1) { + const h = handlers[i]; + + if (h.formatter === null) { + h.formatter = new Formatter({ + fmt: options.format ?? STYLES[style][1], + datefmt: dateformat, + style: style + }); + } + + MANAGER.root.addHandler(h); + } + + if (level !== null) { MANAGER.root.setLevel(level) } }