Add wrapping

Signed-off-by: jolheiser <john.olheiser@gmail.com>
pull/2/head
jolheiser 2020-02-25 21:57:44 -06:00
parent f3053ba01b
commit 1050a04a88
Signed by: jolheiser
GPG Key ID: B853ADA5DA7BBF7A
3 changed files with 59 additions and 15 deletions

View File

@ -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

View File

@ -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}

View File

@ -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)
}