Skip to content

Commit

Permalink
Adds default file log option
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed Apr 7, 2015
1 parent 0b7dce4 commit 6c788a4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
15 changes: 8 additions & 7 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
package gin

import (
"log"
"fmt"
"io"
"time"

"github.com/mattn/go-colorable"
)

var (
Expand Down Expand Up @@ -39,9 +38,10 @@ func ErrorLoggerT(typ uint32) HandlerFunc {
}

func Logger() HandlerFunc {
stdlogger := log.New(colorable.NewColorableStdout(), "", 0)
//errlogger := log.New(os.Stderr, "", 0)
return LoggerInFile(DefaultLogFile)
}

func LoggerInFile(out io.Writer) HandlerFunc {
return func(c *Context) {
// Start timer
start := time.Now()
Expand All @@ -58,15 +58,16 @@ func Logger() HandlerFunc {
statusCode := c.Writer.Status()
statusColor := colorForStatus(statusCode)
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"),
statusColor, statusCode, reset,
latency,
clientIP,
methodColor, reset, method,
c.Request.URL.Path,
c.Errors.String(),
comment,
)
}
}
Expand Down
3 changes: 3 additions & 0 deletions mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package gin
import (
"log"
"os"

"github.com/mattn/go-colorable"
)

const GIN_MODE = "GIN_MODE"
Expand All @@ -22,6 +24,7 @@ const (
testCode = iota
)

var DefaultLogFile = colorable.NewColorableStdout()
var ginMode int = debugCode
var modeName string = DebugMode

Expand Down

0 comments on commit 6c788a4

Please sign in to comment.