Adds default file log option

This commit is contained in:
Manu Mtz-Almeida 2015-04-07 18:37:17 +02:00
parent 0b7dce4bc9
commit 6c788a4300
2 changed files with 11 additions and 7 deletions

View File

@ -5,10 +5,9 @@
package gin package gin
import ( import (
"log" "fmt"
"io"
"time" "time"
"github.com/mattn/go-colorable"
) )
var ( var (
@ -39,9 +38,10 @@ func ErrorLoggerT(typ uint32) HandlerFunc {
} }
func Logger() HandlerFunc { func Logger() HandlerFunc {
stdlogger := log.New(colorable.NewColorableStdout(), "", 0) return LoggerInFile(DefaultLogFile)
//errlogger := log.New(os.Stderr, "", 0) }
func LoggerInFile(out io.Writer) HandlerFunc {
return func(c *Context) { return func(c *Context) {
// Start timer // Start timer
start := time.Now() start := time.Now()
@ -58,15 +58,16 @@ func Logger() HandlerFunc {
statusCode := c.Writer.Status() statusCode := c.Writer.Status()
statusColor := colorForStatus(statusCode) statusColor := colorForStatus(statusCode)
methodColor := colorForMethod(method) methodColor := colorForMethod(method)
comment := c.Errors.String()
stdlogger.Printf("[GIN] %v |%s %3d %s| %12v | %s |%s %s %-7s %s\n%s", fmt.Fprintf(out, "[GIN] %v |%s %3d %s| %12v | %s |%s %s %-7s %s\n%s",
end.Format("2006/01/02 - 15:04:05"), end.Format("2006/01/02 - 15:04:05"),
statusColor, statusCode, reset, statusColor, statusCode, reset,
latency, latency,
clientIP, clientIP,
methodColor, reset, method, methodColor, reset, method,
c.Request.URL.Path, c.Request.URL.Path,
c.Errors.String(), comment,
) )
} }
} }

View File

@ -7,6 +7,8 @@ package gin
import ( import (
"log" "log"
"os" "os"
"github.com/mattn/go-colorable"
) )
const GIN_MODE = "GIN_MODE" const GIN_MODE = "GIN_MODE"
@ -22,6 +24,7 @@ const (
testCode = iota testCode = iota
) )
var DefaultLogFile = colorable.NewColorableStdout()
var ginMode int = debugCode var ginMode int = debugCode
var modeName string = DebugMode var modeName string = DebugMode