5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-30 00:21:36 +00:00
matterbridge/vendor/go.mau.fi/whatsmeow/util/log/log.go
2022-03-20 14:57:48 +01:00

84 lines
2.7 KiB
Go

// Copyright (c) 2021 Tulir Asokan
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// Package waLog contains a simple logger interface used by the other whatsmeow packages.
package waLog
import (
"fmt"
"strings"
"time"
)
// Logger is a simple logger interface that can have subloggers for specific areas.
type Logger interface {
Warnf(msg string, args ...interface{})
Errorf(msg string, args ...interface{})
Infof(msg string, args ...interface{})
Debugf(msg string, args ...interface{})
Sub(module string) Logger
}
type noopLogger struct{}
func (n *noopLogger) Errorf(_ string, _ ...interface{}) {}
func (n *noopLogger) Warnf(_ string, _ ...interface{}) {}
func (n *noopLogger) Infof(_ string, _ ...interface{}) {}
func (n *noopLogger) Debugf(_ string, _ ...interface{}) {}
func (n *noopLogger) Sub(_ string) Logger { return n }
// Noop is a no-op Logger implementation that silently drops everything.
var Noop Logger = &noopLogger{}
type stdoutLogger struct {
mod string
color bool
min int
}
var colors = map[string]string{
"INFO": "\033[36m",
"WARN": "\033[33m",
"ERROR": "\033[31m",
}
var levelToInt = map[string]int{
"": -1,
"DEBUG": 0,
"INFO": 1,
"WARN": 2,
"ERROR": 3,
}
func (s *stdoutLogger) outputf(level, msg string, args ...interface{}) {
if levelToInt[level] < s.min {
return
}
var colorStart, colorReset string
if s.color {
colorStart = colors[level]
colorReset = "\033[0m"
}
fmt.Printf("%s%s [%s %s] %s%s\n", time.Now().Format("15:04:05.000"), colorStart, s.mod, level, fmt.Sprintf(msg, args...), colorReset)
}
func (s *stdoutLogger) Errorf(msg string, args ...interface{}) { s.outputf("ERROR", msg, args...) }
func (s *stdoutLogger) Warnf(msg string, args ...interface{}) { s.outputf("WARN", msg, args...) }
func (s *stdoutLogger) Infof(msg string, args ...interface{}) { s.outputf("INFO", msg, args...) }
func (s *stdoutLogger) Debugf(msg string, args ...interface{}) { s.outputf("DEBUG", msg, args...) }
func (s *stdoutLogger) Sub(mod string) Logger {
return &stdoutLogger{mod: fmt.Sprintf("%s/%s", s.mod, mod), color: s.color, min: s.min}
}
// Stdout is a simple Logger implementation that outputs to stdout. The module name given is included in log lines.
//
// minLevel specifies the minimum log level to output. An empty string will output all logs.
//
// If color is true, then info, warn and error logs will be colored cyan, yellow and red respectively using ANSI color escape codes.
func Stdout(module string, minLevel string, color bool) Logger {
return &stdoutLogger{mod: module, color: color, min: levelToInt[strings.ToUpper(minLevel)]}
}