-
Notifications
You must be signed in to change notification settings - Fork 542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add access log for Manager API #994
Changes from 7 commits
37c1694
64d8285
ab3fe0c
0a94375
63b8647
1629a2e
aaf3e6a
95482ad
cf6cdcb
45129f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,7 @@ var ( | |
ETCDConfig *Etcd | ||
ErrorLogLevel = "warn" | ||
ErrorLogPath = "logs/error.log" | ||
AccessLogPath = "logs/access.log" | ||
UserList = make(map[string]User, 2) | ||
AuthConf Authentication | ||
SSLDefaultStatus = 1 //enable ssl by default | ||
|
@@ -69,8 +70,14 @@ type ErrorLog struct { | |
FilePath string `yaml:"file_path"` | ||
} | ||
|
||
type AccessLog struct { | ||
Level string | ||
FilePath string `yaml:"file_path"` | ||
} | ||
|
||
type Log struct { | ||
ErrorLog ErrorLog `yaml:"error_log"` | ||
ErrorLog ErrorLog `yaml:"error_log"` | ||
AccessLog AccessLog `yaml:"access_log"` | ||
} | ||
|
||
type Conf struct { | ||
|
@@ -149,6 +156,17 @@ func setConf() { | |
} | ||
} | ||
|
||
// access log | ||
if config.Conf.Log.AccessLog.FilePath != "" { | ||
AccessLogPath = config.Conf.Log.AccessLog.FilePath | ||
} | ||
if !filepath.IsAbs(AccessLogPath) { | ||
AccessLogPath, err = filepath.Abs(WorkDir + "/" + AccessLogPath) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed. |
||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
//auth | ||
initAuthentication(config.Authentication) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,53 @@ | |
*/ | ||
package filter | ||
|
||
//for logging access log, will refactor it in a new pr. | ||
import ( | ||
"bytes" | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func RequestLogHandler(logger *zap.SugaredLogger) gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
start, host, remoteIP, path, method := time.Now(), c.Request.Host, c.ClientIP(), c.Request.URL.Path, c.Request.Method | ||
query := c.Request.URL.RawQuery | ||
requestId := c.Writer.Header().Get("X-Request-Id") | ||
|
||
blw := &bodyLogWriter{body: bytes.NewBufferString(""), ResponseWriter: c.Writer} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed. |
||
c.Writer = blw | ||
c.Next() | ||
latency := time.Since(start) / 1000000 | ||
statusCode := c.Writer.Status() | ||
//respBody := blw.body.String() | ||
|
||
var errs []string | ||
for _, err := range c.Errors { | ||
errs = append(errs, err.Error()) | ||
} | ||
|
||
logger.Desugar().Info(path, | ||
//zap.String("path", path), | ||
zap.Int("status", statusCode), | ||
zap.String("host", host), | ||
zap.String("query", query), | ||
zap.String("requestId", requestId), | ||
zap.Duration("latency", latency), | ||
zap.String("remoteIP", remoteIP), | ||
zap.String("method", method), | ||
//zap.String("respBody", respBody), | ||
zap.Strings("errs", errs), | ||
) | ||
} | ||
} | ||
|
||
type bodyLogWriter struct { | ||
gin.ResponseWriter | ||
body *bytes.Buffer | ||
} | ||
|
||
func (w bodyLogWriter) Write(b []byte) (int, error) { | ||
w.body.Write(b) | ||
return w.ResponseWriter.Write(b) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
log level in access log is no sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can change the zapcore encoder to remove the level encoding.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed.