Add library and command for human-readable logs (#9809)
* Update logrus to 1.2 and add as a direct dependency * Create an mlog/human package for pretty-printing logs This package can read JSON logs from mattermost.log, and output the data to either logrus or a custom formatter, to make the logs more human readable. * Create a command for outputting human-readable logs This command will read JSON data from mattermost.log or stdin, and output in a human readable format. An optional argument can be used to activate logrus output (which includes color support). * Reorganize code in mlog/human and improve logrus timestamp formatting
Этот коммит содержится в:
коммит произвёл
Christopher Speller
родитель
e67d89b9a8
Коммит
8d56fcf568
52
cmd/mattermost/commands/logs.go
Обычный файл
52
cmd/mattermost/commands/logs.go
Обычный файл
@@ -0,0 +1,52 @@
|
||||
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package commands
|
||||
|
||||
import (
|
||||
"github.com/mattermost/mattermost-server/mlog/human"
|
||||
"github.com/spf13/cobra"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
var LogsCmd = &cobra.Command{
|
||||
Use: "logs",
|
||||
Short: "Display logs in a human-readable format",
|
||||
RunE: logsCmdF,
|
||||
}
|
||||
|
||||
func init() {
|
||||
LogsCmd.Flags().Bool("logrus", false, "Use logrus for formatting.")
|
||||
RootCmd.AddCommand(LogsCmd)
|
||||
}
|
||||
|
||||
func logsCmdF(command *cobra.Command, args []string) error {
|
||||
// check stdin to see if we have a pipe
|
||||
fi, err := os.Stdin.Stat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var input io.Reader
|
||||
if fi.Size() == 0 && fi.Mode()&os.ModeNamedPipe == 0 {
|
||||
file, err := os.Open("mattermost.log")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
input = file
|
||||
} else {
|
||||
input = os.Stdin
|
||||
}
|
||||
var writer human.LogWriter
|
||||
|
||||
if flag, _ := command.Flags().GetBool("logrus"); flag {
|
||||
writer = human.NewLogrusWriter(os.Stdout)
|
||||
} else {
|
||||
writer = human.NewSimpleWriter(os.Stdout)
|
||||
}
|
||||
human.ProcessLogs(input, writer)
|
||||
|
||||
return nil
|
||||
}
|
||||
Ссылка в новой задаче
Block a user