All files / framework/logging logging.ts

78.95% Statements 15/19
60% Branches 15/25
100% Functions 3/3
76.47% Lines 13/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62                                                256x 256x 256x 256x     7380x     9x   9x   7x           9x 7x 2x 2x                 1845x            
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import type { ModuleName } from 'framework/types/ModuleName'
 
declare global {
  interface Window {
    bugsnagClient: any // eslint-disable-line
  }
}
 
type LogFunction = (message: string, obj?: Record<string, unknown>) => void
 
interface Logger {
  error: LogFunction
  info: LogFunction
  warn: LogFunction
  debug: LogFunction
}
 
const ERROR = 'ERROR'
const WARN = 'WARN'
const INFO = 'INFO'
const DEBUG = 'DEBUG'
 
function log(type: string, module: ModuleName, subModule?: string) {
  return function (message: string, obj: Record<string, unknown> = {}) {
    // Message format: `ModuleName/SubModuleName: Actual Message`, obj
    // This format will make it easier to query logs against a module
    const _message = `${module}${subModule ? `/${subModule}` : ''}: ${message}`
 
    if (type === ERROR || type === WARN) {
      // Log to Bugsnag if it's available
      window.bugsnagClient?.notify?.(new Error(_message), {
        severity: type === ERROR ? 'error' : 'warning',
        user: obj
      })
    }
 
    if (type === ERROR) {
      console.error(_message, obj) // eslint-disable-line
    } else Eif (type === INFO) {
      console.info(_message, obj) // eslint-disable-line
    } else if (type === DEBUG) {
      console.debug(_message, obj) // eslint-disable-line
    } else if (type === WARN) {
      console.warn(_message, obj) // eslint-disable-line
    }
  }
}
 
export const loggerFor = (module: ModuleName, subModule?: string): Logger => ({
  error: log(ERROR, module, subModule),
  info: log(INFO, module, subModule),
  warn: log(WARN, module, subModule),
  debug: log(DEBUG, module, subModule)
})