defer close files
This commit is contained in:
parent
36847f671f
commit
0881151a44
2 changed files with 11 additions and 3 deletions
|
@ -29,9 +29,13 @@ func NewLogger(file string) *Logger {
|
|||
}
|
||||
|
||||
func (l *Logger) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
||||
if(l.file == nil){
|
||||
if (l.file == nil) {
|
||||
next(rw, r)
|
||||
}else{
|
||||
}else {
|
||||
handlers.CombinedLoggingHandler(l.file, next).ServeHTTP(rw, r)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Logger) Close() {
|
||||
l.file.Close()
|
||||
}
|
|
@ -46,6 +46,9 @@ func main() {
|
|||
// load global configuration
|
||||
gloablConfiguration := LoadFileConfig(*globalConfigFile)
|
||||
|
||||
loggerMiddleware := middlewares.NewLogger(gloablConfiguration.AccessLogsFile)
|
||||
defer loggerMiddleware.Close()
|
||||
|
||||
// logging
|
||||
backends := []logging.Backend{}
|
||||
level, err := logging.LogLevel(gloablConfiguration.LogLevel)
|
||||
|
@ -55,6 +58,7 @@ func main() {
|
|||
|
||||
if len(gloablConfiguration.TraefikLogsFile) > 0 {
|
||||
fi, err := os.OpenFile(gloablConfiguration.TraefikLogsFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||
defer fi.Close()
|
||||
if err != nil {
|
||||
log.Fatal("Error opening file", err)
|
||||
} else {
|
||||
|
@ -137,7 +141,7 @@ func main() {
|
|||
// middlewares
|
||||
var negroni = negroni.New()
|
||||
negroni.Use(metrics)
|
||||
negroni.Use(middlewares.NewLogger(gloablConfiguration.AccessLogsFile))
|
||||
negroni.Use(loggerMiddleware)
|
||||
negroni.UseHandler(configurationRouter)
|
||||
|
||||
srv = &graceful.Server{
|
||||
|
|
Loading…
Reference in a new issue