Skip to content

Commit

Permalink
Avoid closing stdout when the accesslog handler is closed
Browse files Browse the repository at this point in the history
Co-authored-by: Ludovic Fernandez <ldez@users.noreply.github.com>
Co-authored-by: jlevesy <julien.levesy@containo.us>
  • Loading branch information
3 people authored and traefiker committed Sep 23, 2019
1 parent 2167108 commit 640eb62
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions middlewares/accesslog/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package accesslog
import (
"context"
"fmt"
"io"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -32,6 +33,19 @@ const (
JSONFormat = "json"
)

type noopCloser struct {
*os.File
}

func (n noopCloser) Write(p []byte) (int, error) {
return n.File.Write(p)
}

func (n noopCloser) Close() error {
// noop
return nil
}

type logHandlerParams struct {
logDataTable *LogData
crr *captureRequestReader
Expand All @@ -42,7 +56,7 @@ type logHandlerParams struct {
type LogHandler struct {
config *types.AccessLog
logger *logrus.Logger
file *os.File
file io.WriteCloser
mu sync.Mutex
httpCodeRanges types.HTTPCodeRanges
logHandlerChan chan logHandlerParams
Expand All @@ -51,7 +65,7 @@ type LogHandler struct {

// NewLogHandler creates a new LogHandler
func NewLogHandler(config *types.AccessLog) (*LogHandler, error) {
file := os.Stdout
var file io.WriteCloser = noopCloser{os.Stdout}
if len(config.FilePath) > 0 {
f, err := openAccessLogFile(config.FilePath)
if err != nil {
Expand Down Expand Up @@ -205,14 +219,15 @@ func (l *LogHandler) Close() error {
// Rotate closes and reopens the log file to allow for rotation
// by an external source.
func (l *LogHandler) Rotate() error {
var err error
if l.config.FilePath == "" {
return nil
}

if l.file != nil {
defer func(f *os.File) {
f.Close()
}(l.file)
defer func(f io.Closer) { _ = f.Close() }(l.file)
}

var err error
l.file, err = os.OpenFile(l.config.FilePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0664)
if err != nil {
return err
Expand Down

0 comments on commit 640eb62

Please sign in to comment.