mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 10:27:06 +00:00
Update to v3.0.0-beta.4 - Logger changes book, examples updated
This commit is contained in:
154
logger/logger.go
154
logger/logger.go
@@ -1,111 +1,191 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/kataras/iris/config"
|
||||
"github.com/mattn/go-colorable"
|
||||
)
|
||||
|
||||
var (
|
||||
// Prefix is the prefix for the logger, default is [IRIS]
|
||||
Prefix = "[IRIS] "
|
||||
// bannersRan keeps track of the logger's print banner count
|
||||
bannersRan = 0
|
||||
)
|
||||
|
||||
// Logger is just a log.Logger
|
||||
// Logger the logger
|
||||
type Logger struct {
|
||||
Logger *log.Logger
|
||||
enabled bool
|
||||
config config.Logger
|
||||
underline *color.Color
|
||||
}
|
||||
|
||||
// New creates a new Logger. The out variable sets the
|
||||
// destination to which log data will be written.
|
||||
// The prefix appears at the beginning of each generated log line.
|
||||
// The flag argument defines the logging properties.
|
||||
func New(cfg ...config.Logger) *Logger {
|
||||
c := config.DefaultLogger().Merge(cfg)
|
||||
return &Logger{Logger: log.New(c.Out, Prefix+c.Prefix, c.Flag), enabled: true}
|
||||
// attr takes a color integer and converts it to color.Attribute
|
||||
func attr(sgr int) color.Attribute {
|
||||
return color.Attribute(sgr)
|
||||
}
|
||||
|
||||
// New creates a new Logger from config.Logger configuration
|
||||
func New(c config.Logger) *Logger {
|
||||
color.Output = colorable.NewColorable(c.Out)
|
||||
|
||||
l := &Logger{c, color.New(attr(c.ColorBgDefault), attr(c.ColorFgDefault), color.Bold)}
|
||||
return l
|
||||
}
|
||||
|
||||
// PrintBanner prints a text (banner) with BannerFgColor, BannerBgColor and a success message at the end
|
||||
// It doesn't cares if the logger is disabled or not, it will print this
|
||||
func (l *Logger) PrintBanner(banner string, sucessMessage string) {
|
||||
c := color.New(attr(l.config.ColorBgDefault), attr(l.config.ColorFgBanner), color.Bold)
|
||||
c.Println(banner)
|
||||
bannersRan++
|
||||
|
||||
if sucessMessage != "" {
|
||||
c.Add(attr(l.config.ColorBgSuccess), attr(l.config.ColorFgSuccess), color.Bold)
|
||||
|
||||
if bannersRan > 1 {
|
||||
c.Printf("Server[%#v]\n", bannersRan)
|
||||
|
||||
}
|
||||
c.Println(sucessMessage)
|
||||
}
|
||||
|
||||
c.DisableColor()
|
||||
c = nil
|
||||
}
|
||||
|
||||
// ResetColors sets the colors to the default
|
||||
// this func is called every time a success, info, warning, or danger message is printed
|
||||
func (l *Logger) ResetColors() {
|
||||
l.underline.Add(attr(l.config.ColorBgDefault), attr(l.config.ColorFgBanner), color.Bold)
|
||||
}
|
||||
|
||||
// SetEnable true enables, false disables the Logger
|
||||
func (l *Logger) SetEnable(enable bool) {
|
||||
l.enabled = enable
|
||||
l.config.Disabled = !enable
|
||||
}
|
||||
|
||||
// IsEnabled returns true if Logger is enabled, otherwise false
|
||||
func (l *Logger) IsEnabled() bool {
|
||||
return l.enabled
|
||||
return !l.config.Disabled
|
||||
}
|
||||
|
||||
// Print calls l.Output to print to the logger.
|
||||
// Arguments are handled in the manner of fmt.Print.
|
||||
func (l *Logger) Print(v ...interface{}) {
|
||||
if l.enabled {
|
||||
l.Logger.Print(v...)
|
||||
if !l.config.Disabled {
|
||||
l.underline.Print(v...)
|
||||
}
|
||||
}
|
||||
|
||||
// Printf calls l.Output to print to the logger.
|
||||
// Arguments are handled in the manner of fmt.Printf.
|
||||
func (l *Logger) Printf(format string, a ...interface{}) {
|
||||
if l.enabled {
|
||||
l.Logger.Printf(format, a...)
|
||||
if !l.config.Disabled {
|
||||
l.underline.Printf(format, a...)
|
||||
}
|
||||
}
|
||||
|
||||
// Println calls l.Output to print to the logger.
|
||||
// Arguments are handled in the manner of fmt.Println.
|
||||
func (l *Logger) Println(a ...interface{}) {
|
||||
if l.enabled {
|
||||
l.Logger.Println(a...)
|
||||
if !l.config.Disabled {
|
||||
l.underline.Println(a...)
|
||||
}
|
||||
}
|
||||
|
||||
// Fatal is equivalent to l.Print() followed by a call to os.Exit(1).
|
||||
func (l *Logger) Fatal(a ...interface{}) {
|
||||
if l.enabled {
|
||||
l.Logger.Fatal(a...)
|
||||
} else {
|
||||
os.Exit(1) //we have to exit at any case because this is the Fatal
|
||||
if !l.config.Disabled {
|
||||
l.underline.Print(a...)
|
||||
|
||||
}
|
||||
os.Exit(1)
|
||||
|
||||
}
|
||||
|
||||
// Fatalf is equivalent to l.Printf() followed by a call to os.Exit(1).
|
||||
func (l *Logger) Fatalf(format string, a ...interface{}) {
|
||||
if l.enabled {
|
||||
l.Logger.Fatalf(format, a...)
|
||||
} else {
|
||||
os.Exit(1)
|
||||
if !l.config.Disabled {
|
||||
l.underline.Printf(format, a...)
|
||||
}
|
||||
|
||||
os.Exit(1)
|
||||
|
||||
}
|
||||
|
||||
// Fatalln is equivalent to l.Println() followed by a call to os.Exit(1).
|
||||
func (l *Logger) Fatalln(a ...interface{}) {
|
||||
if l.enabled {
|
||||
l.Logger.Fatalln(a...)
|
||||
} else {
|
||||
os.Exit(1)
|
||||
if !l.config.Disabled {
|
||||
l.underline.Println(a...)
|
||||
}
|
||||
|
||||
os.Exit(1)
|
||||
|
||||
}
|
||||
|
||||
// Panic is equivalent to l.Print() followed by a call to panic().
|
||||
func (l *Logger) Panic(a ...interface{}) {
|
||||
if l.enabled {
|
||||
l.Logger.Panic(a...)
|
||||
if !l.config.Disabled {
|
||||
l.underline.Print(a...)
|
||||
}
|
||||
|
||||
panic("")
|
||||
}
|
||||
|
||||
// Panicf is equivalent to l.Printf() followed by a call to panic().
|
||||
func (l *Logger) Panicf(format string, a ...interface{}) {
|
||||
if l.enabled {
|
||||
l.Logger.Panicf(format, a...)
|
||||
if !l.config.Disabled {
|
||||
l.underline.Printf(format, a...)
|
||||
}
|
||||
panic("")
|
||||
}
|
||||
|
||||
// Panicln is equivalent to l.Println() followed by a call to panic().
|
||||
func (l *Logger) Panicln(a ...interface{}) {
|
||||
if l.enabled {
|
||||
l.Logger.Panicln(a...)
|
||||
if !l.config.Disabled {
|
||||
l.underline.Println(a...)
|
||||
}
|
||||
panic("")
|
||||
}
|
||||
|
||||
// Sucessf calls l.Output to print to the logger with the Success colors.
|
||||
// Arguments are handled in the manner of fmt.Printf.
|
||||
func (l *Logger) Sucessf(format string, a ...interface{}) {
|
||||
if !l.config.Disabled {
|
||||
l.underline.Add(attr(l.config.ColorBgSuccess), attr(l.config.ColorFgSuccess))
|
||||
l.underline.Printf(format, a...)
|
||||
l.ResetColors()
|
||||
}
|
||||
}
|
||||
|
||||
// Infof calls l.Output to print to the logger with the Info colors.
|
||||
// Arguments are handled in the manner of fmt.Printf.
|
||||
func (l *Logger) Infof(format string, a ...interface{}) {
|
||||
if !l.config.Disabled {
|
||||
l.underline.Add(attr(l.config.ColorBgInfo), attr(l.config.ColorFgInfo))
|
||||
l.underline.Printf(format, a...)
|
||||
l.ResetColors()
|
||||
}
|
||||
}
|
||||
|
||||
// Warningf calls l.Output to print to the logger with the Warning colors.
|
||||
// Arguments are handled in the manner of fmt.Printf.
|
||||
func (l *Logger) Warningf(format string, a ...interface{}) {
|
||||
if !l.config.Disabled {
|
||||
l.underline.Add(attr(l.config.ColorBgWarning), attr(l.config.ColorFgWarning))
|
||||
l.underline.Printf(format, a...)
|
||||
l.ResetColors()
|
||||
}
|
||||
}
|
||||
|
||||
// Dangerf calls l.Output to print to the logger with the Danger colors.
|
||||
// Arguments are handled in the manner of fmt.Printf.
|
||||
func (l *Logger) Dangerf(format string, a ...interface{}) {
|
||||
if !l.config.Disabled {
|
||||
l.underline.Add(attr(l.config.ColorBgDanger), attr(l.config.ColorFgDanger))
|
||||
l.underline.Printf(format, a...)
|
||||
l.ResetColors()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user