2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
"github.com/minio/minio/internal/logger/message/log"
|
2021-05-28 18:17:01 -04:00
|
|
|
c "github.com/minio/pkg/console"
|
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"
|
|
|
|
|
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:
|
2021-06-04 14:11:30 -04:00
|
|
|
console.quiet(msg+"\n", args...)
|
2018-11-19 17:47:03 -05:00
|
|
|
default:
|
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{}) {
|
|
|
|
var errMsg string
|
|
|
|
if msg != "" {
|
|
|
|
errMsg = errorFmtFunc(fmt.Sprintf(msg, data...), err, jsonFlag)
|
|
|
|
} else {
|
|
|
|
errMsg = err.Error()
|
2018-07-19 18:55:06 -04:00
|
|
|
}
|
2018-11-19 17:47:03 -05:00
|
|
|
consoleLog(fatalMessage, errMsg)
|
|
|
|
}
|
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{
|
2021-06-04 14:11:30 -04:00
|
|
|
Level: FatalLvl.String(),
|
|
|
|
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
|
|
|
}
|
2018-11-19 17:47:03 -05:00
|
|
|
fmt.Println(string(logJSON))
|
2018-07-19 18:55:06 -04:00
|
|
|
|
2018-11-19 17:47:03 -05:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f fatalMsg) quiet(msg string, args ...interface{}) {
|
|
|
|
f.pretty(msg, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
logTag = "ERROR"
|
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 {
|
2020-01-13 16:05:51 -05:00
|
|
|
c.Print(logBanner)
|
2018-11-19 17:47:03 -05:00
|
|
|
tagPrinted = true
|
|
|
|
} else {
|
2020-01-13 16:05:51 -05:00
|
|
|
c.Print(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
|
2020-01-13 16:05:51 -05:00
|
|
|
c.Println(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
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
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{
|
|
|
|
Level: InformationLvl.String(),
|
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
|
|
|
}
|
2018-11-19 17:47:03 -05:00
|
|
|
fmt.Println(string(logJSON))
|
|
|
|
}
|
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 == "" {
|
|
|
|
c.Println(args...)
|
|
|
|
}
|
2018-11-19 17:47:03 -05:00
|
|
|
c.Printf(msg, args...)
|
|
|
|
}
|
|
|
|
|
2021-06-04 14:11:30 -04:00
|
|
|
type errorMsg struct{}
|
|
|
|
|
|
|
|
var errorm errorMsg
|
|
|
|
|
|
|
|
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{
|
|
|
|
Level: ErrorLvl.String(),
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
fmt.Println(string(logJSON))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i errorMsg) quiet(msg string, args ...interface{}) {
|
|
|
|
i.pretty(msg, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i errorMsg) pretty(msg string, args ...interface{}) {
|
|
|
|
if msg == "" {
|
|
|
|
c.Println(args...)
|
|
|
|
}
|
|
|
|
c.Printf(msg, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error :
|
|
|
|
func Error(msg string, data ...interface{}) {
|
|
|
|
consoleLog(errorm, msg, data...)
|
|
|
|
}
|
|
|
|
|
2018-11-19 17:47:03 -05:00
|
|
|
// Info :
|
|
|
|
func Info(msg string, data ...interface{}) {
|
2021-06-04 14:11:30 -04:00
|
|
|
consoleLog(info, msg, data...)
|
2018-11-19 17:47:03 -05:00
|
|
|
}
|