Compare commits
No commits in common. "main" and "v1.1.0" have entirely different histories.
|
@ -82,15 +82,9 @@ func (b *Basic) unformat() string {
|
||||||
return fmt.Sprintf("%s[%dm", escape, Reset)
|
return fmt.Sprintf("%s[%dm", escape, Reset)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format returns a string wrapped in the basic color
|
|
||||||
func (b *Basic) Format(text string) string {
|
func (b *Basic) Format(text string) string {
|
||||||
if len(b.Attrs) > 0 {
|
if len(b.Attrs) > 0 {
|
||||||
return b.wrap(text)
|
return b.wrap(text)
|
||||||
}
|
}
|
||||||
return text
|
return text
|
||||||
}
|
}
|
||||||
|
|
||||||
// Formatf returns a formatted string wrapped in the basic color
|
|
||||||
func (b *Basic) Formatf(format string, v ...interface{}) string {
|
|
||||||
return b.Format(fmt.Sprintf(format, v...))
|
|
||||||
}
|
|
||||||
|
|
|
@ -47,24 +47,3 @@ const (
|
||||||
BgHiCyan
|
BgHiCyan
|
||||||
BgHiWhite
|
BgHiWhite
|
||||||
)
|
)
|
||||||
|
|
||||||
var attrCache = make(map[BasicAttribute]*Basic)
|
|
||||||
|
|
||||||
func attr(a BasicAttribute) *Basic {
|
|
||||||
if c, ok := attrCache[a]; ok {
|
|
||||||
return c
|
|
||||||
}
|
|
||||||
c := New(a)
|
|
||||||
attrCache[a] = c
|
|
||||||
return c
|
|
||||||
}
|
|
||||||
|
|
||||||
// Format is a quick way to format a string using a single attribute
|
|
||||||
func (a BasicAttribute) Format(text string) string {
|
|
||||||
return attr(a).Format(text)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Format is a quick way to format a formatted string using a single attribute
|
|
||||||
func (a BasicAttribute) Formatf(text string, v ...interface{}) string {
|
|
||||||
return attr(a).Formatf(text, v...)
|
|
||||||
}
|
|
||||||
|
|
|
@ -8,7 +8,6 @@ const escape = "\x1b"
|
||||||
|
|
||||||
type Color interface {
|
type Color interface {
|
||||||
Format(text string) string
|
Format(text string) string
|
||||||
Formatf(text string, v ...interface{}) string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
|
|
|
@ -43,12 +43,6 @@ func (e *Extended) unformat() string {
|
||||||
return fmt.Sprintf("%s[%dm", escape, Reset)
|
return fmt.Sprintf("%s[%dm", escape, Reset)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format returns a string wrapped in the extended color
|
|
||||||
func (e *Extended) Format(text string) string {
|
func (e *Extended) Format(text string) string {
|
||||||
return e.wrap(text)
|
return e.wrap(text)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Formatf returns a formatted string wrapped in the extended color
|
|
||||||
func (e *Extended) Formatf(text string, v ...interface{}) string {
|
|
||||||
return e.wrap(fmt.Sprintf(text, v...))
|
|
||||||
}
|
|
||||||
|
|
|
@ -40,12 +40,6 @@ func (t *True) unformat() string {
|
||||||
return fmt.Sprintf("%s[%dm", escape, Reset)
|
return fmt.Sprintf("%s[%dm", escape, Reset)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format returns a string wrapped in the true color
|
|
||||||
func (t *True) Format(text string) string {
|
func (t *True) Format(text string) string {
|
||||||
return t.wrap(text)
|
return t.wrap(text)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Formatf returns a formatted string wrapped in the true color
|
|
||||||
func (t *True) Formatf(text string, v ...interface{}) string {
|
|
||||||
return t.wrap(fmt.Sprintf(text, v...))
|
|
||||||
}
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Console = &Logger{
|
Console = &Logger{
|
||||||
Writer: os.Stderr,
|
Writer: os.Stdout,
|
||||||
Level: INFO,
|
Level: INFO,
|
||||||
Format: FormatOptions{
|
Format: FormatOptions{
|
||||||
MessageColor: true,
|
MessageColor: true,
|
||||||
|
|
36
level.go
36
level.go
|
@ -3,6 +3,7 @@ package beaver
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"go.jolheiser.com/beaver/color"
|
"go.jolheiser.com/beaver/color"
|
||||||
)
|
)
|
||||||
|
@ -104,3 +105,38 @@ func ParseLevel(level string) Level {
|
||||||
}
|
}
|
||||||
return INFO
|
return INFO
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func timePrefix(t time.Time) string {
|
||||||
|
var buf = &[]byte{}
|
||||||
|
year, month, day := t.Date()
|
||||||
|
itoa(buf, int(month), 2)
|
||||||
|
*buf = append(*buf, '/')
|
||||||
|
itoa(buf, day, 2)
|
||||||
|
*buf = append(*buf, '/')
|
||||||
|
itoa(buf, year, 4)
|
||||||
|
*buf = append(*buf, ' ')
|
||||||
|
|
||||||
|
hour, min, sec := t.Clock()
|
||||||
|
itoa(buf, hour, 2)
|
||||||
|
*buf = append(*buf, ':')
|
||||||
|
itoa(buf, min, 2)
|
||||||
|
*buf = append(*buf, ':')
|
||||||
|
itoa(buf, sec, 2)
|
||||||
|
|
||||||
|
return string(*buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
func itoa(buf *[]byte, i int, wid int) {
|
||||||
|
var b [20]byte
|
||||||
|
bp := len(b) - 1
|
||||||
|
for i >= 10 || wid > 1 {
|
||||||
|
wid--
|
||||||
|
q := i / 10
|
||||||
|
b[bp] = byte('0' + i - q*10)
|
||||||
|
bp--
|
||||||
|
i = q
|
||||||
|
}
|
||||||
|
// i < 10
|
||||||
|
b[bp] = byte('0' + i)
|
||||||
|
*buf = append(*buf, b[bp:]...)
|
||||||
|
}
|
||||||
|
|
|
@ -42,7 +42,7 @@ func (l *Logger) write(level Level, text string) {
|
||||||
if l.Level <= level {
|
if l.Level <= level {
|
||||||
var message string
|
var message string
|
||||||
if l.Format.TimePrefix {
|
if l.Format.TimePrefix {
|
||||||
message += color.Time.Format(time.Now().Format("01/02/2006 15:04:05")) + " "
|
message += color.Time.Format(timePrefix(time.Now())) + " "
|
||||||
}
|
}
|
||||||
if l.Format.StackPrefix {
|
if l.Format.StackPrefix {
|
||||||
_, file, line, _ := runtime.Caller(l.skip)
|
_, file, line, _ := runtime.Caller(l.skip)
|
||||||
|
|
Reference in New Issue