diff --git a/color/color.go b/color/color.go index 27b06ea..cc83b7c 100644 --- a/color/color.go +++ b/color/color.go @@ -118,14 +118,14 @@ func (c *Color) sequence() string { } func (c *Color) wrap(s string) string { - return c.format() + s + c.unformat() + return c.format() + s + unformat() } func (c *Color) format() string { return fmt.Sprintf("%s[%sm", escape, c.sequence()) } -func (c *Color) unformat() string { +func unformat() string { return fmt.Sprintf("%s[%dm", escape, Reset) } @@ -136,6 +136,22 @@ func (c *Color) Format(text string) string { return text } +func (c *Color) Formatf(format string, v ...interface{}) string { + return c.Format(fmt.Sprintf(format, v...)) +} + +func (c *Color) Wrap(color *Color, text string) string { + return unformat() + c.Format(text) + color.format() +} + +func (c *Color) Wrapf(color *Color, format string, v ...interface{}) string { + args := make([]interface{}, len(v)) + for idx, arg := range v { + args[idx] = color.Wrap(c, fmt.Sprintf("%v", arg)) + } + return unformat() + c.Formatf(format, args...) + color.format() +} + // ParseLevel parses a string and returns a Beaver Level's Color, defaulting to Info func ParseLevel(level string) *Color { switch strings.ToUpper(level) { @@ -156,12 +172,18 @@ func ParseLevel(level string) *Color { } var ( - Trace = New(Bold, FgCyan) - Debug = New(Bold, FgBlue) - Info = New(Bold, FgGreen) - Warn = New(Bold, FgYellow) - Error = New(Bold, FgRed) - Fatal = New(Bold, BgRed) + Trace = New(FgCyan) + Tracef = New(Bold, FgCyan) + Debug = New(FgBlue) + Debugf = New(Bold, FgBlue) + Info = New(FgGreen) + Infof = New(Bold, FgGreen) + Warn = New(FgYellow) + Warnf = New(Bold, FgYellow) + Error = New(FgRed) + Errorf = New(Bold, FgRed) + Fatal = New(BgRed) + Fatalf = New(Bold, BgRed) Default = New() Time = Default Stack = Default diff --git a/level.go b/level.go index 314121a..c8a16a4 100644 --- a/level.go +++ b/level.go @@ -8,7 +8,10 @@ import ( "go.jolheiser.com/beaver/color" ) -var bold = color.New(color.Bold) +var ( + bold = color.New(color.Bold) + italic = color.New(color.Italic) +) // Level defines a Beaver logging level type Level int @@ -82,6 +85,26 @@ func (l Level) Color() *color.Color { } } +// Color returns a Level's colorf, defaulting to italic +func (l Level) Colorf() *color.Color { + switch l { + case TRACE: + return color.Tracef + case DEBUG: + return color.Debugf + case INFO: + return color.Infof + case WARN: + return color.Warnf + case ERROR: + return color.Errorf + case FATAL: + return color.Fatalf + default: + return italic + } +} + // Levels returns a list of Beaver logging levels func Levels() []Level { return []Level{TRACE, DEBUG, INFO, WARN, ERROR, FATAL} diff --git a/logger.go b/logger.go index 4e8b3e5..75fb93c 100644 --- a/logger.go +++ b/logger.go @@ -5,7 +5,6 @@ import ( "io" "os" "runtime" - "strings" "time" "go.jolheiser.com/beaver/color" @@ -27,6 +26,7 @@ type FormatOptions struct { LevelPrefix bool LevelColor bool MessageColor bool + StyleArgs bool } // New returns a new Beaver logger @@ -75,16 +75,15 @@ func (l *Logger) write(level Level, text string) { } func (l *Logger) log(level Level, v ...interface{}) { - var args = make([]string, len(v)) - for i := 0; i < len(v); i++ { - args[i] = fmt.Sprintf("%v", v[i]) - } - text := strings.Join(args, " ") + text := fmt.Sprint(v...) l.write(level, text) } func (l *Logger) logf(level Level, format string, v ...interface{}) { text := fmt.Sprintf(format, v...) + if l.Format.StyleArgs { + + } l.write(level, text) }