tile38/internal/log/log.go

262 lines
4.7 KiB
Go
Raw Normal View History

2016-03-05 02:08:16 +03:00
package log
import (
"encoding/json"
2016-03-05 02:08:16 +03:00
"fmt"
"io"
"os"
"sync"
"sync/atomic"
2016-03-05 02:08:16 +03:00
"time"
"go.uber.org/zap"
"golang.org/x/term"
2016-03-05 02:08:16 +03:00
)
var wmu sync.Mutex
2017-10-03 18:49:33 +03:00
var wr io.Writer
var zmu sync.Mutex
var zlogger *zap.SugaredLogger
var tty atomic.Bool
var ljson atomic.Bool
var llevel atomic.Int32
func init() {
SetOutput(os.Stderr)
SetLevel(1)
}
2016-03-05 02:08:16 +03:00
2017-10-03 18:49:33 +03:00
// Level is the log level
// 0: silent - do not log
// 1: normal - show everything except debug and warn
// 2: verbose - show everything except debug
// 3: very verbose - show everything
func SetLevel(level int) {
if level < 0 {
level = 0
} else if level > 3 {
level = 3
}
llevel.Store(int32(level))
}
// Level returns the log level
func Level() int {
return int(llevel.Load())
}
func SetLogJSON(logJSON bool) {
ljson.Store(logJSON)
}
func LogJSON() bool {
return ljson.Load()
}
2016-03-05 02:08:16 +03:00
2017-10-03 18:49:33 +03:00
// SetOutput sets the output of the logger
func SetOutput(w io.Writer) {
f, ok := w.(*os.File)
tty.Store(ok && term.IsTerminal(int(f.Fd())))
wmu.Lock()
2017-10-03 18:49:33 +03:00
wr = w
wmu.Unlock()
2016-03-05 02:08:16 +03:00
}
// Build a zap logger from default or custom config
func Build(c string) error {
var zcfg zap.Config
if c == "" {
zcfg = zap.NewProductionConfig()
2021-12-28 13:18:44 +03:00
// to be able to filter with Tile38 levels
zcfg.Level.SetLevel(zap.DebugLevel)
2021-12-28 13:18:44 +03:00
// disable caller because caller is always log.go
zcfg.DisableCaller = true
} else {
err := json.Unmarshal([]byte(c), &zcfg)
if err != nil {
return err
}
// to be able to filter with Tile38 levels
zcfg.Level.SetLevel(zap.DebugLevel)
2021-12-28 13:18:44 +03:00
// disable caller because caller is always log.go
zcfg.DisableCaller = true
}
core, err := zcfg.Build()
if err != nil {
return err
}
defer core.Sync()
zmu.Lock()
zlogger = core.Sugar()
zmu.Unlock()
return nil
}
// Set a zap logger
func Set(sl *zap.SugaredLogger) {
zmu.Lock()
zlogger = sl
zmu.Unlock()
}
// Get a zap logger
func Get() *zap.SugaredLogger {
zmu.Lock()
sl := zlogger
zmu.Unlock()
return sl
2018-10-29 18:16:04 +03:00
}
// Output returns the output writer
2021-02-19 01:34:01 +03:00
func Output() io.Writer {
wmu.Lock()
defer wmu.Unlock()
2021-02-19 01:34:01 +03:00
return wr
}
2017-10-03 18:49:33 +03:00
func log(level int, tag, color string, formatted bool, format string, args ...interface{}) {
if llevel.Load() < int32(level) {
2017-10-03 18:49:33 +03:00
return
2016-03-05 02:08:16 +03:00
}
var msg string
if formatted {
msg = fmt.Sprintf(format, args...)
} else {
msg = fmt.Sprint(args...)
}
if ljson.Load() {
zmu.Lock()
defer zmu.Unlock()
switch tag {
case "ERRO":
zlogger.Error(msg)
case "FATA":
zlogger.Fatal(msg)
case "WARN":
zlogger.Warn(msg)
case "DEBU":
zlogger.Debug(msg)
default:
zlogger.Info(msg)
}
return
}
2017-10-03 18:49:33 +03:00
s := []byte(time.Now().Format("2006/01/02 15:04:05"))
s = append(s, ' ')
if tty.Load() {
2017-10-03 18:49:33 +03:00
s = append(s, color...)
2016-03-05 02:08:16 +03:00
}
2017-10-03 18:49:33 +03:00
s = append(s, '[')
s = append(s, tag...)
s = append(s, ']')
if tty.Load() {
2017-10-03 18:49:33 +03:00
s = append(s, "\x1b[0m"...)
2016-03-05 02:08:16 +03:00
}
2017-10-03 18:49:33 +03:00
s = append(s, ' ')
s = append(s, msg...)
2017-10-03 18:49:33 +03:00
if s[len(s)-1] != '\n' {
s = append(s, '\n')
2016-03-05 02:08:16 +03:00
}
wmu.Lock()
2017-10-03 18:49:33 +03:00
wr.Write(s)
wmu.Unlock()
2016-03-05 02:08:16 +03:00
}
2018-10-23 00:52:48 +03:00
var emptyFormat string
// Infof ...
2017-10-03 18:49:33 +03:00
func Infof(format string, args ...interface{}) {
if llevel.Load() >= 1 {
2018-10-27 17:08:24 +03:00
log(1, "INFO", "\x1b[36m", true, format, args...)
}
2016-03-05 02:08:16 +03:00
}
2018-10-23 00:52:48 +03:00
// Info ...
2017-10-03 18:49:33 +03:00
func Info(args ...interface{}) {
if llevel.Load() >= 1 {
2018-10-27 17:08:24 +03:00
log(1, "INFO", "\x1b[36m", false, emptyFormat, args...)
}
2016-03-05 02:08:16 +03:00
}
2018-10-23 00:52:48 +03:00
// HTTPf ...
2017-10-03 18:49:33 +03:00
func HTTPf(format string, args ...interface{}) {
if llevel.Load() >= 1 {
2018-10-27 17:08:24 +03:00
log(1, "HTTP", "\x1b[1m\x1b[30m", true, format, args...)
}
2016-03-05 02:08:16 +03:00
}
2018-10-23 00:52:48 +03:00
// HTTP ...
2017-10-03 18:49:33 +03:00
func HTTP(args ...interface{}) {
if llevel.Load() >= 1 {
2018-10-27 17:08:24 +03:00
log(1, "HTTP", "\x1b[1m\x1b[30m", false, emptyFormat, args...)
}
2016-03-05 02:08:16 +03:00
}
2018-10-23 00:52:48 +03:00
// Errorf ...
2017-10-03 18:49:33 +03:00
func Errorf(format string, args ...interface{}) {
if llevel.Load() >= 1 {
2018-10-27 17:08:24 +03:00
log(1, "ERRO", "\x1b[1m\x1b[31m", true, format, args...)
}
2016-03-05 02:08:16 +03:00
}
2018-10-23 00:52:48 +03:00
// Error ..
2017-10-03 18:49:33 +03:00
func Error(args ...interface{}) {
if llevel.Load() >= 1 {
2018-10-27 17:08:24 +03:00
log(1, "ERRO", "\x1b[1m\x1b[31m", false, emptyFormat, args...)
}
2016-03-05 02:08:16 +03:00
}
2018-10-23 00:52:48 +03:00
// Warnf ...
2017-10-03 18:49:33 +03:00
func Warnf(format string, args ...interface{}) {
if llevel.Load() >= 1 {
2018-10-27 17:08:24 +03:00
log(2, "WARN", "\x1b[33m", true, format, args...)
}
2016-03-05 02:08:16 +03:00
}
2018-10-23 00:52:48 +03:00
// Warn ...
2017-10-03 18:49:33 +03:00
func Warn(args ...interface{}) {
if llevel.Load() >= 1 {
2018-10-27 17:08:24 +03:00
log(2, "WARN", "\x1b[33m", false, emptyFormat, args...)
}
2016-03-05 02:08:16 +03:00
}
2018-10-23 00:52:48 +03:00
// Debugf ...
2017-10-03 18:49:33 +03:00
func Debugf(format string, args ...interface{}) {
if llevel.Load() >= 3 {
2018-10-27 17:08:24 +03:00
log(3, "DEBU", "\x1b[35m", true, format, args...)
}
2016-03-05 02:08:16 +03:00
}
2018-10-23 00:52:48 +03:00
// Debug ...
2017-10-03 18:49:33 +03:00
func Debug(args ...interface{}) {
if llevel.Load() >= 3 {
2018-10-27 17:08:24 +03:00
log(3, "DEBU", "\x1b[35m", false, emptyFormat, args...)
}
2016-03-05 02:08:16 +03:00
}
2018-10-23 00:52:48 +03:00
// Printf ...
2017-10-03 18:49:33 +03:00
func Printf(format string, args ...interface{}) {
Infof(format, args...)
2016-03-05 02:08:16 +03:00
}
2018-10-23 00:52:48 +03:00
// Print ...
func Print(args ...interface{}) {
2017-10-03 18:49:33 +03:00
Info(args...)
2016-03-05 02:08:16 +03:00
}
2018-10-23 00:52:48 +03:00
// Fatalf ...
2017-10-03 18:49:33 +03:00
func Fatalf(format string, args ...interface{}) {
log(1, "FATA", "\x1b[31m", true, format, args...)
2017-10-03 18:49:33 +03:00
os.Exit(1)
2016-03-05 02:08:16 +03:00
}
2018-10-23 00:52:48 +03:00
// Fatal ...
2017-10-03 18:49:33 +03:00
func Fatal(args ...interface{}) {
2018-10-23 00:52:48 +03:00
log(1, "FATA", "\x1b[31m", false, emptyFormat, args...)
2017-10-03 18:49:33 +03:00
os.Exit(1)
2016-03-05 02:08:16 +03:00
}