-
Notifications
You must be signed in to change notification settings - Fork 5.1k
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
Add log file close and reopen on receipt of SIGUSR1 #1761
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
################################################################ | ||
# Global configuration | ||
################################################################ | ||
traefikLogsFile = "traefik.log" | ||
accessLogsFile = "access.log" | ||
logLevel = "ERROR" | ||
defaultEntryPoints = ["http"] | ||
[entryPoints] | ||
[entryPoints.http] | ||
address = ":8000" | ||
|
||
################################################################ | ||
# Web configuration backend | ||
################################################################ | ||
[web] | ||
address = ":7888" | ||
|
||
################################################################ | ||
# File configuration backend | ||
################################################################ | ||
[file] | ||
|
||
################################################################ | ||
# rules | ||
################################################################ | ||
[backends] | ||
[backends.backend1] | ||
[backends.backend1.servers.server1] | ||
url = "http://127.0.0.1:8081" | ||
[frontends] | ||
[frontends.frontend1] | ||
backend = "backend1" | ||
[frontends.frontend1.routes.test_1] | ||
rule = "Path: /test1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
################################################################ | ||
# Global configuration | ||
################################################################ | ||
traefikLogsFile = "traefik.log" | ||
accessLogsFile = "access.log" | ||
logLevel = "DEBUG" | ||
checkNewVersion = false | ||
defaultEntryPoints = ["http"] | ||
[entryPoints] | ||
[entryPoints.http] | ||
address = ":8000" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
// +build !windows | ||
|
||
package integration | ||
|
||
import ( | ||
"bufio" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/containous/traefik/integration/try" | ||
"github.com/go-check/check" | ||
checker "github.com/vdemeester/shakers" | ||
) | ||
|
||
// Log rotation integration test suite | ||
type LogRotationSuite struct{ BaseSuite } | ||
|
||
func (s *LogRotationSuite) TestAccessLogRotation(c *check.C) { | ||
// Start Traefik | ||
cmd, _ := s.cmdTraefik(withConfigFile("fixtures/access_log_config.toml")) | ||
err := cmd.Start() | ||
c.Assert(err, checker.IsNil) | ||
defer cmd.Process.Kill() | ||
defer os.Remove(traefikTestAccessLogFile) | ||
defer os.Remove(traefikTestLogFile) | ||
|
||
// Verify Traefik started ok | ||
verifyEmptyErrorLog(c, "traefik.log") | ||
|
||
// Start test servers | ||
ts1 := startAccessLogServer(8081) | ||
defer ts1.Close() | ||
|
||
// Allow time to startup | ||
time.Sleep(1 * time.Second) | ||
|
||
// Make some requests | ||
err = try.GetRequest("http://127.0.0.1:8000/test1", 500*time.Millisecond) | ||
c.Assert(err, checker.IsNil) | ||
|
||
// Rename access log | ||
err = os.Rename(traefikTestAccessLogFile, traefikTestAccessLogFile+".rotated") | ||
c.Assert(err, checker.IsNil) | ||
|
||
// in the midst of the requests, issue SIGUSR1 signal to server process | ||
err = cmd.Process.Signal(syscall.SIGUSR1) | ||
c.Assert(err, checker.IsNil) | ||
|
||
// continue issuing requests | ||
_, err = http.Get("http://127.0.0.1:8000/test1") | ||
c.Assert(err, checker.IsNil) | ||
_, err = http.Get("http://127.0.0.1:8000/test1") | ||
c.Assert(err, checker.IsNil) | ||
|
||
// Verify access.log.rotated output as expected | ||
lineCount := verifyLogLines(c, traefikTestAccessLogFile+".rotated", 0, true) | ||
c.Assert(lineCount, checker.GreaterOrEqualThan, 1) | ||
|
||
// Verify access.log output as expected | ||
lineCount = verifyLogLines(c, traefikTestAccessLogFile, lineCount, true) | ||
c.Assert(lineCount, checker.Equals, 3) | ||
|
||
verifyEmptyErrorLog(c, traefikTestLogFile) | ||
} | ||
|
||
func (s *LogRotationSuite) TestTraefikLogRotation(c *check.C) { | ||
// Start Traefik | ||
cmd, _ := s.cmdTraefik(withConfigFile("fixtures/traefik_log_config.toml")) | ||
err := cmd.Start() | ||
c.Assert(err, checker.IsNil) | ||
defer cmd.Process.Kill() | ||
defer os.Remove(traefikTestAccessLogFile) | ||
defer os.Remove(traefikTestLogFile) | ||
|
||
// Ensure Traefik has started | ||
err = try.GetRequest("http://127.0.0.1:8000/test1", 500*time.Millisecond) | ||
c.Assert(err, checker.IsNil) | ||
|
||
// Rename traefik log | ||
err = os.Rename(traefikTestLogFile, traefikTestLogFile+".rotated") | ||
c.Assert(err, checker.IsNil) | ||
|
||
// issue SIGUSR1 signal to server process | ||
err = cmd.Process.Signal(syscall.SIGUSR1) | ||
c.Assert(err, checker.IsNil) | ||
|
||
err = cmd.Process.Signal(syscall.SIGTERM) | ||
c.Assert(err, checker.IsNil) | ||
|
||
// Allow time for switch to be processed | ||
err = try.Do(3*time.Second, func() error { | ||
_, err = os.Stat(traefikTestLogFile) | ||
return err | ||
}) | ||
c.Assert(err, checker.IsNil) | ||
|
||
// we have at least 6 lines in traefik.log.rotated | ||
lineCount := verifyLogLines(c, traefikTestLogFile+".rotated", 0, false) | ||
|
||
// GreaterOrEqualThan used to ensure test doesn't break | ||
// If more log entries are output on startup | ||
c.Assert(lineCount, checker.GreaterOrEqualThan, 6) | ||
|
||
//Verify traefik.log output as expected | ||
lineCount = verifyLogLines(c, traefikTestLogFile, lineCount, false) | ||
c.Assert(lineCount, checker.GreaterOrEqualThan, 7) | ||
} | ||
|
||
func verifyEmptyErrorLog(c *check.C, name string) { | ||
err := try.Do(5*time.Second, func() error { | ||
traefikLog, e2 := ioutil.ReadFile(name) | ||
if e2 != nil { | ||
return e2 | ||
} | ||
c.Assert(traefikLog, checker.HasLen, 0) | ||
return nil | ||
}) | ||
c.Assert(err, checker.IsNil) | ||
} | ||
|
||
func verifyLogLines(c *check.C, fileName string, countInit int, accessLog bool) int { | ||
rotated, err := os.Open(fileName) | ||
c.Assert(err, checker.IsNil) | ||
rotatedLog := bufio.NewScanner(rotated) | ||
count := countInit | ||
for rotatedLog.Scan() { | ||
line := rotatedLog.Text() | ||
c.Log(line) | ||
if accessLog { | ||
if len(line) > 0 { | ||
CheckAccessLogFormat(c, line, count) | ||
} | ||
} | ||
count++ | ||
} | ||
|
||
return count | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,8 +31,9 @@ const ( | |
|
||
// LogHandler will write each request and its response to the access log. | ||
type LogHandler struct { | ||
logger *logrus.Logger | ||
file *os.File | ||
logger *logrus.Logger | ||
file *os.File | ||
filePath string | ||
} | ||
|
||
// NewLogHandler creates a new LogHandler | ||
|
@@ -63,7 +64,7 @@ func NewLogHandler(config *types.AccessLog) (*LogHandler, error) { | |
Hooks: make(logrus.LevelHooks), | ||
Level: logrus.InfoLevel, | ||
} | ||
return &LogHandler{logger: logger, file: file}, nil | ||
return &LogHandler{logger: logger, file: file, filePath: config.FilePath}, nil | ||
} | ||
|
||
func openAccessLogFile(filePath string) (*os.File, error) { | ||
|
@@ -139,6 +140,22 @@ func (l *LogHandler) Close() error { | |
return l.file.Close() | ||
} | ||
|
||
// Rotate closes and reopens the log file to allow for rotation | ||
// by an external source. | ||
func (l *LogHandler) Rotate() error { | ||
var err error | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should suffice to be declared on line 150 only if we also scope There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't get that to work. |
||
if err = l.Close(); err != nil { | ||
return err | ||
} | ||
|
||
l.file, err = os.OpenFile(l.filePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0664) | ||
if err != nil { | ||
return err | ||
} | ||
l.logger.Out = l.file | ||
return nil | ||
} | ||
|
||
func silentSplitHostPort(value string) (host string, port string) { | ||
host, port, err := net.SplitHostPort(value) | ||
if err != nil { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we use a build tag here to avoid running the test on Windows (where it would always fail to my understanding)? Users may want to run the tests natively, i.e., outside of our Linux-based build container).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes - added