2024-07-12 16:51:54 -04:00
|
|
|
// Copyright (c) 2015-2024 MinIO, Inc.
|
2021-04-18 15:41:13 -04:00
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// 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 <http://www.gnu.org/licenses/>.
|
2018-07-19 18:55:06 -04:00
|
|
|
|
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2018-11-19 17:47:03 -05:00
|
|
|
"os"
|
2018-07-19 18:55:06 -04:00
|
|
|
"strings"
|
|
|
|
"time"
|
2018-11-19 17:47:03 -05:00
|
|
|
|
2021-06-01 17:59:40 -04:00
|
|
|
"github.com/minio/minio/internal/color"
|
2024-05-24 19:05:23 -04:00
|
|
|
"github.com/minio/pkg/v3/logger/message/log"
|
2018-07-19 18:55:06 -04:00
|
|
|
)
|
|
|
|
|
2022-02-17 20:50:10 -05:00
|
|
|
// ConsoleLoggerTgt is a stringified value to represent console logging
|
|
|
|
const ConsoleLoggerTgt = "console+http"
|
|
|
|
|
2022-09-22 14:57:27 -04:00
|
|
|
// ExitFunc is called by Fatal() class functions, by default it calls os.Exit()
|
|
|
|
var ExitFunc = os.Exit
|
|
|
|
|
2019-10-08 01:47:56 -04:00
|
|
|
// Logger interface describes the methods that need to be implemented to satisfy the interface requirements.
|
|
|
|
type Logger interface {
|
2018-11-19 17:47:03 -05:00
|
|
|
json(msg string, args ...interface{})
|
|
|
|
quiet(msg string, args ...interface{})
|
|
|
|
pretty(msg string, args ...interface{})
|
|
|
|
}
|
2018-07-19 18:55:06 -04:00
|
|
|
|
2019-10-08 01:47:56 -04:00
|
|
|
func consoleLog(console Logger, msg string, args ...interface{}) {
|
2018-11-19 17:47:03 -05:00
|
|
|
switch {
|
|
|
|
case jsonFlag:
|
|
|
|
// Strip escape control characters from json message
|
|
|
|
msg = ansiRE.ReplaceAllLiteralString(msg, "")
|
|
|
|
console.json(msg, args...)
|
|
|
|
case quietFlag:
|
2023-10-02 09:39:47 -04:00
|
|
|
if len(msg) != 0 && len(args) == 0 {
|
|
|
|
args = append(args, msg)
|
|
|
|
msg = "%s"
|
|
|
|
}
|
2021-06-04 14:11:30 -04:00
|
|
|
console.quiet(msg+"\n", args...)
|
2018-11-19 17:47:03 -05:00
|
|
|
default:
|
2023-10-02 09:39:47 -04:00
|
|
|
if len(msg) != 0 && len(args) == 0 {
|
|
|
|
args = append(args, msg)
|
|
|
|
msg = "%s"
|
|
|
|
}
|
2021-06-04 14:11:30 -04:00
|
|
|
console.pretty(msg+"\n", args...)
|
2018-10-12 15:25:59 -04:00
|
|
|
}
|
2018-11-19 17:47:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fatal prints only fatal error message with no stack trace
|
|
|
|
// it will be called for input validation failures
|
|
|
|
func Fatal(err error, msg string, data ...interface{}) {
|
|
|
|
fatal(err, msg, data...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func fatal(err error, msg string, data ...interface{}) {
|
2024-05-19 04:06:49 -04:00
|
|
|
if msg == "" {
|
|
|
|
if len(data) > 0 {
|
|
|
|
msg = fmt.Sprint(data...)
|
|
|
|
} else {
|
|
|
|
msg = "a fatal error"
|
|
|
|
}
|
2018-11-19 17:47:03 -05:00
|
|
|
} else {
|
2024-05-19 04:06:49 -04:00
|
|
|
msg = fmt.Sprintf(msg, data...)
|
2018-07-19 18:55:06 -04:00
|
|
|
}
|
2024-05-19 04:06:49 -04:00
|
|
|
consoleLog(fatalMessage, errorFmtFunc(msg, err, jsonFlag))
|
2018-11-19 17:47:03 -05:00
|
|
|
}
|
2018-07-19 18:55:06 -04:00
|
|
|
|
2018-11-19 17:47:03 -05:00
|
|
|
var fatalMessage fatalMsg
|
|
|
|
|
2022-01-02 12:15:06 -05:00
|
|
|
type fatalMsg struct{}
|
2018-07-19 18:55:06 -04:00
|
|
|
|
2018-11-19 17:47:03 -05:00
|
|
|
func (f fatalMsg) json(msg string, args ...interface{}) {
|
2021-06-04 14:11:30 -04:00
|
|
|
var message string
|
|
|
|
if msg != "" {
|
|
|
|
message = fmt.Sprintf(msg, args...)
|
|
|
|
} else {
|
|
|
|
message = fmt.Sprint(args...)
|
|
|
|
}
|
2018-11-19 17:47:03 -05:00
|
|
|
logJSON, err := json.Marshal(&log.Entry{
|
2024-02-01 19:13:57 -05:00
|
|
|
Level: FatalKind,
|
2021-06-04 14:11:30 -04:00
|
|
|
Message: message,
|
2021-12-23 18:33:54 -05:00
|
|
|
Time: time.Now().UTC(),
|
2021-06-04 14:11:30 -04:00
|
|
|
Trace: &log.Trace{Message: message, Source: []string{getSource(6)}},
|
2018-11-19 17:47:03 -05:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2018-07-19 18:55:06 -04:00
|
|
|
}
|
2024-05-01 13:57:52 -04:00
|
|
|
fmt.Fprintln(Output, string(logJSON))
|
2022-09-22 14:57:27 -04:00
|
|
|
ExitFunc(1)
|
2018-11-19 17:47:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f fatalMsg) quiet(msg string, args ...interface{}) {
|
|
|
|
f.pretty(msg, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2024-07-12 16:51:54 -04:00
|
|
|
logTag = "FATAL"
|
2019-10-04 13:35:33 -04:00
|
|
|
logBanner = color.BgRed(color.FgWhite(color.Bold(logTag))) + " "
|
|
|
|
emptyBanner = color.BgRed(strings.Repeat(" ", len(logTag))) + " "
|
2018-11-19 17:47:03 -05:00
|
|
|
bannerWidth = len(logTag) + 1
|
|
|
|
)
|
|
|
|
|
|
|
|
func (f fatalMsg) pretty(msg string, args ...interface{}) {
|
|
|
|
// Build the passed error message
|
|
|
|
errMsg := fmt.Sprintf(msg, args...)
|
|
|
|
|
|
|
|
tagPrinted := false
|
|
|
|
|
|
|
|
// Print the error message: the following code takes care
|
|
|
|
// of splitting error text and always pretty printing the
|
|
|
|
// red banner along with the error message. Since the error
|
|
|
|
// message itself contains some colored text, we needed
|
|
|
|
// to use some ANSI control escapes to cursor color state
|
|
|
|
// and freely move in the screen.
|
|
|
|
for _, line := range strings.Split(errMsg, "\n") {
|
|
|
|
if len(line) == 0 {
|
|
|
|
// No more text to print, just quit.
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
// Save the attributes of the current cursor helps
|
|
|
|
// us save the text color of the passed error message
|
|
|
|
ansiSaveAttributes()
|
|
|
|
// Print banner with or without the log tag
|
|
|
|
if !tagPrinted {
|
2024-05-01 13:57:52 -04:00
|
|
|
fmt.Fprint(Output, logBanner)
|
2018-11-19 17:47:03 -05:00
|
|
|
tagPrinted = true
|
|
|
|
} else {
|
2024-05-01 13:57:52 -04:00
|
|
|
fmt.Fprint(Output, emptyBanner)
|
2018-07-19 18:55:06 -04:00
|
|
|
}
|
2018-11-19 17:47:03 -05:00
|
|
|
// Restore the text color of the error message
|
|
|
|
ansiRestoreAttributes()
|
|
|
|
ansiMoveRight(bannerWidth)
|
|
|
|
// Continue error message printing
|
2024-05-01 13:57:52 -04:00
|
|
|
fmt.Fprintln(Output, line)
|
2018-11-19 17:47:03 -05:00
|
|
|
break
|
2018-07-19 18:55:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-19 17:47:03 -05:00
|
|
|
// Exit because this is a fatal error message
|
2022-09-22 14:57:27 -04:00
|
|
|
ExitFunc(1)
|
2018-11-19 17:47:03 -05:00
|
|
|
}
|
2018-07-19 18:55:06 -04:00
|
|
|
|
2018-11-19 17:47:03 -05:00
|
|
|
type infoMsg struct{}
|
2018-07-19 18:55:06 -04:00
|
|
|
|
2018-11-19 17:47:03 -05:00
|
|
|
var info infoMsg
|
2018-07-19 18:55:06 -04:00
|
|
|
|
2018-11-19 17:47:03 -05:00
|
|
|
func (i infoMsg) json(msg string, args ...interface{}) {
|
2021-06-04 14:11:30 -04:00
|
|
|
var message string
|
|
|
|
if msg != "" {
|
|
|
|
message = fmt.Sprintf(msg, args...)
|
|
|
|
} else {
|
|
|
|
message = fmt.Sprint(args...)
|
|
|
|
}
|
2018-11-19 17:47:03 -05:00
|
|
|
logJSON, err := json.Marshal(&log.Entry{
|
2024-02-01 19:13:57 -05:00
|
|
|
Level: InfoKind,
|
2021-06-04 14:11:30 -04:00
|
|
|
Message: message,
|
2021-12-23 18:33:54 -05:00
|
|
|
Time: time.Now().UTC(),
|
2018-11-19 17:47:03 -05:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2018-07-19 18:55:06 -04:00
|
|
|
}
|
2024-05-01 13:57:52 -04:00
|
|
|
fmt.Fprintln(Output, string(logJSON))
|
2018-11-19 17:47:03 -05:00
|
|
|
}
|
2018-07-19 18:55:06 -04:00
|
|
|
|
2018-11-19 17:47:03 -05:00
|
|
|
func (i infoMsg) quiet(msg string, args ...interface{}) {
|
|
|
|
}
|
2018-07-19 18:55:06 -04:00
|
|
|
|
2018-11-19 17:47:03 -05:00
|
|
|
func (i infoMsg) pretty(msg string, args ...interface{}) {
|
2021-06-04 14:11:30 -04:00
|
|
|
if msg == "" {
|
2024-05-01 13:57:52 -04:00
|
|
|
fmt.Fprintln(Output, args...)
|
2024-05-19 04:06:49 -04:00
|
|
|
} else {
|
2024-07-12 16:51:54 -04:00
|
|
|
fmt.Fprintf(Output, `INFO: `+msg, args...)
|
2021-06-04 14:11:30 -04:00
|
|
|
}
|
2018-11-19 17:47:03 -05:00
|
|
|
}
|
|
|
|
|
2021-06-04 14:11:30 -04:00
|
|
|
type errorMsg struct{}
|
|
|
|
|
2024-05-19 04:06:49 -04:00
|
|
|
var errorMessage errorMsg
|
2021-06-04 14:11:30 -04:00
|
|
|
|
|
|
|
func (i errorMsg) json(msg string, args ...interface{}) {
|
|
|
|
var message string
|
|
|
|
if msg != "" {
|
|
|
|
message = fmt.Sprintf(msg, args...)
|
|
|
|
} else {
|
|
|
|
message = fmt.Sprint(args...)
|
|
|
|
}
|
|
|
|
logJSON, err := json.Marshal(&log.Entry{
|
2024-02-01 19:13:57 -05:00
|
|
|
Level: ErrorKind,
|
2021-06-04 14:11:30 -04:00
|
|
|
Message: message,
|
2021-12-23 18:33:54 -05:00
|
|
|
Time: time.Now().UTC(),
|
2021-06-04 14:11:30 -04:00
|
|
|
Trace: &log.Trace{Message: message, Source: []string{getSource(6)}},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-05-01 13:57:52 -04:00
|
|
|
fmt.Fprintln(Output, string(logJSON))
|
2021-06-04 14:11:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i errorMsg) quiet(msg string, args ...interface{}) {
|
|
|
|
i.pretty(msg, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i errorMsg) pretty(msg string, args ...interface{}) {
|
|
|
|
if msg == "" {
|
2024-05-01 13:57:52 -04:00
|
|
|
fmt.Fprintln(Output, args...)
|
2024-05-19 04:06:49 -04:00
|
|
|
} else {
|
2024-07-12 16:51:54 -04:00
|
|
|
fmt.Fprintf(Output, `ERRO: `+msg, args...)
|
2021-06-04 14:11:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error :
|
|
|
|
func Error(msg string, data ...interface{}) {
|
2024-07-12 16:51:54 -04:00
|
|
|
if DisableLog {
|
2022-08-30 11:23:40 -04:00
|
|
|
return
|
|
|
|
}
|
2024-05-19 04:06:49 -04:00
|
|
|
consoleLog(errorMessage, msg, data...)
|
2021-06-04 14:11:30 -04:00
|
|
|
}
|
|
|
|
|
2018-11-19 17:47:03 -05:00
|
|
|
// Info :
|
|
|
|
func Info(msg string, data ...interface{}) {
|
2024-07-12 16:51:54 -04:00
|
|
|
if DisableLog {
|
2022-08-30 11:23:40 -04:00
|
|
|
return
|
|
|
|
}
|
2021-06-04 14:11:30 -04:00
|
|
|
consoleLog(info, msg, data...)
|
2018-11-19 17:47:03 -05:00
|
|
|
}
|
2024-07-12 16:51:54 -04:00
|
|
|
|
|
|
|
// Startup :
|
|
|
|
func Startup(msg string, data ...interface{}) {
|
|
|
|
if DisableLog {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
consoleLog(startup, msg, data...)
|
|
|
|
}
|
|
|
|
|
|
|
|
type startupMsg struct{}
|
|
|
|
|
|
|
|
var startup startupMsg
|
|
|
|
|
|
|
|
func (i startupMsg) json(msg string, args ...interface{}) {
|
|
|
|
var message string
|
|
|
|
if msg != "" {
|
|
|
|
message = fmt.Sprintf(msg, args...)
|
|
|
|
} else {
|
|
|
|
message = fmt.Sprint(args...)
|
|
|
|
}
|
|
|
|
logJSON, err := json.Marshal(&log.Entry{
|
|
|
|
Level: InfoKind,
|
|
|
|
Message: message,
|
|
|
|
Time: time.Now().UTC(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
fmt.Fprintln(Output, string(logJSON))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i startupMsg) quiet(msg string, args ...interface{}) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i startupMsg) pretty(msg string, args ...interface{}) {
|
|
|
|
if msg == "" {
|
|
|
|
fmt.Fprintln(Output, args...)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(Output, msg, args...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type warningMsg struct{}
|
|
|
|
|
|
|
|
var warningMessage warningMsg
|
|
|
|
|
|
|
|
func (i warningMsg) json(msg string, args ...interface{}) {
|
|
|
|
var message string
|
|
|
|
if msg != "" {
|
|
|
|
message = fmt.Sprintf(msg, args...)
|
|
|
|
} else {
|
|
|
|
message = fmt.Sprint(args...)
|
|
|
|
}
|
|
|
|
logJSON, err := json.Marshal(&log.Entry{
|
|
|
|
Level: WarningKind,
|
|
|
|
Message: message,
|
|
|
|
Time: time.Now().UTC(),
|
|
|
|
Trace: &log.Trace{Message: message, Source: []string{getSource(6)}},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
fmt.Fprintln(Output, string(logJSON))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i warningMsg) quiet(msg string, args ...interface{}) {
|
|
|
|
i.pretty(msg, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i warningMsg) pretty(msg string, args ...interface{}) {
|
|
|
|
if msg == "" {
|
|
|
|
fmt.Fprintln(Output, args...)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(Output, `WARN: `+msg, args...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warning :
|
|
|
|
func Warning(msg string, data ...interface{}) {
|
|
|
|
if DisableLog {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
consoleLog(warningMessage, msg, data...)
|
|
|
|
}
|