forked from knadh/listmonk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new 'Logs' page to the UI to view stdout logs
- Loading branch information
Showing
12 changed files
with
172 additions
and
8 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
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
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,56 @@ | ||
<template> | ||
<section class="logs content relative"> | ||
<h1 class="title is-4">Logs</h1> | ||
<hr /> | ||
<b-loading :active="loading.logs" :is-full-page="false" /> | ||
<pre class="lines" ref="lines"> | ||
<template v-for="(l, i) in lines"><span v-html="formatLine(l)" :key="i" class="line"></span> | ||
</template> | ||
</pre> | ||
</section> | ||
</template> | ||
|
||
<script> | ||
import Vue from 'vue'; | ||
import { mapState } from 'vuex'; | ||
const reFormatLine = new RegExp(/^(.*) (.+?)\.go:[0-9]+:\s/g); | ||
export default Vue.extend({ | ||
data() { | ||
return { | ||
lines: '', | ||
pollId: null, | ||
}; | ||
}, | ||
methods: { | ||
formatLine: (l) => l.replace(reFormatLine, '<span class="stamp">$1</span> '), | ||
getLogs() { | ||
this.$api.getLogs().then((data) => { | ||
this.lines = data; | ||
this.$nextTick(() => { | ||
this.$refs.lines.scrollTop = this.$refs.lines.scrollHeight; | ||
}); | ||
}); | ||
}, | ||
}, | ||
computed: { | ||
...mapState(['logs', 'loading']), | ||
}, | ||
mounted() { | ||
this.getLogs(); | ||
// Update the logs every 10 seconds. | ||
this.pollId = setInterval(() => this.getLogs(), 10000); | ||
}, | ||
destroyed() { | ||
clearInterval(this.pollId); | ||
}, | ||
}); | ||
</script> |
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,50 @@ | ||
package buflog | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
// BufLog implements a simple log buffer that can be supplied to a std | ||
// log instance. It stores logs up to N lines. | ||
type BufLog struct { | ||
maxLines int | ||
buf *bytes.Buffer | ||
lines []string | ||
|
||
sync.RWMutex | ||
} | ||
|
||
// New returns a new log buffer that stores up to maxLines lines. | ||
func New(maxLines int) *BufLog { | ||
return &BufLog{ | ||
maxLines: maxLines, | ||
buf: &bytes.Buffer{}, | ||
lines: make([]string, 0, maxLines), | ||
} | ||
} | ||
|
||
// Write writes a log item to the buffer maintaining maxLines capacity | ||
// using LIFO. | ||
func (bu *BufLog) Write(b []byte) (n int, err error) { | ||
bu.Lock() | ||
if len(bu.lines) >= bu.maxLines { | ||
bu.lines[0] = "" | ||
bu.lines = bu.lines[1:len(bu.lines)] | ||
} | ||
|
||
bu.lines = append(bu.lines, strings.TrimSpace(string(b))) | ||
bu.Unlock() | ||
return len(b), nil | ||
} | ||
|
||
// Lines returns the log lines. | ||
func (bu *BufLog) Lines() []string { | ||
bu.RLock() | ||
defer bu.RUnlock() | ||
|
||
out := make([]string, len(bu.lines)) | ||
copy(out[:], bu.lines[:]) | ||
return out | ||
} |