Skip to content

Commit ea3f1cd

Browse files
committed
feat: read nginx access logs and error logs
1 parent 6e3004b commit ea3f1cd

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

frontend/src/routes/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,18 @@ export const routes = [
9191
{
9292
path: 'nginx_log',
9393
name: () => $gettext('Nginx Log'),
94-
component: () => import('@/views/nginx_log/NginxLog.vue'),
9594
meta: {
9695
icon: FileTextOutlined
97-
}
96+
},
97+
children: [{
98+
path: 'access',
99+
name: () => $gettext('Access Logs'),
100+
component: () => import('@/views/nginx_log/NginxLog.vue'),
101+
}, {
102+
path: 'error',
103+
name: () => $gettext('Error Logs'),
104+
component: () => import('@/views/nginx_log/NginxLog.vue'),
105+
}]
98106
},
99107
{
100108
path: 'about',

frontend/src/views/nginx_log/NginxLog.vue

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
<script setup lang="ts">
22
import {useGettext} from 'vue3-gettext'
33
import ws from '@/lib/websocket'
4-
import {nextTick, onMounted, reactive, ref, watch} from 'vue'
4+
import {computed, nextTick, onMounted, onUnmounted, reactive, ref, watch} from 'vue'
55
import ReconnectingWebSocket from 'reconnecting-websocket'
6+
import {useRoute} from 'vue-router'
67
78
const {$gettext} = useGettext()
89
910
const logContainer = ref(null)
1011
1112
let websocket: ReconnectingWebSocket | WebSocket
13+
const route = useRoute()
14+
15+
function logType() {
16+
return route.path.indexOf('access') > 0 ? 'access' : 'error'
17+
}
1218
1319
const control = reactive({
14-
fetch: 'new'
20+
fetch: 'new',
21+
type: logType()
1522
})
1623
1724
function openWs() {
1825
websocket = ws('/api/nginx_log')
1926
websocket.send(JSON.stringify(control))
20-
websocket.onopen = () => {
21-
(logContainer.value as any as Element).innerHTML = ''
22-
}
2327
websocket.onmessage = (m: any) => {
2428
const para = document.createElement('p')
2529
para.appendChild(document.createTextNode(m.data.trim()));
@@ -42,12 +46,23 @@ const auto_refresh = ref(true)
4246
4347
watch(auto_refresh, (value) => {
4448
if (value) {
45-
openWs()
49+
openWs();
50+
(logContainer.value as any as Element).innerHTML = ''
51+
4652
} else {
4753
websocket.close()
4854
}
4955
})
5056
57+
watch(route, () => {
58+
control.type = logType();
59+
(logContainer.value as any as Element).innerHTML = ''
60+
61+
nextTick(() => {
62+
websocket.send(JSON.stringify(control))
63+
})
64+
})
65+
5166
watch(control, () => {
5267
(logContainer.value as any as Element).innerHTML = ''
5368
auto_refresh.value = true
@@ -57,6 +72,10 @@ watch(control, () => {
5772
})
5873
})
5974
75+
onUnmounted(() => {
76+
websocket.close()
77+
})
78+
6079
</script>
6180

6281
<template>

server/api/nginx_log.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
type controlStruct struct {
1616
Fetch string `json:"fetch"`
17+
Type string `json:"type"`
1718
}
1819

1920
func tailNginxLog(ws *websocket.Conn, controlChan chan controlStruct, errChan chan error) {
@@ -33,10 +34,15 @@ func tailNginxLog(ws *websocket.Conn, controlChan chan controlStruct, errChan ch
3334
seek.Whence = io.SeekEnd
3435
}
3536

37+
logPath := settings.NginxLogSettings.AccessLogPath
38+
39+
if control.Type == "error" {
40+
logPath = settings.NginxLogSettings.ErrorLogPath
41+
}
42+
3643
// Create a tail
37-
t, err := tail.TailFile(
38-
settings.NginxLogSettings.AccessLogPath, tail.Config{Follow: true,
39-
ReOpen: true, Location: &seek})
44+
t, err := tail.TailFile(logPath, tail.Config{Follow: true,
45+
ReOpen: true, Location: &seek})
4046

4147
if err != nil {
4248
errChan <- errors.Wrap(err, "error NginxAccessLog Tail")

0 commit comments

Comments
 (0)