add error level logger similar to Info() for console (#12445)

This commit is contained in:
Harshavardhana 2021-06-04 11:11:30 -07:00 committed by GitHub
parent d524544494
commit c0e41356f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -43,9 +43,9 @@ func consoleLog(console Logger, msg string, args ...interface{}) {
msg = ansiRE.ReplaceAllLiteralString(msg, "") msg = ansiRE.ReplaceAllLiteralString(msg, "")
console.json(msg, args...) console.json(msg, args...)
case quietFlag: case quietFlag:
console.quiet(msg, args...) console.quiet(msg+"\n", args...)
default: default:
console.pretty(msg, args...) console.pretty(msg+"\n", args...)
} }
} }
@ -71,10 +71,17 @@ type fatalMsg struct {
} }
func (f fatalMsg) json(msg string, args ...interface{}) { func (f fatalMsg) 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{ logJSON, err := json.Marshal(&log.Entry{
Level: FatalLvl.String(), Level: FatalLvl.String(),
Time: time.Now().UTC().Format(time.RFC3339Nano), Message: message,
Trace: &log.Trace{Message: fmt.Sprintf(msg, args...), Source: []string{getSource(6)}}, Time: time.Now().UTC().Format(time.RFC3339Nano),
Trace: &log.Trace{Message: message, Source: []string{getSource(6)}},
}) })
if err != nil { if err != nil {
panic(err) panic(err)
@ -143,9 +150,15 @@ type infoMsg struct{}
var info infoMsg var info infoMsg
func (i infoMsg) json(msg string, args ...interface{}) { func (i infoMsg) 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{ logJSON, err := json.Marshal(&log.Entry{
Level: InformationLvl.String(), Level: InformationLvl.String(),
Message: fmt.Sprintf(msg, args...), Message: message,
Time: time.Now().UTC().Format(time.RFC3339Nano), Time: time.Now().UTC().Format(time.RFC3339Nano),
}) })
if err != nil { if err != nil {
@ -159,12 +172,54 @@ func (i infoMsg) quiet(msg string, args ...interface{}) {
} }
func (i infoMsg) pretty(msg string, args ...interface{}) { func (i infoMsg) pretty(msg string, args ...interface{}) {
if msg == "" {
c.Println(args...)
}
c.Printf(msg, args...) c.Printf(msg, args...)
} }
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,
Time: time.Now().UTC().Format(time.RFC3339Nano),
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...)
}
// Info : // Info :
func Info(msg string, data ...interface{}) { func Info(msg string, data ...interface{}) {
consoleLog(info, msg+"\n", data...) consoleLog(info, msg, data...)
} }
var startupMessage startUpMsg var startupMessage startUpMsg
@ -184,5 +239,5 @@ func (s startUpMsg) pretty(msg string, args ...interface{}) {
// StartupMessage : // StartupMessage :
func StartupMessage(msg string, data ...interface{}) { func StartupMessage(msg string, data ...interface{}) {
consoleLog(startupMessage, msg+"\n", data...) consoleLog(startupMessage, msg, data...)
} }