Skip to content
This repository has been archived by the owner on May 2, 2024. It is now read-only.

Commit

Permalink
Fix persistent formatting after output (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
athyk authored Jul 17, 2023
1 parent 6657146 commit 5d9357e
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions simple_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ var std = New(log.LstdFlags)
// The prefix is followed by a colon only when Llongfile or Lshortfile
// is specified.
// For example, flags Ldate | Ltime (or LstdFlags) produce,
//
// 2009/01/23 01:23:23 message
//
// while flags Ldate | Ltime | Lmicroseconds | Llongfile produce,
//
// 2009/01/23 01:23:23.123123 /a/b/c/d.go:23: message
const (
Ldate = 1 << iota // the date in the local time zone: 2009/01/23
Expand Down Expand Up @@ -90,12 +93,12 @@ func SetLevelColor(level Level, color Style) {
Styles[level] = color
}

//Default returns the default SimpleLogger
// Default returns the default SimpleLogger
func Default() *SimpleLogger {
return std
}

//SetDefault sets the default SimpleLogger
// SetDefault sets the default SimpleLogger
func SetDefault(logger *SimpleLogger) {
std = logger
}
Expand Down Expand Up @@ -140,23 +143,25 @@ func (l *SimpleLogger) Output(calldepth int, level Level, v ...any) {

levelStr := level.String() + " "
textStyleStr := ""
endStyleStr := ""
if EnableColors {
levelStr = LevelStyle.And(Styles[level]).Apply(levelStr)
textStyleStr = TextStyle.String()
endStyleStr = StyleReset.String()
}
v[0] = levelStr
v[1] = textStyleStr

s := fmt.Sprint(v...) + endStyleStr
switch level {
case LevelFatal:
_ = l.logger.Output(calldepth, fmt.Sprint(v...))
_ = l.logger.Output(calldepth, s)
os.Exit(1)
case LevelPanic:
s := fmt.Sprint(v...)
_ = l.logger.Output(calldepth, s)
panic(s)
default:
_ = l.logger.Output(calldepth, fmt.Sprint(v...))
_ = l.logger.Output(calldepth, s)
}
}

Expand Down

0 comments on commit 5d9357e

Please sign in to comment.