Skip to content
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

support --access-log-rotate-time #38

Merged
merged 1 commit into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,24 @@ Usage:
chocon [OPTIONS]

Application Options:
-l, --listen= address to bind (default: 0.0.0.0)
-p, --port= Port number to bind (default: 3000)
--access-log-dir= directory to store logfiles
--access-log-rotate= Number of day before remove logs (default: 30)
-v, --version Show version
-c, --keepalive-conns= maximum keepalive connections for upstream (default: 2)
--max-conns-per-host= maximum connections per host (default: 0)
--read-timeout= timeout of reading request (default: 30)
--write-timeout= timeout of writing response (default: 90)
--proxy-read-timeout= timeout of reading response from upstream (default: 60)
--upstream= upstream server: http://upstream-server/
--stsize= buffer size for http stats (default: 1000)
--spfactor= sampling factor for http stats (default: 3)
-l, --listen= address to bind (default: 0.0.0.0)
-p, --port= Port number to bind (default: 3000)
--access-log-dir= directory to store logfiles
--access-log-rotate= Number of rotation before remove logs (default: 30)
--access-log-rotate-time= Interval minutes between file rotation (default: 1440)
-v, --version Show version
--pid-file= filename to store pid. disabled by default
-c, --keepalive-conns= maximum keepalive connections for upstream (default: 2)
--max-conns-per-host= maximum connections per host (default: 0)
--read-timeout= timeout of reading request (default: 30)
--write-timeout= timeout of writing response (default: 90)
--proxy-read-timeout= timeout of reading response from upstream (default: 60)
--shutdown-timeout= timeout to wait for all connections to be closed. (default: 1h)
--upstream= upstream server: http://upstream-server/
--stsize= buffer size for http stats (default: 1000)
--spfactor= sampling factor for http stats (default: 3)

Help Options:
-h, --help Show this help message
-h, --help Show this help message

```
8 changes: 4 additions & 4 deletions accesslog/accesslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type AccessLog struct {
logger *zap.Logger
}

func logWriter(logDir string, logRotate int64) (io.Writer, error) {
func logWriter(logDir string, logRotate int64, logRotateTime int64) (io.Writer, error) {
if logDir == "stdout" {
return os.Stdout, nil
} else if logDir == "" {
Expand All @@ -40,7 +40,7 @@ func logWriter(logDir string, logRotate int64) (io.Writer, error) {
logFile,
rotatelogs.WithLinkName(linkName),
rotatelogs.WithMaxAge(time.Duration(logRotate)*86400*time.Second),
rotatelogs.WithRotationTime(time.Second*86400),
rotatelogs.WithRotationTime(time.Second*time.Duration(logRotateTime)*60),
)
if err != nil {
return nil, errors.Wrap(err, "rotatelogs.New failed")
Expand All @@ -49,8 +49,8 @@ func logWriter(logDir string, logRotate int64) (io.Writer, error) {
}

// New :
func New(logDir string, logRotate int64) (*AccessLog, error) {
w, err := logWriter(logDir, logRotate)
func New(logDir string, logRotate int64, logRotateTime int64) (*AccessLog, error) {
w, err := logWriter(logDir, logRotate, logRotateTime)
if err != nil {
return nil, err
}
Expand Down
13 changes: 7 additions & 6 deletions chocon.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (
"syscall"
"time"

"github.com/fukata/golang-stats-api-handler"
stats_api "github.com/fukata/golang-stats-api-handler"
"github.com/jessevdk/go-flags"
"github.com/kazeburo/chocon/accesslog"
"github.com/kazeburo/chocon/pidfile"
"github.com/kazeburo/chocon/proxy"
"github.com/kazeburo/chocon/upstream"
"github.com/lestrrat/go-server-starter-listener"
ss "github.com/lestrrat/go-server-starter-listener"
statsHTTP "go.mercari.io/go-httpstats"
"go.uber.org/zap"
)
Expand All @@ -34,7 +34,8 @@ type cmdOpts struct {
Listen string `short:"l" long:"listen" default:"0.0.0.0" description:"address to bind"`
Port string `short:"p" long:"port" default:"3000" description:"Port number to bind"`
LogDir string `long:"access-log-dir" default:"" description:"directory to store logfiles"`
LogRotate int64 `long:"access-log-rotate" default:"30" description:"Number of day before remove logs"`
LogRotate int64 `long:"access-log-rotate" default:"30" description:"Number of rotation before remove logs"`
LogRotateTime int64 `long:"access-log-rotate-time" default:"1440" description:"Interval minutes between file rotation"`
Version bool `short:"v" long:"version" description:"Show version"`
PidFile string `long:"pid-file" default:"" description:"filename to store pid. disabled by default"`
KeepaliveConns int `short:"c" default:"2" long:"keepalive-conns" description:"maximum keepalive connections for upstream"`
Expand Down Expand Up @@ -66,8 +67,8 @@ func addStatsHandler(h http.Handler, mw *statsHTTP.Metrics) http.Handler {
})
}

func wrapLogHandler(h http.Handler, logDir string, logRotate int64, logger *zap.Logger) http.Handler {
al, err := accesslog.New(logDir, logRotate)
func wrapLogHandler(h http.Handler, logDir string, logRotate int64, logRotateTime int64, logger *zap.Logger) http.Handler {
al, err := accesslog.New(logDir, logRotate, logRotateTime)
if err != nil {
logger.Fatal("could not init accesslog", zap.Error(err))
}
Expand Down Expand Up @@ -143,7 +144,7 @@ func _main() int {
log.Fatal(err)
}
handler = addStatsHandler(handler, statsChocon)
handler = wrapLogHandler(handler, opts.LogDir, opts.LogRotate, logger)
handler = wrapLogHandler(handler, opts.LogDir, opts.LogRotate, opts.LogRotateTime, logger)
handler = wrapStatsHandler(handler, statsChocon)

server := http.Server{
Expand Down