/
logs.go
89 lines (80 loc) · 2.26 KB
/
logs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package common
import (
"fmt"
"github.com/admpub/tail"
"github.com/webx-top/echo"
)
// LogParsers 日志格式解析器
var LogParsers = map[string]LogParser{}
type LogParser func(line *tail.Line) (interface{}, error)
// LogShow 获取日志内容用于显示
func LogShow(ctx echo.Context, logFile string, extensions ...echo.H) error {
data := ctx.Data()
var result echo.H
if len(extensions) > 0 {
result = extensions[0]
}
if result == nil {
result = echo.H{}
}
if len(logFile) == 0 {
result.Set(`content`, ctx.T(`没有日志文件`))
data.SetData(result)
return ctx.JSON(data)
}
lastLines := ctx.Formx(`lastLines`).Int()
config := tail.Config{
MustExist: true,
LastLines: 50,
}
if lastLines > 0 {
config.LastLines = lastLines
}
obj, err := tail.TailFile(logFile, config)
if err != nil {
data.SetError(fmt.Errorf(`%w: %s`, err, logFile))
} else {
pipe := ctx.Query(`pipe`)
if len(pipe) > 0 {
parser, ok := LogParsers[pipe]
if !ok {
return ctx.JSON(data.SetInfo(ctx.T(`Invalid pipe: %s`, pipe), 0))
}
rows := []interface{}{}
for line := range obj.Lines {
row, err := parser(line)
if err != nil {
return ctx.JSON(data.SetError(err))
}
if row == nil {
continue
}
rows = append(rows, row)
}
result.Set(`list`, rows)
data.SetData(result)
return ctx.JSON(data)
}
var content string
for line := range obj.Lines {
content += line.Text + "\n"
}
result.Set(`content`, content)
data.SetData(result)
}
return ctx.JSON(data)
}