parent
f3053ba01b
commit
1050a04a88
|
@ -118,14 +118,14 @@ func (c *Color) sequence() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Color) wrap(s string) string {
|
func (c *Color) wrap(s string) string {
|
||||||
return c.format() + s + c.unformat()
|
return c.format() + s + unformat()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Color) format() string {
|
func (c *Color) format() string {
|
||||||
return fmt.Sprintf("%s[%sm", escape, c.sequence())
|
return fmt.Sprintf("%s[%sm", escape, c.sequence())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Color) unformat() string {
|
func unformat() string {
|
||||||
return fmt.Sprintf("%s[%dm", escape, Reset)
|
return fmt.Sprintf("%s[%dm", escape, Reset)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,6 +136,22 @@ func (c *Color) Format(text string) string {
|
||||||
return text
|
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
|
// ParseLevel parses a string and returns a Beaver Level's Color, defaulting to Info
|
||||||
func ParseLevel(level string) *Color {
|
func ParseLevel(level string) *Color {
|
||||||
switch strings.ToUpper(level) {
|
switch strings.ToUpper(level) {
|
||||||
|
@ -156,12 +172,18 @@ func ParseLevel(level string) *Color {
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Trace = New(Bold, FgCyan)
|
Trace = New(FgCyan)
|
||||||
Debug = New(Bold, FgBlue)
|
Tracef = New(Bold, FgCyan)
|
||||||
Info = New(Bold, FgGreen)
|
Debug = New(FgBlue)
|
||||||
Warn = New(Bold, FgYellow)
|
Debugf = New(Bold, FgBlue)
|
||||||
Error = New(Bold, FgRed)
|
Info = New(FgGreen)
|
||||||
Fatal = New(Bold, BgRed)
|
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()
|
Default = New()
|
||||||
Time = Default
|
Time = Default
|
||||||
Stack = Default
|
Stack = Default
|
||||||
|
|
25
level.go
25
level.go
|
@ -8,7 +8,10 @@ import (
|
||||||
"go.jolheiser.com/beaver/color"
|
"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
|
// Level defines a Beaver logging level
|
||||||
type Level int
|
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
|
// Levels returns a list of Beaver logging levels
|
||||||
func Levels() []Level {
|
func Levels() []Level {
|
||||||
return []Level{TRACE, DEBUG, INFO, WARN, ERROR, FATAL}
|
return []Level{TRACE, DEBUG, INFO, WARN, ERROR, FATAL}
|
||||||
|
|
11
logger.go
11
logger.go
|
@ -5,7 +5,6 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.jolheiser.com/beaver/color"
|
"go.jolheiser.com/beaver/color"
|
||||||
|
@ -27,6 +26,7 @@ type FormatOptions struct {
|
||||||
LevelPrefix bool
|
LevelPrefix bool
|
||||||
LevelColor bool
|
LevelColor bool
|
||||||
MessageColor bool
|
MessageColor bool
|
||||||
|
StyleArgs bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a new Beaver logger
|
// 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{}) {
|
func (l *Logger) log(level Level, v ...interface{}) {
|
||||||
var args = make([]string, len(v))
|
text := fmt.Sprint(v...)
|
||||||
for i := 0; i < len(v); i++ {
|
|
||||||
args[i] = fmt.Sprintf("%v", v[i])
|
|
||||||
}
|
|
||||||
text := strings.Join(args, " ")
|
|
||||||
l.write(level, text)
|
l.write(level, text)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Logger) logf(level Level, format string, v ...interface{}) {
|
func (l *Logger) logf(level Level, format string, v ...interface{}) {
|
||||||
text := fmt.Sprintf(format, v...)
|
text := fmt.Sprintf(format, v...)
|
||||||
|
if l.Format.StyleArgs {
|
||||||
|
|
||||||
|
}
|
||||||
l.write(level, text)
|
l.write(level, text)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in New Issue