Repository URL to install this package:
|
Version:
0.7.10 ▾
|
// (c) Copyright 2023 Supertenant Ltd. - all rights reserved.
// See LICENSE file in project root for license terms.
"use strict";const superbrain=require("@supertenant/superbrain")["superbrain"],consts=require("@supertenant/superconsts");let parentLogger=null;const registry={},dedupKeys={};let dedupErrorLogger=null;const DedupLogPeriod=36e5,consoleLogger={debug:function(...args){console.log(...args),superbrain.log(consts.BrainLogLevel.Debug,args.join(" "))},info:function(...args){console.log(...args),superbrain.log(consts.BrainLogLevel.Info,args.join(" "))},warn:function(...args){console.warn(...args),superbrain.log(consts.BrainLogLevel.Warn,args.join(" "))},error:function(...args){console.error(...args),superbrain.log(consts.BrainLogLevel.Error,args.join(" "))}};function hasLoggingFunctions(logger){return"function"==typeof logger.debug&&"function"==typeof logger.info&&"function"==typeof logger.warn&&"function"==typeof logger.error}exports.init=function(config={}){parentLogger=config.logger&&"function"==typeof config.logger.child&&config.logger.fields&&1===config.logger.fields.__in?config.logger:config.logger&&"function"==typeof config.logger.child?config.logger.child({module:"@supertenant/core",__in:1}):config.logger&&hasLoggingFunctions(config.logger)?config.logger:consoleLogger,Object.keys(registry).forEach(loggerName=>{(0,registry[loggerName])(exports.getLogger(loggerName))})},exports.getLogger=function(loggerName,reInitFn){parentLogger||exports.init({});let logger;if(logger="function"==typeof parentLogger.child?parentLogger.child({module:loggerName}):parentLogger,reInitFn){if(registry[loggerName])throw new Error(`Duplicate logger name: ${loggerName}.`);registry[loggerName]=reInitFn}return logger},exports.superbrainLogger=function(level,msg){superbrain.log(level,msg)},exports.dedupReportError=function(dedupKey,...args){var now=Date.now(),ts=dedupKeys[dedupKey];null!=ts&&now-DedupLogPeriod<ts||(null===dedupErrorLogger&&(dedupErrorLogger=exports.getLogger("DedupError")),dedupKeys[dedupKey]=now,ts=(new Error).stack,dedupErrorLogger.error({dedupKey:dedupKey,stack:ts},...args))};