sendmail/log.go

97 lines
1.9 KiB
Go
Raw Normal View History

package main
import (
"io"
"io/ioutil"
"log"
"os"
"github.com/fatih/color"
)
type DebugLog struct {
logger *log.Logger
}
type InfoLog struct {
logger *log.Logger
}
type WarningLog struct {
logger *log.Logger
}
type ErrorLog struct {
logger *log.Logger
}
var (
Debug = &DebugLog{}
Info = &InfoLog{}
Warning = &WarningLog{}
Error = &ErrorLog{}
)
func (l *DebugLog) F(fmt string, text ...interface{}) {
l.logger.Print(color.MagentaString(fmt, text...))
}
func (l *DebugLog) Ln(text ...interface{}) {
l.logger.Print(color.MagentaString("%s", text...))
}
func (l *InfoLog) F(fmt string, text ...interface{}) {
l.logger.Print(color.WhiteString(fmt, text...))
}
func (l *InfoLog) Ln(text ...interface{}) {
l.logger.Print(color.WhiteString("%s", text...))
}
func (l *WarningLog) F(fmt string, text ...interface{}) {
l.logger.Print(color.YellowString(fmt, text...))
}
func (l *WarningLog) Ln(text ...interface{}) {
l.logger.Print(color.YellowString("%s", text...))
}
func (l *ErrorLog) F(fmt string, text ...interface{}) {
l.logger.Print(color.RedString(fmt, text...))
}
func (l *ErrorLog) Ln(text ...interface{}) {
l.logger.Print(color.RedString("%s", text...))
}
func (l *DebugLog) init(w io.Writer, prefix string, flag int) {
l.logger = &log.Logger{}
l.logger = log.New(w, prefix, flag)
}
func (l *InfoLog) init(w io.Writer, prefix string, flag int) {
l.logger = &log.Logger{}
l.logger = log.New(w, prefix, flag)
}
func (l *WarningLog) init(w io.Writer, prefix string, flag int) {
l.logger = &log.Logger{}
l.logger = log.New(w, prefix, flag)
}
func (l *ErrorLog) init(w io.Writer, prefix string, flag int) {
l.logger = &log.Logger{}
l.logger = log.New(w, prefix, flag)
}
func LogInit(debug bool) {
var debugOut io.Writer
if debug {
debugOut = os.Stdout
} else {
debugOut = ioutil.Discard
}
Debug.init(debugOut, "", 0)
Info.init(os.Stdout, "", 0)
Warning.init(os.Stdout, "", 0)
Error.init(os.Stderr, "", 0)
}