mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-24 02:32:17 +08:00
fix colored logger on windows
This commit is contained in:
parent
cdf26f994b
commit
aa3cba9bfd
12
logger.go
12
logger.go
@ -8,8 +8,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/mattn/go-colorable"
|
||||||
"github.com/mattn/go-isatty"
|
"github.com/mattn/go-isatty"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -56,11 +58,15 @@ func Logger() HandlerFunc {
|
|||||||
// Example: os.Stdout, a file opened in write mode, a socket...
|
// Example: os.Stdout, a file opened in write mode, a socket...
|
||||||
func LoggerWithWriter(out io.Writer, notlogged ...string) HandlerFunc {
|
func LoggerWithWriter(out io.Writer, notlogged ...string) HandlerFunc {
|
||||||
isTerm := true
|
isTerm := true
|
||||||
|
cout := out
|
||||||
|
|
||||||
if w, ok := out.(*os.File); !ok ||
|
if w, ok := out.(*os.File); !ok ||
|
||||||
(os.Getenv("TERM") == "dumb" || (!isatty.IsTerminal(w.Fd()) && !isatty.IsCygwinTerminal(w.Fd()))) ||
|
disableColor ||
|
||||||
disableColor {
|
os.Getenv("TERM") == "dumb" ||
|
||||||
|
!isatty.IsTerminal(w.Fd()) && !isatty.IsCygwinTerminal(w.Fd()) {
|
||||||
isTerm = false
|
isTerm = false
|
||||||
|
} else if runtime.GOOS == "windows" {
|
||||||
|
cout = colorable.NewColorable(w)
|
||||||
}
|
}
|
||||||
|
|
||||||
var skip map[string]struct{}
|
var skip map[string]struct{}
|
||||||
@ -103,7 +109,7 @@ func LoggerWithWriter(out io.Writer, notlogged ...string) HandlerFunc {
|
|||||||
path = path + "?" + raw
|
path = path + "?" + raw
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprintf(out, "[GIN] %v |%s %3d %s| %13v | %15s |%s %-7s %s %s\n%s",
|
fmt.Fprintf(cout, "[GIN] %v |%s %3d %s| %13v | %15s |%s %-7s %s %s\n%s",
|
||||||
end.Format("2006/01/02 - 15:04:05"),
|
end.Format("2006/01/02 - 15:04:05"),
|
||||||
statusColor, statusCode, resetColor,
|
statusColor, statusCode, resetColor,
|
||||||
latency,
|
latency,
|
||||||
|
3
mode.go
3
mode.go
@ -28,9 +28,6 @@ const (
|
|||||||
// middleware output like Logger() or Recovery().
|
// middleware output like Logger() or Recovery().
|
||||||
// Note that both Logger and Recovery provides custom ways to configure their
|
// Note that both Logger and Recovery provides custom ways to configure their
|
||||||
// output io.Writer.
|
// output io.Writer.
|
||||||
// To support coloring in Windows use:
|
|
||||||
// import "github.com/mattn/go-colorable"
|
|
||||||
// gin.DefaultWriter = colorable.NewColorableStdout()
|
|
||||||
var DefaultWriter io.Writer = os.Stdout
|
var DefaultWriter io.Writer = os.Stdout
|
||||||
var DefaultErrorWriter io.Writer = os.Stderr
|
var DefaultErrorWriter io.Writer = os.Stderr
|
||||||
|
|
||||||
|
8
vendor/vendor.json
vendored
8
vendor/vendor.json
vendored
@ -45,6 +45,12 @@
|
|||||||
"revision": "8f2d6ace262eba462e9beb552382c98be51d807b",
|
"revision": "8f2d6ace262eba462e9beb552382c98be51d807b",
|
||||||
"revisionTime": "2015-05-31T20:46:25Z"
|
"revisionTime": "2015-05-31T20:46:25Z"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"checksumSHA1": "cTDA66oZUy18cIzJsU1diKq+9CE=",
|
||||||
|
"path": "github.com/mattn/go-colorable",
|
||||||
|
"revision": "ad5389df28cdac544c99bd7b9161a0b5b6ca9d1b",
|
||||||
|
"revisionTime": "2017-08-16T03:18:13Z"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"checksumSHA1": "U6lX43KDDlNOn+Z0Yyww+ZzHfFo=",
|
"checksumSHA1": "U6lX43KDDlNOn+Z0Yyww+ZzHfFo=",
|
||||||
"path": "github.com/mattn/go-isatty",
|
"path": "github.com/mattn/go-isatty",
|
||||||
@ -91,7 +97,7 @@
|
|||||||
"revisionTime": "2016-10-18T08:54:36Z"
|
"revisionTime": "2016-10-18T08:54:36Z"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"checksumSHA1": "TVEkpH3gq84iQ39I4R+mlDwjuVI=",
|
"checksumSHA1": "/oZpHfYc+ZgOwYAhlvcMhmETYpw=",
|
||||||
"path": "golang.org/x/sys/unix",
|
"path": "golang.org/x/sys/unix",
|
||||||
"revision": "99f16d856c9836c42d24e7ab64ea72916925fa97",
|
"revision": "99f16d856c9836c42d24e7ab64ea72916925fa97",
|
||||||
"revisionTime": "2017-03-08T15:04:45Z"
|
"revisionTime": "2017-03-08T15:04:45Z"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user