Added lumberjack library for log rolling

This commit is contained in:
Maxime 2015-08-08 16:13:10 +02:00
parent afd6b7ea27
commit bb5a322ce2
2 changed files with 14 additions and 2 deletions

View File

@ -11,6 +11,7 @@ import (
"github.com/hashicorp/go-syslog"
"github.com/mholt/caddy/middleware"
"github.com/mholt/caddy/middleware/errors"
"gopkg.in/natefinch/lumberjack.v2"
)
// Errors configures a new gzip middleware instance.
@ -35,10 +36,15 @@ func Errors(c *Controller) (middleware.Middleware, error) {
return err
}
} else if handler.LogFile != "" {
file, err = os.OpenFile(handler.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
_, err = os.OpenFile(handler.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
return err
}
file = &lumberjack.Logger{
Filename: handler.LogFile,
MaxSize: 20,
MaxBackups: 10,
}
}
handler.Log = log.New(file, "", 0)

View File

@ -9,6 +9,7 @@ import (
"github.com/mholt/caddy/middleware"
caddylog "github.com/mholt/caddy/middleware/log"
"github.com/mholt/caddy/server"
"gopkg.in/natefinch/lumberjack.v2"
)
// Log sets up the logging middleware.
@ -34,10 +35,15 @@ func Log(c *Controller) (middleware.Middleware, error) {
return err
}
} else {
file, err = os.OpenFile(rules[i].OutputFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
_, err = os.OpenFile(rules[i].OutputFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
return err
}
file = &lumberjack.Logger{
Filename: rules[i].OutputFile,
MaxSize: 20,
MaxBackups: 10,
}
}
rules[i].Log = log.New(file, "", 0)