| 
									
										
										
										
											2021-04-19 03:41:13 +08: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-20 06:55:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | package logger | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2018-11-20 06:47:03 +08:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2018-07-20 06:55:06 +08:00
										 |  |  | 	"strings" | 
					
						
							|  |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2018-11-20 06:47:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-02 05:59:40 +08:00
										 |  |  | 	"github.com/minio/minio/internal/color" | 
					
						
							| 
									
										
										
										
											2021-05-29 06:17:01 +08:00
										 |  |  | 	c "github.com/minio/pkg/console" | 
					
						
							| 
									
										
										
										
											2023-02-22 13:21:17 +08:00
										 |  |  | 	"github.com/minio/pkg/logger/message/log" | 
					
						
							| 
									
										
										
										
											2018-07-20 06:55:06 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-18 09:50:10 +08:00
										 |  |  | // ConsoleLoggerTgt is a stringified value to represent console logging
 | 
					
						
							|  |  |  | const ConsoleLoggerTgt = "console+http" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-23 02:57:27 +08:00
										 |  |  | // ExitFunc is called by Fatal() class functions, by default it calls os.Exit()
 | 
					
						
							|  |  |  | var ExitFunc = os.Exit | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-08 13:47:56 +08:00
										 |  |  | // Logger interface describes the methods that need to be implemented to satisfy the interface requirements.
 | 
					
						
							|  |  |  | type Logger interface { | 
					
						
							| 
									
										
										
										
											2018-11-20 06:47:03 +08:00
										 |  |  | 	json(msg string, args ...interface{}) | 
					
						
							|  |  |  | 	quiet(msg string, args ...interface{}) | 
					
						
							|  |  |  | 	pretty(msg string, args ...interface{}) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-20 06:55:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-08 13:47:56 +08:00
										 |  |  | func consoleLog(console Logger, msg string, args ...interface{}) { | 
					
						
							| 
									
										
										
										
											2018-11-20 06:47:03 +08:00
										 |  |  | 	switch { | 
					
						
							|  |  |  | 	case jsonFlag: | 
					
						
							|  |  |  | 		// Strip escape control characters from json message
 | 
					
						
							|  |  |  | 		msg = ansiRE.ReplaceAllLiteralString(msg, "") | 
					
						
							|  |  |  | 		console.json(msg, args...) | 
					
						
							|  |  |  | 	case quietFlag: | 
					
						
							| 
									
										
										
										
											2021-06-05 02:11:30 +08:00
										 |  |  | 		console.quiet(msg+"\n", args...) | 
					
						
							| 
									
										
										
										
											2018-11-20 06:47:03 +08:00
										 |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2021-06-05 02:11:30 +08:00
										 |  |  | 		console.pretty(msg+"\n", args...) | 
					
						
							| 
									
										
										
										
											2018-10-13 03:25:59 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-20 06:47:03 +08: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-20 06:55:06 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-20 06:47:03 +08:00
										 |  |  | 	consoleLog(fatalMessage, errMsg) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-20 06:55:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-20 06:47:03 +08:00
										 |  |  | var fatalMessage fatalMsg | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-03 01:15:06 +08:00
										 |  |  | type fatalMsg struct{} | 
					
						
							| 
									
										
										
										
											2018-07-20 06:55:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-20 06:47:03 +08:00
										 |  |  | func (f fatalMsg) json(msg string, args ...interface{}) { | 
					
						
							| 
									
										
										
										
											2021-06-05 02:11:30 +08:00
										 |  |  | 	var message string | 
					
						
							|  |  |  | 	if msg != "" { | 
					
						
							|  |  |  | 		message = fmt.Sprintf(msg, args...) | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		message = fmt.Sprint(args...) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-20 06:47:03 +08:00
										 |  |  | 	logJSON, err := json.Marshal(&log.Entry{ | 
					
						
							| 
									
										
										
										
											2021-06-05 02:11:30 +08:00
										 |  |  | 		Level:   FatalLvl.String(), | 
					
						
							|  |  |  | 		Message: message, | 
					
						
							| 
									
										
										
										
											2021-12-24 07:33:54 +08:00
										 |  |  | 		Time:    time.Now().UTC(), | 
					
						
							| 
									
										
										
										
											2021-06-05 02:11:30 +08:00
										 |  |  | 		Trace:   &log.Trace{Message: message, Source: []string{getSource(6)}}, | 
					
						
							| 
									
										
										
										
											2018-11-20 06:47:03 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		panic(err) | 
					
						
							| 
									
										
										
										
											2018-07-20 06:55:06 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-20 06:47:03 +08:00
										 |  |  | 	fmt.Println(string(logJSON)) | 
					
						
							| 
									
										
										
										
											2018-07-20 06:55:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-23 02:57:27 +08:00
										 |  |  | 	ExitFunc(1) | 
					
						
							| 
									
										
										
										
											2018-11-20 06:47:03 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (f fatalMsg) quiet(msg string, args ...interface{}) { | 
					
						
							|  |  |  | 	f.pretty(msg, args...) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var ( | 
					
						
							|  |  |  | 	logTag      = "ERROR" | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | 	logBanner   = color.BgRed(color.FgWhite(color.Bold(logTag))) + " " | 
					
						
							|  |  |  | 	emptyBanner = color.BgRed(strings.Repeat(" ", len(logTag))) + " " | 
					
						
							| 
									
										
										
										
											2018-11-20 06:47:03 +08: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-14 05:05:51 +08:00
										 |  |  | 				c.Print(logBanner) | 
					
						
							| 
									
										
										
										
											2018-11-20 06:47:03 +08:00
										 |  |  | 				tagPrinted = true | 
					
						
							|  |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2020-01-14 05:05:51 +08:00
										 |  |  | 				c.Print(emptyBanner) | 
					
						
							| 
									
										
										
										
											2018-07-20 06:55:06 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-11-20 06:47:03 +08:00
										 |  |  | 			// Restore the text color of the error message
 | 
					
						
							|  |  |  | 			ansiRestoreAttributes() | 
					
						
							|  |  |  | 			ansiMoveRight(bannerWidth) | 
					
						
							|  |  |  | 			// Continue  error message printing
 | 
					
						
							| 
									
										
										
										
											2020-01-14 05:05:51 +08:00
										 |  |  | 			c.Println(line) | 
					
						
							| 
									
										
										
										
											2018-11-20 06:47:03 +08:00
										 |  |  | 			break | 
					
						
							| 
									
										
										
										
											2018-07-20 06:55:06 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-20 06:47:03 +08:00
										 |  |  | 	// Exit because this is a fatal error message
 | 
					
						
							| 
									
										
										
										
											2022-09-23 02:57:27 +08:00
										 |  |  | 	ExitFunc(1) | 
					
						
							| 
									
										
										
										
											2018-11-20 06:47:03 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-20 06:55:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-20 06:47:03 +08:00
										 |  |  | type infoMsg struct{} | 
					
						
							| 
									
										
										
										
											2018-07-20 06:55:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-20 06:47:03 +08:00
										 |  |  | var info infoMsg | 
					
						
							| 
									
										
										
										
											2018-07-20 06:55:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-20 06:47:03 +08:00
										 |  |  | func (i infoMsg) json(msg string, args ...interface{}) { | 
					
						
							| 
									
										
										
										
											2021-06-05 02:11:30 +08:00
										 |  |  | 	var message string | 
					
						
							|  |  |  | 	if msg != "" { | 
					
						
							|  |  |  | 		message = fmt.Sprintf(msg, args...) | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		message = fmt.Sprint(args...) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-20 06:47:03 +08:00
										 |  |  | 	logJSON, err := json.Marshal(&log.Entry{ | 
					
						
							| 
									
										
										
										
											2022-08-30 23:23:40 +08:00
										 |  |  | 		Level:   InfoLvl.String(), | 
					
						
							| 
									
										
										
										
											2021-06-05 02:11:30 +08:00
										 |  |  | 		Message: message, | 
					
						
							| 
									
										
										
										
											2021-12-24 07:33:54 +08:00
										 |  |  | 		Time:    time.Now().UTC(), | 
					
						
							| 
									
										
										
										
											2018-11-20 06:47:03 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		panic(err) | 
					
						
							| 
									
										
										
										
											2018-07-20 06:55:06 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-20 06:47:03 +08:00
										 |  |  | 	fmt.Println(string(logJSON)) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-20 06:55:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-20 06:47:03 +08:00
										 |  |  | func (i infoMsg) quiet(msg string, args ...interface{}) { | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-20 06:55:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-20 06:47:03 +08:00
										 |  |  | func (i infoMsg) pretty(msg string, args ...interface{}) { | 
					
						
							| 
									
										
										
										
											2021-06-05 02:11:30 +08:00
										 |  |  | 	if msg == "" { | 
					
						
							|  |  |  | 		c.Println(args...) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-20 06:47:03 +08:00
										 |  |  | 	c.Printf(msg, args...) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-05 02:11:30 +08: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-24 07:33:54 +08:00
										 |  |  | 		Time:    time.Now().UTC(), | 
					
						
							| 
									
										
										
										
											2021-06-05 02:11:30 +08: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{}) { | 
					
						
							| 
									
										
										
										
											2022-08-30 23:23:40 +08:00
										 |  |  | 	if MinimumLogLevel > ErrorLvl { | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-06-05 02:11:30 +08:00
										 |  |  | 	consoleLog(errorm, msg, data...) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-20 06:47:03 +08:00
										 |  |  | // Info :
 | 
					
						
							|  |  |  | func Info(msg string, data ...interface{}) { | 
					
						
							| 
									
										
										
										
											2022-08-30 23:23:40 +08:00
										 |  |  | 	if MinimumLogLevel > InfoLvl { | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-06-05 02:11:30 +08:00
										 |  |  | 	consoleLog(info, msg, data...) | 
					
						
							| 
									
										
										
										
											2018-11-20 06:47:03 +08:00
										 |  |  | } |