mirror of
https://github.com/kataras/iris.git
synced 2025-12-19 02:47:04 +00:00
Final touches for Logger
This commit is contained in:
@@ -9,7 +9,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
const DefaultLoggerPrefix = "[IRIS] "
|
const DefaultLoggerPrefix = ""
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// TimeFormat default time format for any kind of datetime parsing
|
// TimeFormat default time format for any kind of datetime parsing
|
||||||
@@ -23,6 +23,7 @@ type (
|
|||||||
// Default is os.Stdout
|
// Default is os.Stdout
|
||||||
Out *os.File
|
Out *os.File
|
||||||
// Prefix the prefix for each message
|
// Prefix the prefix for each message
|
||||||
|
// Default is ""
|
||||||
Prefix string
|
Prefix string
|
||||||
// Disabled default is false
|
// Disabled default is false
|
||||||
Disabled bool
|
Disabled bool
|
||||||
@@ -39,10 +40,12 @@ type (
|
|||||||
ColorFgWarning int
|
ColorFgWarning int
|
||||||
// ColorFgDanger the foreground color for error messages
|
// ColorFgDanger the foreground color for error messages
|
||||||
ColorFgDanger int
|
ColorFgDanger int
|
||||||
|
// OtherFgColor the foreground color for the rest of the message types
|
||||||
|
ColorFgOther int
|
||||||
|
|
||||||
// background colors single SGR Code
|
// background colors single SGR Code
|
||||||
|
|
||||||
// ColorBgDefault the background color for the normal message bodies
|
// ColorBgDefault the background color for the normal messages
|
||||||
ColorBgDefault int
|
ColorBgDefault int
|
||||||
// ColorBgInfo the background color for info messages
|
// ColorBgInfo the background color for info messages
|
||||||
ColorBgInfo int
|
ColorBgInfo int
|
||||||
@@ -52,8 +55,11 @@ type (
|
|||||||
ColorBgWarning int
|
ColorBgWarning int
|
||||||
// ColorBgDanger the background color for error messages
|
// ColorBgDanger the background color for error messages
|
||||||
ColorBgDanger int
|
ColorBgDanger int
|
||||||
|
// OtherFgColor the background color for the rest of the message types
|
||||||
|
ColorBgOther int
|
||||||
|
|
||||||
// banners are the force printed/written messages, doesn't care about Disabled field
|
// banners are the force printed/written messages, doesn't care about Disabled field
|
||||||
|
|
||||||
// ColorFgBanner the foreground color for the banner
|
// ColorFgBanner the foreground color for the banner
|
||||||
ColorFgBanner int
|
ColorFgBanner int
|
||||||
}
|
}
|
||||||
@@ -63,21 +69,23 @@ type (
|
|||||||
func DefaultLogger() Logger {
|
func DefaultLogger() Logger {
|
||||||
return Logger{
|
return Logger{
|
||||||
Out: os.Stdout,
|
Out: os.Stdout,
|
||||||
Prefix: DefaultLoggerPrefix,
|
Prefix: "",
|
||||||
Disabled: false,
|
Disabled: false,
|
||||||
// foreground colors
|
// foreground colors
|
||||||
ColorFgDefault: int(color.FgHiWhite),
|
ColorFgDefault: int(color.FgHiWhite),
|
||||||
ColorFgInfo: int(color.FgCyan),
|
ColorFgInfo: int(color.FgHiCyan),
|
||||||
ColorFgSuccess: int(color.FgHiGreen),
|
ColorFgSuccess: int(color.FgHiGreen),
|
||||||
ColorFgWarning: int(color.FgHiMagenta),
|
ColorFgWarning: int(color.FgHiMagenta),
|
||||||
ColorFgDanger: int(color.FgHiRed),
|
ColorFgDanger: int(color.FgHiRed),
|
||||||
|
ColorFgOther: int(color.FgHiYellow),
|
||||||
// background colors
|
// background colors
|
||||||
ColorBgDefault: int(color.BgHiBlack),
|
ColorBgDefault: int(color.BgHiBlack),
|
||||||
ColorBgInfo: int(color.BgHiBlack),
|
ColorBgInfo: int(color.BgHiBlack),
|
||||||
ColorBgSuccess: int(color.BgHiBlack),
|
ColorBgSuccess: int(color.BgHiBlack),
|
||||||
ColorBgWarning: int(color.BgHiBlack),
|
ColorBgWarning: int(color.BgHiBlack),
|
||||||
ColorBgDanger: int(color.BgHiWhite),
|
ColorBgDanger: int(color.BgHiWhite),
|
||||||
// banner colors
|
ColorBgOther: int(color.BgHiBlack),
|
||||||
|
// banner color
|
||||||
ColorFgBanner: int(color.FgHiBlue),
|
ColorFgBanner: int(color.FgHiBlue),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
125
logger/logger.go
125
logger/logger.go
@@ -34,6 +34,22 @@ func New(c config.Logger) *Logger {
|
|||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetEnable true enables, false disables the Logger
|
||||||
|
func (l *Logger) SetEnable(enable bool) {
|
||||||
|
l.config.Disabled = !enable
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsEnabled returns true if Logger is enabled, otherwise false
|
||||||
|
func (l *Logger) IsEnabled() bool {
|
||||||
|
return !l.config.Disabled
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
|
||||||
// PrintBanner prints a text (banner) with BannerFgColor, BannerBgColor and a success message at the end
|
// 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
|
// It doesn't cares if the logger is disabled or not, it will print this
|
||||||
func (l *Logger) PrintBanner(banner string, sucessMessage string) {
|
func (l *Logger) PrintBanner(banner string, sucessMessage string) {
|
||||||
@@ -55,98 +71,51 @@ func (l *Logger) PrintBanner(banner string, sucessMessage string) {
|
|||||||
c = nil
|
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.config.Disabled = !enable
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsEnabled returns true if Logger is enabled, otherwise false
|
|
||||||
func (l *Logger) IsEnabled() bool {
|
|
||||||
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.config.Disabled {
|
|
||||||
l.underline.Print(v...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Printf calls l.Output to print to the logger.
|
// Printf calls l.Output to print to the logger.
|
||||||
// Arguments are handled in the manner of fmt.Printf.
|
// Arguments are handled in the manner of fmt.Printf.
|
||||||
func (l *Logger) Printf(format string, a ...interface{}) {
|
func (l *Logger) Printf(format string, a ...interface{}) {
|
||||||
if !l.config.Disabled {
|
if !l.config.Disabled {
|
||||||
l.underline.Printf(format, a...)
|
l.underline.Printf(l.config.Prefix+format, a...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print calls l.Output to print to the logger.
|
||||||
|
// Arguments are handled in the manner of fmt.Print.
|
||||||
|
func (l *Logger) Print(a interface{}) {
|
||||||
|
if !l.config.Disabled {
|
||||||
|
l.Printf("%#v", a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Println calls l.Output to print to the logger.
|
// Println calls l.Output to print to the logger.
|
||||||
// Arguments are handled in the manner of fmt.Println.
|
// Arguments are handled in the manner of fmt.Println.
|
||||||
func (l *Logger) Println(a ...interface{}) {
|
func (l *Logger) Println(a interface{}) {
|
||||||
if !l.config.Disabled {
|
if !l.config.Disabled {
|
||||||
l.underline.Println(a...)
|
l.Printf("%#v\n", a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fatal is equivalent to l.Print() followed by a call to os.Exit(1).
|
// Fatal is equivalent to l.Dangerf("%#v",interface{}) followed by a call to panic().
|
||||||
func (l *Logger) Fatal(a ...interface{}) {
|
func (l *Logger) Fatal(a interface{}) {
|
||||||
if !l.config.Disabled {
|
l.Warningf("%#v", a)
|
||||||
l.underline.Print(a...)
|
panic("")
|
||||||
|
|
||||||
}
|
|
||||||
os.Exit(1)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fatalf is equivalent to l.Printf() followed by a call to os.Exit(1).
|
// Fatalf is equivalent to l.Warningf() followed by a call to os.Exit(1).
|
||||||
func (l *Logger) Fatalf(format string, a ...interface{}) {
|
func (l *Logger) Fatalf(format string, a ...interface{}) {
|
||||||
if !l.config.Disabled {
|
l.Warningf(format, a...)
|
||||||
l.underline.Printf(format, a...)
|
|
||||||
}
|
|
||||||
|
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fatalln is equivalent to l.Println() followed by a call to os.Exit(1).
|
// Panic is equivalent to l.Dangerf("%#v",interface{}) followed by a call to panic().
|
||||||
func (l *Logger) Fatalln(a ...interface{}) {
|
func (l *Logger) Panic(a interface{}) {
|
||||||
if !l.config.Disabled {
|
l.Dangerf("%#v", a)
|
||||||
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.config.Disabled {
|
|
||||||
l.underline.Print(a...)
|
|
||||||
}
|
|
||||||
|
|
||||||
panic("")
|
panic("")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Panicf is equivalent to l.Printf() followed by a call to panic().
|
// Panicf is equivalent to l.Dangerf() followed by a call to panic().
|
||||||
func (l *Logger) Panicf(format string, a ...interface{}) {
|
func (l *Logger) Panicf(format string, a ...interface{}) {
|
||||||
if !l.config.Disabled {
|
l.Dangerf(format, a...)
|
||||||
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.config.Disabled {
|
|
||||||
l.underline.Println(a...)
|
|
||||||
}
|
|
||||||
panic("")
|
panic("")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -155,7 +124,7 @@ func (l *Logger) Panicln(a ...interface{}) {
|
|||||||
func (l *Logger) Sucessf(format string, a ...interface{}) {
|
func (l *Logger) Sucessf(format string, a ...interface{}) {
|
||||||
if !l.config.Disabled {
|
if !l.config.Disabled {
|
||||||
l.underline.Add(attr(l.config.ColorBgSuccess), attr(l.config.ColorFgSuccess))
|
l.underline.Add(attr(l.config.ColorBgSuccess), attr(l.config.ColorFgSuccess))
|
||||||
l.underline.Printf(format, a...)
|
l.Printf(format, a...)
|
||||||
l.ResetColors()
|
l.ResetColors()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -165,7 +134,7 @@ func (l *Logger) Sucessf(format string, a ...interface{}) {
|
|||||||
func (l *Logger) Infof(format string, a ...interface{}) {
|
func (l *Logger) Infof(format string, a ...interface{}) {
|
||||||
if !l.config.Disabled {
|
if !l.config.Disabled {
|
||||||
l.underline.Add(attr(l.config.ColorBgInfo), attr(l.config.ColorFgInfo))
|
l.underline.Add(attr(l.config.ColorBgInfo), attr(l.config.ColorFgInfo))
|
||||||
l.underline.Printf(format, a...)
|
l.Printf(format, a...)
|
||||||
l.ResetColors()
|
l.ResetColors()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -175,7 +144,7 @@ func (l *Logger) Infof(format string, a ...interface{}) {
|
|||||||
func (l *Logger) Warningf(format string, a ...interface{}) {
|
func (l *Logger) Warningf(format string, a ...interface{}) {
|
||||||
if !l.config.Disabled {
|
if !l.config.Disabled {
|
||||||
l.underline.Add(attr(l.config.ColorBgWarning), attr(l.config.ColorFgWarning))
|
l.underline.Add(attr(l.config.ColorBgWarning), attr(l.config.ColorFgWarning))
|
||||||
l.underline.Printf(format, a...)
|
l.Printf(format, a...)
|
||||||
l.ResetColors()
|
l.ResetColors()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -185,7 +154,17 @@ func (l *Logger) Warningf(format string, a ...interface{}) {
|
|||||||
func (l *Logger) Dangerf(format string, a ...interface{}) {
|
func (l *Logger) Dangerf(format string, a ...interface{}) {
|
||||||
if !l.config.Disabled {
|
if !l.config.Disabled {
|
||||||
l.underline.Add(attr(l.config.ColorBgDanger), attr(l.config.ColorFgDanger))
|
l.underline.Add(attr(l.config.ColorBgDanger), attr(l.config.ColorFgDanger))
|
||||||
l.underline.Printf(format, a...)
|
l.Printf(format, a...)
|
||||||
|
l.ResetColors()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherf calls l.Output to print to the logger with the Other colors.
|
||||||
|
// Arguments are handled in the manner of fmt.Printf.
|
||||||
|
func (l *Logger) Otherf(format string, a ...interface{}) {
|
||||||
|
if !l.config.Disabled {
|
||||||
|
l.underline.Add(attr(l.config.ColorBgOther), attr(l.config.ColorFgOther))
|
||||||
|
l.Printf(format, a...)
|
||||||
l.ResetColors()
|
l.ResetColors()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -75,9 +75,9 @@ func (l *loggerMiddleware) Serve(ctx *iris.Context) {
|
|||||||
|
|
||||||
//finally print the logs
|
//finally print the logs
|
||||||
if l.options.Latency {
|
if l.options.Latency {
|
||||||
l.Infof("%s %v %4v %s %s %s \n", date, status, latency, ip, method, path)
|
l.Otherf("%s %v %4v %s %s %s \n", date, status, latency, ip, method, path)
|
||||||
} else {
|
} else {
|
||||||
l.Infof("%s %v %s %s %s \n", date, status, ip, method, path)
|
l.Otherf("%s %v %s %s %s \n", date, status, ip, method, path)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user