Skip to content
Closed
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
64 changes: 46 additions & 18 deletions cmd/admin-logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@
},
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 @@
{{.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 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) {

Check failure on line 91 in cmd/admin-logs.go

View workflow job for this annotation

GitHub Actions / vetchecks

ST1017: don't use Yoda conditions (staticcheck)

Check failure on line 91 in cmd/admin-logs.go

View workflow job for this annotation

GitHub Actions / Test on Go 1.23.x and ubuntu-latest

ST1017: don't use Yoda conditions (staticcheck)
return true
}
}
return false
}

// Extend madmin.LogInfo to add String() and JSON() methods
type logMessage struct {
Status string `json:"status"`
Expand Down Expand Up @@ -133,25 +151,25 @@
if l.UserAgent != "" {
fmt.Fprintf(b, "\n%s UserAgent: %s", hostStr, l.UserAgent)
}
if l.Message != "" {
fmt.Fprintf(b, "\n%s Message: %s", hostStr, l.Message)
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 != 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.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 @@ -183,6 +201,13 @@
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 @@ -199,6 +224,9 @@
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