tile38/pkg/log/log.go

104 lines
2.3 KiB
Go
Raw Normal View History

2016-03-05 02:08:16 +03:00
package log
import (
"fmt"
"io"
"os"
"sync"
"time"
2017-10-03 18:49:33 +03:00
"golang.org/x/crypto/ssh/terminal"
2016-03-05 02:08:16 +03:00
)
2017-10-03 18:49:33 +03:00
var mu sync.Mutex
var wr io.Writer
var tty bool
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
var Level int = 1
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 = ok && terminal.IsTerminal(int(f.Fd()))
wr = w
2016-03-05 02:08:16 +03:00
}
2017-10-03 18:49:33 +03:00
func log(level int, tag, color string, formatted bool, format string, args ...interface{}) {
if Level < level {
return
2016-03-05 02:08:16 +03:00
}
2017-10-03 18:49:33 +03:00
s := []byte(time.Now().Format("2006/01/02 15:04:05"))
s = append(s, ' ')
if tty {
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 {
s = append(s, "\x1b[0m"...)
2016-03-05 02:08:16 +03:00
}
2017-10-03 18:49:33 +03:00
s = append(s, ' ')
if formatted {
s = append(s, fmt.Sprintf(format, args...)...)
} else {
s = append(s, fmt.Sprint(args...)...)
2016-03-05 02:08:16 +03:00
}
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
}
2017-10-03 18:49:33 +03:00
mu.Lock()
wr.Write(s)
mu.Unlock()
2016-03-05 02:08:16 +03:00
}
2017-10-03 18:49:33 +03:00
func Infof(format string, args ...interface{}) {
log(1, "INFO", "\x1b[36m", true, format, args...)
2016-03-05 02:08:16 +03:00
}
2017-10-03 18:49:33 +03:00
func Info(args ...interface{}) {
log(1, "INFO", "\x1b[36m", false, "", args...)
2016-03-05 02:08:16 +03:00
}
2017-10-03 18:49:33 +03:00
func HTTPf(format string, args ...interface{}) {
log(1, "HTTP", "\x1b[1m\x1b[30m", true, format, args...)
2016-03-05 02:08:16 +03:00
}
2017-10-03 18:49:33 +03:00
func HTTP(args ...interface{}) {
log(1, "HTTP", "\x1b[1m\x1b[30m", false, "", args...)
2016-03-05 02:08:16 +03:00
}
2017-10-03 18:49:33 +03:00
func Errorf(format string, args ...interface{}) {
log(1, "ERRO", "\x1b[1m\x1b[31m", true, format, args...)
2016-03-05 02:08:16 +03:00
}
2017-10-03 18:49:33 +03:00
func Error(args ...interface{}) {
log(1, "ERRO", "\x1b[1m\x1b[31m", false, "", args...)
2016-03-05 02:08:16 +03:00
}
2017-10-03 18:49:33 +03:00
func Warnf(format string, args ...interface{}) {
log(2, "WARN", "\x1b[33m", true, format, args...)
2016-03-05 02:08:16 +03:00
}
2017-10-03 18:49:33 +03:00
func Warn(args ...interface{}) {
log(2, "WARN", "\x1b[33m", false, "", args...)
2016-03-05 02:08:16 +03:00
}
2017-10-03 18:49:33 +03:00
func Debugf(format string, args ...interface{}) {
log(3, "DEBU", "\x1b[35m", true, format, args...)
2016-03-05 02:08:16 +03:00
}
2017-10-03 18:49:33 +03:00
func Debug(args ...interface{}) {
log(3, "DEBU", "\x1b[35m", false, "", args...)
2016-03-05 02:08:16 +03:00
}
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
}
2017-10-03 18:49:33 +03:00
func Print(format string, args ...interface{}) {
Info(args...)
2016-03-05 02:08:16 +03:00
}
2017-10-03 18:49:33 +03:00
func Fatalf(format string, args ...interface{}) {
log(1, "FATA", "x1b[31m", true, format, args...)
os.Exit(1)
2016-03-05 02:08:16 +03:00
}
2017-10-03 18:49:33 +03:00
func Fatal(args ...interface{}) {
log(1, "FATA", "\x1b[31m", false, "", args...)
os.Exit(1)
2016-03-05 02:08:16 +03:00
}