Skip to content

Commit

Permalink
feat: HS-172: Conditional log push based on loggingLevel (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
vsrivatsa-edinburgh authored Nov 24, 2023
1 parent 23a1a68 commit 5ee2f78
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 42 deletions.
1 change: 1 addition & 0 deletions src/GlobalVars.res
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
@val external sentryDSN: string = "sentryDSN"
@val external sentryScriptUrl: string = "sentryScriptUrl"
@val external enableLogging: bool = "enableLogging"
@val external loggingLevelStr: string = "loggingLevel"
let targetOrigin: string = "*"
let isInteg = sdkUrl === "https://dev.hyperswitch.io"
let isSandbox = sdkUrl === "https://beta.hyperswitch.io"
Expand Down
54 changes: 29 additions & 25 deletions src/orca-log-catcher/ErrorBoundary.res
Original file line number Diff line number Diff line change
Expand Up @@ -108,32 +108,36 @@ module ErrorCard = {
}

React.useEffect0(() => {
let errorLog: OrcaLogger.logFile = {
logType: ERROR,
timestamp: Js.Date.now()->Belt.Float.toString,
sessionId: "",
source: "orca-elements",
version: GlobalVars.repoVersion,
value: error->errorToJson->Js.Json.stringify,
internalMetadata: "",
category: USER_ERROR,
paymentId: "",
merchantId: "",
browserName: OrcaLogger.arrayOfNameAndVersion
->Belt.Array.get(0)
->Belt.Option.getWithDefault("Others"),
browserVersion: OrcaLogger.arrayOfNameAndVersion
->Belt.Array.get(1)
->Belt.Option.getWithDefault("0"),
platform: Window.platform,
userAgent: Window.userAgent,
appId: "",
eventName: "SDK_CRASH",
latency: "",
paymentMethod: "",
firstEvent: false,
let loggingLevel = GlobalVars.loggingLevelStr
let enableLogging = GlobalVars.enableLogging
if enableLogging && ["DEBUG", "INFO", "WARN", "ERROR"]->Js.Array2.includes(loggingLevel) {
let errorLog: OrcaLogger.logFile = {
logType: ERROR,
timestamp: Js.Date.now()->Belt.Float.toString,
sessionId: "",
source: "orca-elements",
version: GlobalVars.repoVersion,
value: error->errorToJson->Js.Json.stringify,
internalMetadata: "",
category: USER_ERROR,
paymentId: "",
merchantId: "",
browserName: OrcaLogger.arrayOfNameAndVersion
->Belt.Array.get(0)
->Belt.Option.getWithDefault("Others"),
browserVersion: OrcaLogger.arrayOfNameAndVersion
->Belt.Array.get(1)
->Belt.Option.getWithDefault("0"),
platform: Window.platform,
userAgent: Window.userAgent,
appId: "",
eventName: "SDK_CRASH",
latency: "",
paymentMethod: "",
firstEvent: false,
}
beaconApiCall([errorLog])
}
beaconApiCall([errorLog])
None
})

Expand Down
61 changes: 44 additions & 17 deletions src/orca-log-catcher/OrcaLogger.res
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type logType = DEBUG | INFO | ERROR | WARNING
type logType = DEBUG | INFO | ERROR | WARNING | SILENT
type logCategory = API | USER_ERROR | USER_EVENT | MERCHANT_EVENT

type eventName =
Expand Down Expand Up @@ -227,6 +227,7 @@ let logFileToObj = logFile => {
| INFO => "INFO"
| ERROR => "ERROR"
| WARNING => "WARNING"
| SILENT => "SILENT"
}->Js.Json.string,
),
("component", "WEB"->Js.Json.string),
Expand Down Expand Up @@ -374,6 +375,15 @@ let browserDetect = content => {
let arrayOfNameAndVersion = Js.String2.split(Window.userAgent->browserDetect, "-")

let make = (~sessionId=?, ~source: option<source>=?, ~clientSecret=?, ~merchantId=?, ()) => {
let loggingLevel = switch GlobalVars.loggingLevelStr {
| "DEBUG" => DEBUG
| "INFO" => INFO
| "WARNING" => WARNING
| "ERROR" => ERROR
| "SILENT"
| _ =>
SILENT
}
let mainLogFile: array<logFile> = []
let sessionId = getRefFromOption(sessionId)
let setSessionId = value => {
Expand All @@ -391,6 +401,25 @@ let make = (~sessionId=?, ~source: option<source>=?, ~clientSecret=?, ~merchantI
merchantId := value
}

let conditionalLogPush = (log: logFile) => {
if GlobalVars.enableLogging {
switch loggingLevel {
| DEBUG => log->Js.Array2.push(mainLogFile, _)->ignore
| INFO =>
[INFO, WARNING, ERROR]->Js.Array2.includes(log.logType)
? log->Js.Array2.push(mainLogFile, _)->ignore
: ()
| WARNING =>
[WARNING, ERROR]->Js.Array2.includes(log.logType)
? log->Js.Array2.push(mainLogFile, _)->ignore
: ()
| ERROR =>
[ERROR]->Js.Array2.includes(log.logType) ? log->Js.Array2.push(mainLogFile, _)->ignore : ()
| SILENT => ()
}
}
}

let beaconApiCall = data => {
if data->Js.Array2.length > 0 {
let logData =
Expand All @@ -410,19 +439,17 @@ let make = (~sessionId=?, ~source: option<source>=?, ~clientSecret=?, ~merchantI
let events = ref(Js.Dict.empty())

let rec sendLogs = () => {
if GlobalVars.enableLogging {
switch timeOut.contents {
| Some(val) => {
Js.Global.clearTimeout(val)
timeOut := Some(Js.Global.setTimeout(() => sendLogs(), 120000))
}
| None => timeOut := Some(Js.Global.setTimeout(() => sendLogs(), 120000))
}
beaconApiCall(mainLogFile)
let len = mainLogFile->Js.Array2.length
for _ in 0 to len - 1 {
mainLogFile->Js.Array2.pop->ignore
switch timeOut.contents {
| Some(val) => {
Js.Global.clearTimeout(val)
timeOut := Some(Js.Global.setTimeout(() => sendLogs(), 120000))
}
| None => timeOut := Some(Js.Global.setTimeout(() => sendLogs(), 120000))
}
beaconApiCall(mainLogFile)
let len = mainLogFile->Js.Array2.length
for _ in 0 to len - 1 {
mainLogFile->Js.Array2.pop->ignore
}
}

Expand Down Expand Up @@ -527,7 +554,7 @@ let make = (~sessionId=?, ~source: option<source>=?, ~clientSecret=?, ~merchantI
paymentMethod: paymentMethod,
firstEvent: firstEvent,
}
->Js.Array2.push(mainLogFile, _)
->conditionalLogPush
->ignore
checkLogSizeAndSendData()
events.contents->Js.Dict.set(eventNameStr, localTimestampFloat)
Expand Down Expand Up @@ -585,7 +612,7 @@ let make = (~sessionId=?, ~source: option<source>=?, ~clientSecret=?, ~merchantI
paymentMethod: paymentMethod,
firstEvent: firstEvent,
}
->Js.Array2.push(mainLogFile, _)
->conditionalLogPush
->ignore
checkLogSizeAndSendData()
events.contents->Js.Dict.set(eventNameStr, localTimestampFloat)
Expand Down Expand Up @@ -630,7 +657,7 @@ let make = (~sessionId=?, ~source: option<source>=?, ~clientSecret=?, ~merchantI
paymentMethod: paymentMethod,
firstEvent: firstEvent,
}
->Js.Array2.push(mainLogFile, _)
->conditionalLogPush
->ignore
checkLogSizeAndSendData()
events.contents->Js.Dict.set(eventNameStr, localTimestampFloat)
Expand Down Expand Up @@ -663,7 +690,7 @@ let make = (~sessionId=?, ~source: option<source>=?, ~clientSecret=?, ~merchantI
paymentMethod: "",
firstEvent: firstEvent,
}
->Js.Array2.push(mainLogFile, _)
->conditionalLogPush
->ignore
checkLogSizeAndSendData()
events.contents->Js.Dict.set(eventName, Js.Date.now())
Expand Down
4 changes: 4 additions & 0 deletions webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ let logEndpoint =

let enableLogging = false;

// Choose from DEBUG, INFO, WARNING, ERROR, SILENT
let loggingLevel = "DEBUG";

module.exports = (publicPath = "auto") => {
let entries = {
app: "./index.js",
Expand Down Expand Up @@ -118,6 +121,7 @@ module.exports = (publicPath = "auto") => {
sentryDSN: JSON.stringify(process.env.SENTRY_DSN),
sentryScriptUrl: JSON.stringify(process.env.SENTRY_SCRIPT_URL),
enableLogging: JSON.stringify(enableLogging),
loggingLevel: JSON.stringify(loggingLevel),
}),
new HtmlWebpackPlugin({
inject: false,
Expand Down

0 comments on commit 5ee2f78

Please sign in to comment.