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: logs tab #137

Merged
merged 28 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
9f5cdb9
feat: console logs tab
kobenguyent May 7, 2024
d27604f
feat: logs tab
kobenguyent May 7, 2024
ee3a6d8
Merge branch 'main' into feat/log-tab
kobenguyent May 7, 2024
429e74b
Merge branch 'main' into feat/log-tab
kobenguyent May 8, 2024
222385d
feat: showing logs modal
kobenguyent May 8, 2024
2844a08
Merge branch 'main' into feat/log-tab
kobenguyent May 8, 2024
e7d6049
fix: logs print object
kobenguyent May 8, 2024
be6b12a
fix: add log level to message
kobenguyent May 8, 2024
3205dfe
fix: add log level to message
kobenguyent May 8, 2024
3454022
fix: add log level to message
kobenguyent May 8, 2024
0f8426f
fix: improve UI
kobenguyent May 8, 2024
dfb6c65
fix: same timestamp and args logs improvement
kobenguyent May 8, 2024
4d285ed
remove json stringify
kobenguyent May 9, 2024
140b1ad
fix: args mapping
kobenguyent May 9, 2024
1f1800d
fix: args mapping function
kobenguyent May 9, 2024
cfd102e
fix: logs show error {}
kobenguyent May 9, 2024
b5c28ab
fix: indents
kobenguyent May 9, 2024
34c8992
fix: beautify json string
kobenguyent May 9, 2024
6da283a
Merge branch 'main' into feat/log-tab
flawiddsouza May 9, 2024
8247cc1
fix: improve the console logs
kobenguyent May 9, 2024
da87d2b
fix: types complain
kobenguyent May 9, 2024
dfeedb0
fix: lint issue
kobenguyent May 9, 2024
8fc24cd
fix: error shows {}
kobenguyent May 9, 2024
fc9516d
fix: logic in args mapping function
kobenguyent May 9, 2024
759a60f
fix: lint issue
kobenguyent May 9, 2024
0d5a2b6
fix: check the Error
kobenguyent May 9, 2024
3afd7fa
address CR
kobenguyent May 10, 2024
20d8c7d
fix: remove extra }
kobenguyent May 10, 2024
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
12 changes: 10 additions & 2 deletions packages/ui/src/components/NavBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
</template>
<a href="#" @click.prevent="showPluginsManager" class="bl">Plugins</a>
<a href="#" @click.prevent="showSettings" class="bl br">Settings</a>
<a href="#" @click.prevent="showLogs" class="bl br">Logs</a>
<span class="spacer"></span>
<div>
<a class="gh-button-container" href="https://github.com/flawiddsouza/Restfox" rel="noopener" target="_blank" title="Star Restfox" aria-label="Star Restfox on GitHub">
Expand All @@ -49,6 +50,7 @@
<PluginManagerModal v-model:showModal="showPluginManagerModal" />
<AddWorkspaceModal v-model:showModal="showAddWorkspaceModal" :is-electron="flags.isElectron" />
<SettingsModal v-model:showModal="showSettingsModal" />
<LogsModal v-model:showModal="showLogsModal"></LogsModal>
<EnvironmentModal v-model:showModal="environmentModalShow" :workspace="activeWorkspace" v-if="activeWorkspace" />
<BackupAndRestoreModal />
</template>
Expand All @@ -59,6 +61,7 @@
import SettingsModal from './modals/SettingsModal.vue'
import EnvironmentModal from './modals/EnvironmentModal.vue'
import BackupAndRestoreModal from './modals/BackupAndRestoreModal.vue'
import LogsModal from './modals/LogsModal.vue'
import { exportRestfoxCollection, applyTheme, generateNewIdsForTree, toTree } from '@/helpers'
import { getCollectionForWorkspace } from '@/db'
import constants from '../constants'
Expand All @@ -69,17 +72,19 @@
AddWorkspaceModal,
SettingsModal,
EnvironmentModal,
BackupAndRestoreModal
BackupAndRestoreModal,
LogsModal
},
props: {
nav: String

Check warning on line 79 in packages/ui/src/components/NavBar.vue

View workflow job for this annotation

GitHub Actions / test

Prop 'nav' requires default value to be set
},
data() {
return {
showSettingsModal: false,
showPluginManagerModal: false,
showAddWorkspaceModal: false,
environmentModalShow: false
environmentModalShow: false,
showLogsModal: false,
}
},
computed: {
Expand Down Expand Up @@ -168,6 +173,9 @@
showSettings() {
this.showSettingsModal = true
},
showLogs() {
this.showLogsModal = true
},
showPluginsManager() {
this.showPluginManagerModal = true
},
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/RequestPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ export default {
},
{
name: 'Docs'
}
},
],
activeRequestPanelTab: 'Body',
methods: [
Expand Down
40 changes: 40 additions & 0 deletions packages/ui/src/components/modals/LogsModal.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<template>
<div v-if="showModalComp">
<modal :title="title" v-model="showModalComp">
<table style="table-layout: fixed">
<tr v-for="(log, index) in consoleLogs" :key="index">
<td>
{{ log.message }}
</td>
</tr>
</table>
</modal>
</div>
</template>

<script>
import { mapState } from 'vuex'
import Modal from '@/components/Modal.vue'

export default {
components: {Modal},
props: {
showModal: Boolean,
},
computed: {
title() {
return 'Logs'
},
showModalComp: {
get() {
return this.showModal
},
set(value) {
this.$emit('update:showModal', value)
}
},
...mapState(['consoleLogs']),
},
}
</script>

1 change: 1 addition & 0 deletions packages/ui/src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export interface RequestFinalResponse {
}

export interface State {
consoleLogs: string[]
collection: CollectionItem[]
collectionTree: CollectionItem[]
tabs: CollectionItem[]
Expand Down
10 changes: 7 additions & 3 deletions packages/ui/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ export async function handleRequest(

return responseToSend
} catch(e: any) {
console.log(e)
console.error(e)

let error = 'Error: Request failed'

Expand Down Expand Up @@ -1578,8 +1578,8 @@ export function setEnvironmentVariable(store: ActionContext<State, State>, objec
environments: environmentsToModify
})
} catch(e) {
console.log('Failed to set environment variable:')
console.log(e)
console.error('Failed to set environment variable:')
console.error(e)
}
}

Expand Down Expand Up @@ -1611,6 +1611,10 @@ export function getAlertConfirmPromptContainer(componentRootElement: HTMLElement
return (componentRootElement.ownerDocument?.defaultView ?? window).document.querySelector('alert-confirm-prompt') as any
}

export function getCurrentTimestamp(): string {
return dayjs().format('HH:mm:ss:SSS')
}

export function getVersion(): string {
return version
}
62 changes: 54 additions & 8 deletions packages/ui/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import store from './store'
import App from './App.vue'
import VueToast from 'vue-toast-notification'
import 'vue-toast-notification/dist/theme-default.css'
import dayjs from 'dayjs'
import { getCurrentTimestamp } from '@/helpers'

const app = createApp(App)

Expand All @@ -12,12 +12,58 @@ app.use(VueToast)

app.mount('#app')

const originalConsoleLog = console.log
console.log = (...args) => {
const timestamp = dayjs().format('HH:mm:ss:SSS')
const timestampStyle = 'color: #4CAF50;'
const logMessage = `%c${timestamp} %c`
const resetStyle = 'color: inherit;'
interface ConsoleMethod {
(...args: any[]): void
}

interface OriginalConsoleMethods {
log: ConsoleMethod
warn: ConsoleMethod
error: ConsoleMethod
info: ConsoleMethod
}

originalConsoleLog(logMessage, timestampStyle, resetStyle, ...args)
type LogType = 'log' | 'warn' | 'error' | 'info'

const originalConsoleMethods: OriginalConsoleMethods = {
log: console.log,
warn: console.warn,
error: console.error,
info: console.info
}

function interceptConsole(type: LogType): ConsoleMethod {
return (...args: any[]) => {
const timestampStyle = 'color: #4CAF50;'
const logMessage = `%c${getCurrentTimestamp()} - [${type.toUpperCase()}] - %c`
const resetStyle = 'color: inherit;'

originalConsoleMethods[type](logMessage, timestampStyle, resetStyle, ...args)
storeLog(type, args)
}
}

console.log = interceptConsole('log')
console.warn = interceptConsole('warn')
console.error = interceptConsole('error')
console.info = interceptConsole('info')

function storeLog(type: LogType, args: any[]): void {
try {
store.commit('addConsoleLog', { type, message: `${getCurrentTimestamp()} - [${type.toUpperCase()}] - ${argsMapping(args)}` })
} catch (error) {
console.error('Failed to store log:', error)
}
}

function argsMapping(args: any[]): string {
return args.map(arg => {
if (arg instanceof Error) {
return arg.message
} else if (typeof arg === 'object') {
return JSON.stringify(arg, null, 2)
} else {
return arg
}
}).join(' ')
}
7 changes: 7 additions & 0 deletions packages/ui/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ const store = createStore<State>({
tabEnvironmentResolved: {},
idMap: null,
skipPersistingActiveTab: false,
consoleLogs: [],
}
},
getters: {
Expand Down Expand Up @@ -663,6 +664,12 @@ const store = createStore<State>({
detachTab(state, tab) {
state.detachedTabs.push(tab)
},
addConsoleLog(state, log: string) {
state.consoleLogs.push(log)
},
clearConsoleLogs(state) {
state.consoleLogs = []
},
},
actions: {
addTab(context, tab) {
Expand Down
Loading