defer close files

This commit is contained in:
emile 2015-09-12 15:20:56 +02:00
parent 36847f671f
commit 0881151a44
2 changed files with 11 additions and 3 deletions

View file

@ -29,9 +29,13 @@ func NewLogger(file string) *Logger {
} }
func (l *Logger) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) { func (l *Logger) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
if(l.file == nil){ if (l.file == nil) {
next(rw, r) next(rw, r)
}else{ }else {
handlers.CombinedLoggingHandler(l.file, next).ServeHTTP(rw, r) handlers.CombinedLoggingHandler(l.file, next).ServeHTTP(rw, r)
} }
}
func (l *Logger) Close() {
l.file.Close()
} }

View file

@ -46,6 +46,9 @@ func main() {
// load global configuration // load global configuration
gloablConfiguration := LoadFileConfig(*globalConfigFile) gloablConfiguration := LoadFileConfig(*globalConfigFile)
loggerMiddleware := middlewares.NewLogger(gloablConfiguration.AccessLogsFile)
defer loggerMiddleware.Close()
// logging // logging
backends := []logging.Backend{} backends := []logging.Backend{}
level, err := logging.LogLevel(gloablConfiguration.LogLevel) level, err := logging.LogLevel(gloablConfiguration.LogLevel)
@ -55,6 +58,7 @@ func main() {
if len(gloablConfiguration.TraefikLogsFile) > 0 { if len(gloablConfiguration.TraefikLogsFile) > 0 {
fi, err := os.OpenFile(gloablConfiguration.TraefikLogsFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) fi, err := os.OpenFile(gloablConfiguration.TraefikLogsFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
defer fi.Close()
if err != nil { if err != nil {
log.Fatal("Error opening file", err) log.Fatal("Error opening file", err)
} else { } else {
@ -137,7 +141,7 @@ func main() {
// middlewares // middlewares
var negroni = negroni.New() var negroni = negroni.New()
negroni.Use(metrics) negroni.Use(metrics)
negroni.Use(middlewares.NewLogger(gloablConfiguration.AccessLogsFile)) negroni.Use(loggerMiddleware)
negroni.UseHandler(configurationRouter) negroni.UseHandler(configurationRouter)
srv = &graceful.Server{ srv = &graceful.Server{