Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] LogScreen, Log 로컬스토리지 저장 #10

Merged
merged 8 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@
"react-router-dom": "^6.21.3",
"typescript": "^5.2.2",
"vite": "^5.0.8"
}
},
"dependencies": {}
}
2 changes: 1 addition & 1 deletion src/LogClick.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Children, cloneElement } from 'react';
import { useYLSLogger } from '.';
import { LogPayloadParams } from './types/LogPayloadParams';
import { LogPayloadParams } from './types/LogType';

interface Props {
children: React.ReactElement;
Expand Down
4 changes: 2 additions & 2 deletions src/LogParamsProvider.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { createContext } from 'react';
// import { createContext } from 'react';

const LogParamsContext = createContext(null);
// const LogParamsContext = createContext(null);
9 changes: 2 additions & 7 deletions src/LogScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useYLSLogger } from '.';
import { useLocation } from 'react-router-dom';
import { useEffect } from 'react';
import { LogPayloadParams } from './types/LogPayloadParams';
import { LogPayloadParams } from './types/LogType';

interface Props {
children: React.ReactNode;
Expand All @@ -10,13 +9,9 @@ interface Props {

export const LogScreen = ({ children, params }: Props) => {
const logger = useYLSLogger();
const router = useLocation();
const { path } = params;

useEffect(() => {
if (router) {
// logger.screen(path);
}
logger.screen(params);
}, []);

return <>{children}</>;
Expand Down
61 changes: 46 additions & 15 deletions src/Logger.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import { LogPayloadParams } from './types/LogPayloadParams';
import { LogPayloadParams, LogType, LoggerType } from './types/LogType';

interface LoggerType {
path: string;
platform: string;
serviceName: string;
name: string;
message: string;
}

const createRandomId = () => {
const createUserId = () => {
// Todo: create random id
return 123;
};

const createTimestamp = () => {
Expand All @@ -18,30 +11,68 @@ const createTimestamp = () => {
return now.toISOString();
};

const setLocalStorage = (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) || [];
const updateList = [...remainList, logger];
localStorage.setItem('yls-web', JSON.stringify(updateList));
}
};

export const useYLSLogger = () => {
const screen = ({ path }: LogPayloadParams) => {
// console.log(`Logging screen information for path: ${path}`);
const screen = ({ serviceName, name }: LogPayloadParams) => {
//사용자에서 path,name,message를 넣어줌
const loggerType: LoggerType = {
path: '/',
serviceName: 'home',
name: '',
message: '/',
};
const logger = Logger(loggerType);
console.log(`Logging screen information for path: ${serviceName}`);
logger.event.name = name;

setLocalStorage(logger);
};

const click = ({ name }: LogPayloadParams) => {
console.log(`Logging click information for button: ${name}`);
//사용자에서 path,name,message를 넣어줌
const loggerType: LoggerType = {
path: '/',
serviceName: 'home',
name: '',
message: '/',
};
const logger = Logger(loggerType);

logger.event.name = name;

setLocalStorage(logger);
};
// todo: 로컬스토리지 로그 개수가 10개 넘어가면 postLog.ts호출

return {
screen,
click,
};
};

export const Logger = ({ path, platform, serviceName, name, message }: LoggerType) => {
export const Logger = ({ serviceName, name, message, path, tags }: LoggerType) => {
return {
userId: createRandomId(),
userId: createUserId(),
timestamp: createTimestamp(),
event: {
platform,
platform: 'web',
serviceName,
name,
message,
path,
tags,
},
};
};
1 change: 1 addition & 0 deletions src/apis/postLog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const postLog = async () => {};
24 changes: 8 additions & 16 deletions src/demo/App.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import { useState } from 'react';
import { LogClick } from '../LogClick';

import { BrowserRouter } from 'react-router-dom';
import { Routes, Route } from 'react-router-dom';
import { Home } from './Home';
export const App = () => {
const [count, setCount] = useState(0);

return (
<>
<h1>Vite + React</h1>
<div className="card">
<LogClick
params={{
userId: 123,
name: 'click',
}}
>
<button onClick={() => setCount((count) => count + 1)}>count is {count}</button>
</LogClick>
</div>
<BrowserRouter>
<Routes>
<Route path="/" element={<Home />} />
</Routes>
</BrowserRouter>
</>
);
};
33 changes: 33 additions & 0 deletions src/demo/Home.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useState } from 'react';
import { LogClick } from '../LogClick';
import { useLocation } from 'react-router-dom';
import { LogScreen } from '../LogScreen';
export const Home = () => {
const [count, setCount] = useState(0);
const router = useLocation();

return (
<>
<h1>Vite + React</h1>
<div className="card">
<LogScreen
params={{
userId: 123,
screenName: router.pathname,
eventName: '',
}}
>
<LogClick
params={{
userId: 123,
eventName: 'click',
screenName: '',
}}
>
<button onClick={() => setCount((count) => count + 1)}>count is {count}</button>
</LogClick>
</LogScreen>
</div>
</>
);
};
7 changes: 0 additions & 7 deletions src/types/LogPayloadParams.ts

This file was deleted.

32 changes: 32 additions & 0 deletions src/types/LogType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// LogType: 최종 Log 형태
export interface LogType {
userId: number;
timestamp: string;
event: {
platform: string;
serviceName: 'drawer' | 'home' | 'search';
name: string;
message?: string;
path?: string;
tags?: string[];
};
}

// LoggerType: Log 내 event에 들어가는 값
export interface LoggerType {
serviceName: 'drawer' | 'home' | 'search';
name: string;
message?: string;
path?: string;
tags?: string[];
}

// LogPayloadParams: 사용처에서 넣어주는 값
export interface LogPayloadParams {
userId: number;
name: string | '';
serviceName?: 'drawer' | 'home' | 'search';
message?: string;
path?: string;
tags?: string[];
}