Last active
March 25, 2023 12:36
-
-
Save Lory1990/bfbdcd1b1eefaaa76c78ab32dcdf043a to your computer and use it in GitHub Desktop.
use Logger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { datadogLogs } from "@datadog/browser-logs" | |
| import { datadogRum } from "@datadog/browser-rum" | |
| import { RootState } from "app/rootReducer" | |
| import _ from "lodash" | |
| import { useSelector } from "react-redux" | |
| export interface ILoggerProps { | |
| component?: string | |
| } | |
| export interface ILoggerData { | |
| logApiError: (error?: any) => void | |
| log: (message?: string, method?: string, optionalParams?: any) => void; | |
| info: (message?: string, method?: string, optionalParams?: any) => void; | |
| warn: (message: string, method?: string, optionalParams?: any) => void; | |
| error: (error?: any, method?: string, optionalParams?: any) => void; | |
| init: () => void; | |
| setUser: (out: User ) =>void; | |
| } | |
| export default function useLogger(loggerProps: ILoggerProps = {}): ILoggerData { | |
| const init = ()=>{ | |
| datadogLogs.init({ | |
| clientToken: "TOKEN", | |
| site: "datadoghq.eu", | |
| service: "SERVICE", | |
| env: "PROD", | |
| version: "1.0.0", | |
| forwardErrorsToLogs: true, | |
| sampleRate: 100, | |
| }); | |
| } | |
| const setUser = (out: User) =>{ | |
| datadogLogs.setUser(out); | |
| } | |
| const commonParameters = { company, component: loggerProps.component }; | |
| const logApiError = (error?: any): void => { | |
| if (error.response) { | |
| if (error.response.status === 401) return; | |
| const attributes = { | |
| url: error?.response?.request?.responseURL, | |
| type: "Response Error", | |
| errorNo: error.response.status, | |
| httpMethod: error.response?.config?.method, | |
| payload: error.response?.config?.data, | |
| response: error.response?.data, | |
| trace: _.get(error.response, "data.errors[0].trace"), | |
| correlationId: _.get(error.response, "data.errors[0].correlationId"), | |
| ...commonParameters, | |
| }; | |
| datadogRum.addError(error.message, { ...attributes, source: "network" }); | |
| datadogLogs.logger.error(error.message, attributes); | |
| } else if (error.request) { | |
| const attributes = { | |
| url: error.request.url, | |
| type: "Request Error", | |
| httpMethod: error.request?.config?.method, | |
| payload: error?.request?.config?.data, | |
| ...commonParameters, | |
| }; | |
| //datadogRum.setUser({id: userUid, name: userName}) | |
| datadogRum.addError(error.message, { ...attributes, company, source: "network" }); | |
| datadogLogs.logger.error(error.message, attributes); | |
| } else { | |
| datadogRum.addError("Generic Error"); | |
| datadogLogs.logger.error("Generic Error", { ...commonParameters, ...error }); | |
| } | |
| }; | |
| const log = (message?: any, method?: string, optionalParams?: any): void => { | |
| const { component, ...realOptionalParams } = optionalParams || {}; | |
| const realCompoent = component || loggerProps.component; | |
| if (process.env.NODE_ENV === "development") { | |
| // eslint-disable-next-line no-console | |
| console.log("[" + method + "][" + realCompoent + "]", message, realOptionalParams); | |
| return; | |
| } | |
| datadogLogs.logger.info(message, { | |
| method, | |
| ...commonParameters, | |
| component: realCompoent, | |
| company, | |
| ...realOptionalParams, | |
| }); | |
| }; | |
| const warn = (message: any, method?: string, optionalParams?: any) => { | |
| //window.classicConsole?.warn.apply([message, optionalParams]) | |
| const { component, ...realOptionalParams } = optionalParams || {}; | |
| const realComponent = component || loggerProps.component; | |
| if (process.env.NODE_ENV === "development") { | |
| // eslint-disable-next-line no-console | |
| console.warn(message, "[" + method + "][" + realComponent + "]", realOptionalParams); | |
| return; | |
| } | |
| datadogLogs.logger.warn(message, { | |
| method, | |
| ...commonParameters, | |
| component: realComponent, | |
| company, | |
| ...realOptionalParams, | |
| }); | |
| }; | |
| const error = (errorOrMessage?: any, method?: string, optionalParams?: any): void => { | |
| const { component, ...realOptionalParams } = optionalParams || {}; | |
| const realComponent = component || loggerProps.component; | |
| if (process.env.NODE_ENV === "development") { | |
| // eslint-disable-next-line no-console | |
| console.error(errorOrMessage, "[" + method + "][" + realComponent + "]", realOptionalParams); | |
| return; | |
| } | |
| datadogRum.addError(errorOrMessage, { | |
| method, | |
| ...commonParameters, | |
| component: realComponent, | |
| company, | |
| ...realOptionalParams, | |
| source: "source", | |
| }); | |
| datadogLogs.logger.error(errorOrMessage, { | |
| method, | |
| ...commonParameters, | |
| component: realComponent, | |
| company, | |
| ...realOptionalParams, | |
| }); | |
| }; | |
| return { | |
| logApiError, | |
| info: log, | |
| log, | |
| error, | |
| warn, | |
| init, | |
| setUser | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment