summaryrefslogtreecommitdiff
path: root/node_modules/roarr/dist/factories/createLogger.js.map
blob: 4c8854cbd880f51ae280c71e4d8007c18dada425 (plain)
1
{"version":3,"sources":["../../src/factories/createLogger.js"],"names":["globalThis","domain","environmentIsNode","require","getParentDomainContext","parentRoarrContexts","currentDomain","process","parentDomain","roarr","context","push","domainContext","parentRoarrContext","getFirstParentDomainContext","createLogger","onMessage","parentContext","log","a","b","c","d","e","f","g","h","i","k","time","Date","now","sequence","ROARR","message","args","values","Object","keys","map","key","hasOnlyOneParameterValued","reduce","accumulator","value","TypeError","JSON","parse","version","child","getContext","adopt","routine","adoptedDomain","create","run","logLevel","logLevels"],"mappings":";;;;;;;AAEA;;AACA;;AACA;;AACA;;AAGA;;;;;;;;;;;;AAUA,MAAMA,UAAU,GAAG,0BAAnB;AAEA,IAAIC,MAAJ;;AAEA,IAAIC,mBAAJ,EAAuB;AACrB;AACAD,EAAAA,MAAM,GAAGE,OAAO,CAAC,QAAD,CAAhB;AACD;;AAED,MAAMC,sBAAsB,GAAG,MAAM;AACnC,MAAI,CAACH,MAAL,EAAa;AACX,WAAO,EAAP;AACD;;AAED,QAAMI,mBAAmB,GAAG,EAA5B;AAEA,MAAIC,aAAa,GAAGC,OAAO,CAACN,MAA5B,CAPmC,CASnC;;AACA,MAAI,CAACK,aAAD,IAAkB,CAACA,aAAa,CAACE,YAArC,EAAmD;AACjD,WAAO,EAAP;AACD;;AAED,SAAOF,aAAa,IAAIA,aAAa,CAACE,YAAtC,EAAoD;AAClDF,IAAAA,aAAa,GAAGA,aAAa,CAACE,YAA9B;;AAEA,QAAIF,aAAa,CAACG,KAAd,IAAuBH,aAAa,CAACG,KAAd,CAAoBC,OAA/C,EAAwD;AACtDL,MAAAA,mBAAmB,CAACM,IAApB,CAAyBL,aAAa,CAACG,KAAd,CAAoBC,OAA7C;AACD;AACF;;AAED,MAAIE,aAAa,GAAG,EAApB;;AAEA,OAAK,MAAMC,kBAAX,IAAiCR,mBAAjC,EAAsD;A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KAXI,CAAP;AAYD,GAnBD;;AAqBA,OAAK,MAAMI,QAAX,IAAuBnB,MAAM,CAACC,IAAP,CAAYmB,oBAAZ,CAAvB,EAA+C;AAC7C;AACAvC,IAAAA,GAAG,CAACsC,QAAD,CAAH,GAAgB,CAACrC,CAAD,EAAIC,CAAJ,EAAOC,CAAP,EAAUC,CAAV,EAAaC,CAAb,EAAgBC,CAAhB,EAAmBC,CAAnB,EAAsBC,CAAtB,EAAyBC,CAAzB,EAA4BC,CAA5B,KAAkC;AAChD,aAAOV,GAAG,CAAC+B,KAAJ,CAAU;AACfO,QAAAA,QAAQ,EAAEC,qBAAUD,QAAV;AADK,OAAV,EAEJrC,CAFI,EAEDC,CAFC,EAEEC,CAFF,EAEKC,CAFL,EAEQC,CAFR,EAEWC,CAFX,EAEcC,CAFd,EAEiBC,CAFjB,EAEoBC,CAFpB,EAEuBC,CAFvB,CAAP;AAGD,KAJD;AAKD,GApG0G,CAsG3G;AACA;;;AACA,SAAOV,GAAP;AACD,CAzGD;;eA2GeH,Y","sourcesContent":["// @flow\n\nimport environmentIsNode from 'detect-node';\nimport createGlobalThis from 'globalthis';\nimport stringify from 'json-stringify-safe';\nimport {\n  sprintf,\n} from 'sprintf-js';\nimport {\n  logLevels,\n} from '../constants';\nimport type {\n  LoggerType,\n  MessageContextType,\n  MessageEventHandlerType,\n  TranslateMessageFunctionType,\n} from '../types';\n\nconst globalThis = createGlobalThis();\n\nlet domain;\n\nif (environmentIsNode) {\n  // eslint-disable-next-line global-require\n  domain = require('domain');\n}\n\nconst getParentDomainContext = () => {\n  if (!domain) {\n    return {};\n  }\n\n  const parentRoarrContexts = [];\n\n  let currentDomain = process.domain;\n\n  // $FlowFixMe\n  if (!currentDomain || !currentDomain.parentDomain) {\n    return {};\n  }\n\n  while (currentDomain && currentDomain.parentDomain) {\n    currentDomain = currentDomain.parentDomain;\n\n    if (currentDomain.roarr && currentDomain.roarr.context) {\n      parentRoarrContexts.push(currentDomain.roarr.context);\n    }\n  }\n\n  let domainContext = {};\n\n  for (const parentRoarrContext of parentRoarrContexts) {\n    domainContext = {\n      ...domainContext,\n      ...parentRoarrContext,\n    };\n  }\n\n  return domainContext;\n};\n\nconst getFirstParentDomainContext = () => {\n  if (!domain) {\n    return {};\n  }\n\n  let currentDomain = process.domain;\n\n  // $FlowFixMe\n  if (currentDomain && currentDomain.roarr && currentDomain.roarr.context) {\n    return currentDomain.roarr.context;\n  }\n\n  // $FlowFixMe\n  if (!currentDomain || !currentDomain.parentDomain) {\n    return {};\n  }\n\n  while (currentDomain && currentDomain.parentDomain) {\n    currentDomain = currentDomain.parentDomain;\n\n    if (currentDomain.roarr && currentDomain.roarr.context) {\n      return currentDomain.roarr.context;\n    }\n  }\n\n  return {};\n};\n\nconst createLogger = (onMessage: MessageEventHandlerType, parentContext?: MessageContextType): LoggerType => {\n  // eslint-disable-next-line id-length, unicorn/prevent-abbreviations\n  const log = (a, b, c, d, e, f, g, h, i, k) => {\n    const time = Date.now();\n    const sequence = globalThis.ROARR.sequence++;\n\n    let context;\n    let message;\n\n    if (typeof a === 'string') {\n      context = {\n        ...getFirstParentDomainContext(),\n        ...parentContext || {},\n      };\n      // eslint-disable-next-line id-length, object-property-newline\n      const {...args} = {a, b, c, d, e, f, g, h, i, k};\n      const values = Object.keys(args).map((key) => {\n        return args[key];\n      });\n      // eslint-disable-next-line unicorn/no-reduce\n      const hasOnlyOneParameterValued = 1 === values.reduce((accumulator, value) => {\n        // eslint-disable-next-line no-return-assign, no-param-reassign\n        return accumulator += typeof value === 'undefined' ? 0 : 1;\n      }, 0);\n      message = hasOnlyOneParameterValued ? sprintf('%s', a) : sprintf(a, b, c, d, e, f, g, h, i, k);\n    } else {\n      if (typeof b !== 'string') {\n        throw new TypeError('Message must be a string.');\n      }\n\n      context = JSON.parse(stringify({\n        ...getFirstParentDomainContext(),\n        ...parentContext || {},\n        ...a,\n      }));\n\n      message = sprintf(b, c, d, e, f, g, h, i, k);\n    }\n\n    onMessage({\n      context,\n      message,\n      sequence,\n      time,\n      version: '1.0.0',\n    });\n  };\n\n  log.child = (context: TranslateMessageFunctionType | MessageContextType): LoggerType => {\n    if (typeof context === 'function') {\n      return createLogger((message) => {\n        if (typeof context !== 'function') {\n          throw new TypeError('Unexpected state.');\n        }\n        onMessage(context(message));\n      }, parentContext);\n    }\n\n    return createLogger(onMessage, {\n      ...getFirstParentDomainContext(),\n      ...parentContext,\n      ...context,\n    });\n  };\n\n  log.getContext = (): MessageContextType => {\n    return {\n      ...getFirstParentDomainContext(),\n      ...parentContext || {},\n    };\n  };\n\n  log.adopt = async (routine, context) => {\n    if (!domain) {\n      return routine();\n    }\n\n    const adoptedDomain = domain.create();\n\n    return adoptedDomain\n      .run(() => {\n        // $FlowFixMe\n        adoptedDomain.roarr = {\n          context: {\n            ...getParentDomainContext(),\n            ...context,\n          },\n        };\n\n        return routine();\n      });\n  };\n\n  for (const logLevel of Object.keys(logLevels)) {\n    // eslint-disable-next-line id-length, unicorn/prevent-abbreviations\n    log[logLevel] = (a, b, c, d, e, f, g, h, i, k) => {\n      return log.child({\n        logLevel: logLevels[logLevel],\n      })(a, b, c, d, e, f, g, h, i, k);\n    };\n  }\n\n  // @see https://github.com/facebook/flow/issues/6705\n  // $FlowFixMe\n  return log;\n};\n\nexport default createLogger;\n"],"file":"createLogger.js"}