Move to Go modules (#10)

This commit is contained in:
2019-10-13 08:55:38 +00:00
committed by Gitea
parent 2159aadfcb
commit 6207229e9b
478 changed files with 148676 additions and 20886 deletions

View File

@@ -1,6 +1,6 @@
The MIT License (MIT)
Copyright (c) 2015 labstack
Copyright (c) 2018 labstack
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"regexp"
"strconv"
"strings"
)
type (
@@ -22,7 +23,7 @@ const (
)
var (
pattern = regexp.MustCompile(`(?i)^(-?\d+(?:\.\d+)?)([KMGTPE]B?|B?)$`)
pattern = regexp.MustCompile(`(?i)^(-?\d+(?:\.\d+)?)\s?([KMGTPE]B?|B?)$`)
global = New()
)
@@ -73,7 +74,7 @@ func (*Bytes) Parse(value string) (i int64, err error) {
return 0, fmt.Errorf("error parsing value=%s", value)
}
bytesString := parts[1]
multiple := parts[2]
multiple := strings.ToUpper(parts[2])
bytes, err := strconv.ParseFloat(bytesString, 64)
if err != nil {
return

View File

@@ -10,6 +10,7 @@ import (
"runtime"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/mattn/go-isatty"
@@ -21,7 +22,8 @@ import (
type (
Logger struct {
prefix string
level Lvl
level uint32
skip int
output io.Writer
template *fasttemplate.Template
levels []string
@@ -51,9 +53,14 @@ var (
`"file":"${short_file}","line":"${line}"}`
)
func init() {
global.skip = 3
}
func New(prefix string) (l *Logger) {
l = &Logger{
level: INFO,
level: uint32(INFO),
skip: 2,
prefix: prefix,
template: l.newTemplate(defaultHeader),
color: color.New(),
@@ -104,11 +111,11 @@ func (l *Logger) SetPrefix(p string) {
}
func (l *Logger) Level() Lvl {
return l.level
return Lvl(atomic.LoadUint32(&l.level))
}
func (l *Logger) SetLevel(v Lvl) {
l.level = v
func (l *Logger) SetLevel(level Lvl) {
atomic.StoreUint32(&l.level, uint32(level))
}
func (l *Logger) Output() io.Writer {
@@ -213,7 +220,7 @@ func (l *Logger) Panic(i ...interface{}) {
func (l *Logger) Panicf(format string, args ...interface{}) {
l.log(panicLevel, format, args...)
panic(fmt.Sprintf(format, args))
panic(fmt.Sprintf(format, args...))
}
func (l *Logger) Panicj(j JSON) {
@@ -241,8 +248,8 @@ func Level() Lvl {
return global.Level()
}
func SetLevel(v Lvl) {
global.SetLevel(v)
func SetLevel(level Lvl) {
global.SetLevel(level)
}
func Output() io.Writer {
@@ -341,16 +348,14 @@ func Panicj(j JSON) {
global.Panicj(j)
}
func (l *Logger) log(v Lvl, format string, args ...interface{}) {
l.mutex.Lock()
defer l.mutex.Unlock()
buf := l.bufferPool.Get().(*bytes.Buffer)
buf.Reset()
defer l.bufferPool.Put(buf)
_, file, line, _ := runtime.Caller(2)
if v >= l.level || v == 0 {
func (l *Logger) log(level Lvl, format string, args ...interface{}) {
if level >= l.Level() || level == 0 {
buf := l.bufferPool.Get().(*bytes.Buffer)
buf.Reset()
defer l.bufferPool.Put(buf)
_, file, line, _ := runtime.Caller(l.skip)
message := ""
if format == "" {
message = fmt.Sprint(args...)
} else if format == "json" {
@@ -370,7 +375,7 @@ func (l *Logger) log(v Lvl, format string, args ...interface{}) {
case "time_rfc3339_nano":
return w.Write([]byte(time.Now().Format(time.RFC3339Nano)))
case "level":
return w.Write([]byte(l.levels[v]))
return w.Write([]byte(l.levels[level]))
case "prefix":
return w.Write([]byte(l.prefix))
case "long_file":
@@ -403,6 +408,8 @@ func (l *Logger) log(v Lvl, format string, args ...interface{}) {
buf.WriteString(message)
}
buf.WriteByte('\n')
l.mutex.Lock()
defer l.mutex.Unlock()
l.output.Write(buf.Bytes())
}
}