123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- package logger
- import (
- "fmt"
- "os"
- terminal "github.com/mudler/go-isterminal"
- "github.com/ipfs/go-log"
- "github.com/pterm/pterm"
- )
- var _ log.StandardLogger = &Logger{}
- type Logger struct {
- level log.LogLevel
- }
- func New(lvl log.LogLevel) *Logger {
- if !terminal.IsTerminal(os.Stdout) {
- pterm.DisableColor()
- }
- if lvl == log.LevelDebug {
- pterm.EnableDebugMessages()
- }
- return &Logger{level: lvl}
- }
- func joinMsg(args ...interface{}) (message string) {
- for _, m := range args {
- message += " " + fmt.Sprintf("%v", m)
- }
- return
- }
- func (l Logger) enabled(lvl log.LogLevel) bool {
- return lvl >= l.level
- }
- func (l Logger) Debug(args ...interface{}) {
- if l.enabled(log.LevelDebug) {
- pterm.Debug.Println(joinMsg(args...))
- }
- }
- func (l Logger) Debugf(f string, args ...interface{}) {
- if l.enabled(log.LevelDebug) {
- pterm.Debug.Printfln(f, args...)
- }
- }
- func (l Logger) Error(args ...interface{}) {
- if l.enabled(log.LevelError) {
- pterm.Error.Println(pterm.LightRed(joinMsg(args...)))
- }
- }
- func (l Logger) Errorf(f string, args ...interface{}) {
- if l.enabled(log.LevelError) {
- pterm.Error.Printfln(pterm.LightRed(f), args...)
- }
- }
- func (l Logger) Fatal(args ...interface{}) {
- if l.enabled(log.LevelFatal) {
- pterm.Fatal.Println(pterm.Red(joinMsg(args...)))
- }
- }
- func (l Logger) Fatalf(f string, args ...interface{}) {
- if l.enabled(log.LevelFatal) {
- pterm.Fatal.Printfln(pterm.Red(f), args...)
- }
- }
- func (l Logger) Info(args ...interface{}) {
- if l.enabled(log.LevelInfo) {
- pterm.Info.Println(pterm.LightBlue(joinMsg(args...)))
- }
- }
- func (l Logger) Infof(f string, args ...interface{}) {
- if l.enabled(log.LevelInfo) {
- pterm.Info.Printfln(pterm.LightBlue(f), args...)
- }
- }
- func (l Logger) Panic(args ...interface{}) {
- l.Fatal(args...)
- }
- func (l Logger) Panicf(f string, args ...interface{}) {
- l.Fatalf(f, args...)
- }
- func (l Logger) Warn(args ...interface{}) {
- if l.enabled(log.LevelWarn) {
- pterm.Warning.Println(pterm.LightYellow(joinMsg(args...)))
- }
- }
- func (l Logger) Warnf(f string, args ...interface{}) {
- if l.enabled(log.LevelWarn) {
- pterm.Warning.Printfln(pterm.LightYellow(f), args...)
- }
- }
- func (l Logger) Warning(args ...interface{}) {
- l.Warn(args...)
- }
- func (l Logger) Warningf(f string, args ...interface{}) {
- l.Warnf(f, args...)
- }
|