diff --git a/src/Logger.ts b/src/Logger.ts index 8071c7e..07a1bd3 100644 --- a/src/Logger.ts +++ b/src/Logger.ts @@ -1,4 +1,4 @@ -import { setLocalStorage } from './setLocalStorage'; +import { SetLocalStorage } from './SetLocalStorage'; import { LogPayloadParams, ServiceNameType } from './types/LogType'; import CryptoJS from 'crypto-js'; @@ -57,7 +57,7 @@ const initialLog = ( logger.event.name = name; logger.event.path = path; - setLocalStorage(logger); + SetLocalStorage(logger); }; export const useYLSLogger = () => { diff --git a/src/setLocalStorage.ts b/src/setLocalStorage.ts index e69de29..a96331d 100644 --- a/src/setLocalStorage.ts +++ b/src/setLocalStorage.ts @@ -0,0 +1,24 @@ +import { postLog } from './apis/postLog'; +import { LogType } from './types/LogType'; + +const SetLocalStorageClear = () => { + const list: any[] = []; + localStorage.setItem('yls-web', JSON.stringify(list)); +}; + +export const SetLocalStorage = async (logger: LogType) => { + if (window.localStorage.getItem('yls-web') == undefined) { + const list: any[] = []; + list.push(logger); + localStorage.setItem('yls-web', JSON.stringify(list)); + } else { + const remainList: any[] = JSON.parse(localStorage.getItem('yls-web') as string) || []; + if (remainList.length < 10) { + const updateList = [...remainList, logger]; + localStorage.setItem('yls-web', JSON.stringify(updateList)); + } else { + SetLocalStorageClear(); + const res = await postLog(); + } + } +};