-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
gh-200. Client Side Logger.
- Loading branch information
Showing
33 changed files
with
224 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
import {createClient} from '@util-web-api-client' | ||
import {ClientTypes, createClient} from '@util-web-api-client' | ||
|
||
export const client = createClient() | ||
export const client = createClient({ | ||
type: ClientTypes.Axios, | ||
options: {baseURL: 'http://localhost:7777'}, | ||
}) |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import React, {Component} from 'react' | ||
import {AppContext} from '../context' | ||
import {childrenPropTypes} from '../utils' | ||
|
||
export class ErrorBoundary extends Component { | ||
constructor(props) { | ||
super(props) | ||
this.state = {hasError: false} | ||
} | ||
|
||
static getDerivedStateFromError() { | ||
return {hasError: true} | ||
} | ||
|
||
componentDidCatch(error) { | ||
const {logger} = this.context | ||
logger.error({message: error.message, stack: JSON.stringify(error.stack)}) | ||
} | ||
|
||
render() { | ||
const {hasError} = this.state | ||
const {children} = this.props | ||
|
||
if (hasError) { | ||
return <h1>Something went wrong.</h1> | ||
} | ||
|
||
return children | ||
} | ||
} | ||
|
||
ErrorBoundary.contextType = AppContext | ||
|
||
ErrorBoundary.defaultProps = { | ||
children: childrenPropTypes, | ||
} | ||
|
||
ErrorBoundary.propTypes = { | ||
children: null, | ||
} |
This file contains 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
This file contains 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
This file contains 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
2 changes: 1 addition & 1 deletion
2
apps/web-portal-client/lib/components/layout/main-menu/DesktopMenu.jsx
This file contains 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
2 changes: 1 addition & 1 deletion
2
apps/web-portal-client/lib/components/layout/main-menu/MobileMenu.jsx
This file contains 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
2 changes: 1 addition & 1 deletion
2
apps/web-portal-client/lib/components/layout/sidebar/Sidebar.jsx
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ import {createContext} from 'react' | |
|
||
export const AppContext = createContext({ | ||
api: null, | ||
logger: null, | ||
}) |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {createLogger} from '@zorko-io/util-logger' | ||
import {MessageQueue} from '../utils' | ||
import {client} from '../api' | ||
|
||
const LOGS_MAX_SIZE = 10 | ||
const messageQueue = new MessageQueue(LOGS_MAX_SIZE) | ||
|
||
export const logger = createLogger({ | ||
context: { | ||
browser: { | ||
write: async (message) => { | ||
messageQueue.push(message) | ||
if (message.level >= 50) { | ||
await client.log.save(messageQueue.messages) | ||
} | ||
}, | ||
}, | ||
}, | ||
}) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
import {configureStore} from '@reduxjs/toolkit' | ||
import {logger} from '../logger' | ||
import authReducer from '../features/auth/slices' | ||
|
||
const loggerMiddleware = () => (next) => (action) => { | ||
logger.info(action) | ||
return next(action) | ||
} | ||
|
||
export default configureStore({ | ||
reducer: { | ||
auth: authReducer, | ||
}, | ||
middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(loggerMiddleware), | ||
}) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
export class MessageQueue { | ||
#messages = [] | ||
|
||
constructor(size) { | ||
this.size = size | ||
} | ||
|
||
enqueue(message) { | ||
this.#messages.push(message) | ||
} | ||
|
||
dequeue() { | ||
this.#messages.shift() | ||
} | ||
|
||
get isEmpty() { | ||
return !!this.#messages.length | ||
} | ||
|
||
get queueLength() { | ||
return this.#messages.length | ||
} | ||
|
||
push(message) { | ||
if (!this.isEmpty && this.queueLength >= this.size) { | ||
this.dequeue() | ||
} | ||
this.enqueue(message) | ||
} | ||
|
||
get messages() { | ||
return this.#messages | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './childrenPropTypes' | ||
export * from './MessageQueue' |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import {LogSave} from '../use-cases/log' | ||
|
||
export default ({makeRunner}) => { | ||
return { | ||
save: makeRunner(LogSave, { | ||
toParams: (req) => ({...req.body}), | ||
}), | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {UseCase} from '@zorko-io/util-use-case' | ||
|
||
// TODO: add integration tests for api/v1/log endpoint | ||
export class LogSave extends UseCase { | ||
// eslint-disable-next-line no-unused-vars | ||
async run(logs) { | ||
// TODO: wire with log 'pino' instance | ||
console.log('logs ', logs) | ||
return {} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './LogSave' |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* eslint-disable no-unused-vars */ | ||
import {LogApi} from '../core' | ||
|
||
export class AxiosLogApi extends LogApi { | ||
#http = null | ||
/** | ||
* @param http - Axios instance | ||
*/ | ||
|
||
constructor(http) { | ||
super() | ||
this.#http = http | ||
} | ||
|
||
async save(logs) { | ||
const response = await this.#http.post(`/api/v1/log`, logs) | ||
return response ? response.data : {status: 1} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './AxiosClientApi' | ||
export * from './AxiosAuthApi' | ||
export * from './AxiosPreviewApi' | ||
export * from './AxiosLogApi' |
Oops, something went wrong.