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

admin log:add log level filter #5130

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
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
63 changes: 47 additions & 16 deletions cmd/admin-logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ var logsShowFlags = []cli.Flag{
},
cli.StringFlag{
Name: "type, t",
Usage: "list error logs by type. Valid options are '[minio, application, all]'",
Usage: "list logs by type. Valid options are '[minio, application, all]'",
Value: "all",
},
cli.StringSliceFlag{
Name: "level",
Usage: "list logs filter by log levels. Valid options are '[INFO, EVENT, ERROR, WARNING, FATAL, ALL]'",
},
}

var adminLogsCmd = cli.Command{
Expand All @@ -66,7 +70,9 @@ EXAMPLES:
{{.Prompt}} {{.HelpName}} myminio
2. Show last 5 log entries for node 'node1' for a MinIO server with alias 'myminio'
{{.Prompt}} {{.HelpName}} --last 5 myminio node1
3. Show application errors in logs for a MinIO server with alias 'myminio'
3. Show error logs for a MinIO server with alias 'myminio'
{{.Prompt}} {{.HelpName}} --level ERROR myminio
4. Show application logs for a MinIO server with alias 'myminio'
{{.Prompt}} {{.HelpName}} --type application myminio
`,
}
Expand All @@ -77,6 +83,18 @@ func checkLogsShowSyntax(ctx *cli.Context) {
}
}

func filterLogLevel(logLevels []string, level string) bool {
if len(logLevels) <= 0 {
return true
}
for _, logLevel := range logLevels {
if "ALL" == strings.ToUpper(logLevel) || level == strings.ToUpper(logLevel) {
return true
}
}
return false
}

// Extend madmin.LogInfo to add String() and JSON() methods
type logMessage struct {
Status string `json:"status"`
Expand Down Expand Up @@ -142,22 +160,25 @@ func (l logMessage) String() string {
if l.UserAgent != "" {
fmt.Fprintf(b, "\n%s UserAgent: %s", hostStr, l.UserAgent)
}
if l.Trace != nil {
if l.Trace.Message != "" {
fmt.Fprintf(b, "\n%s Error: %s", hostStr, console.Colorize("LogMessage", l.Trace.Message))
}
if l.Trace.Variables != nil {
for key, value := range l.Trace.Variables {
if value != "" {
fmt.Fprintf(b, "\n%s %s=%s", hostStr, key, value)
}
if l.Trace == nil {
fmt.Fprintf(b, "\n%s %s: %s", hostStr, l.Level, console.Colorize("LogMessage", l.Message))
logMsg := strings.TrimPrefix(b.String(), "\n")
return fmt.Sprintf("%s\n", logMsg)
}
if l.Trace.Message != "" {
fmt.Fprintf(b, "\n%s %s: %s", hostStr, l.Level, console.Colorize("LogMessage", l.Trace.Message))
}
if l.Trace.Variables != nil {
for key, value := range l.Trace.Variables {
if value != "" {
fmt.Fprintf(b, "\n%s %s=%s", hostStr, key, value)
}
}
if l.Trace.Source != nil {
traceLength := len(l.Trace.Source)
for i, element := range l.Trace.Source {
fmt.Fprintf(b, "\n%s %8v: %s", hostStr, traceLength-i, element)
}
}
if l.Trace.Source != nil {
traceLength := len(l.Trace.Source)
for i, element := range l.Trace.Source {
fmt.Fprintf(b, "\n%s %8v: %s", hostStr, traceLength-i, element)
}
}
logMsg := strings.TrimPrefix(b.String(), "\n")
Expand Down Expand Up @@ -189,6 +210,13 @@ func mainAdminLogs(ctx *cli.Context) error {
if logType != "minio" && logType != "application" && logType != "all" {
fatalIf(errInvalidArgument().Trace(ctx.Args()...), "Invalid value for --type flag. Valid options are [minio, application, all]")
}
logLevels := ctx.StringSlice("level")
for _, logLevel := range logLevels {
logLevel := strings.ToUpper(logLevel)
if logLevel != "INFO" && logLevel != "EVENT" && logLevel != "ERROR" && logLevel != "WARNING" && logLevel != "FATAL" && logLevel != "ALL" {
fatalIf(errInvalidArgument().Trace(ctx.Args()...), "Invalid value for --level flag. Valid options are [INFO, EVENT, ERROR, WARNING, FATAL, ALL]")
}
}
// Create a new MinIO Admin Client
client, err := newAdminClient(aliasedURL)
if err != nil {
Expand All @@ -205,6 +233,9 @@ func mainAdminLogs(ctx *cli.Context) error {
if logInfo.Err != nil {
fatalIf(probe.NewError(logInfo.Err), "Unable to listen to console logs")
}
if !filterLogLevel(logLevels, logInfo.Level) {
continue
}
// drop nodeName from output if specified as cli arg
if node != "" {
logInfo.NodeName = ""
Expand Down
Loading