Merge remote-tracking branch 'upstream/v1.3' into merge-v1_3
This commit is contained in:
commit
8ad31d6eb4
10 changed files with 357 additions and 245 deletions
|
@ -7,6 +7,7 @@ import (
|
|||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
|
@ -55,6 +56,13 @@ type logInfoResponseWriter struct {
|
|||
// NewLogger returns a new Logger instance.
|
||||
func NewLogger(file string) *Logger {
|
||||
if len(file) > 0 {
|
||||
dir := filepath.Dir(file)
|
||||
|
||||
err := os.MkdirAll(dir, 0755)
|
||||
if err != nil {
|
||||
log.Errorf("Failed to create log path %s: %s", dir, err)
|
||||
}
|
||||
|
||||
fi, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
log.Error("Error opening file", err)
|
||||
|
|
|
@ -8,7 +8,6 @@ import (
|
|||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"github.com/containous/traefik/middlewares/accesslog"
|
||||
|
@ -20,8 +19,7 @@ type logtestResponseWriter struct{}
|
|||
|
||||
var (
|
||||
logger *Logger
|
||||
logfileName = "traefikTestLogger.log"
|
||||
logfilePath string
|
||||
logfileNameSuffix = "/traefik/logger/test.log"
|
||||
helloWorld = "Hello, World"
|
||||
testBackendName = "http://127.0.0.1/testBackend"
|
||||
testFrontendName = "testFrontend"
|
||||
|
@ -41,14 +39,21 @@ var (
|
|||
)
|
||||
|
||||
func TestLogger(t *testing.T) {
|
||||
if runtime.GOOS == "windows" {
|
||||
logfilePath = filepath.Join(os.Getenv("TEMP"), logfileName)
|
||||
} else {
|
||||
logfilePath = filepath.Join("/tmp", logfileName)
|
||||
tmp, err := ioutil.TempDir("", "testlogger")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create temp dir: %s", err)
|
||||
}
|
||||
defer os.RemoveAll(tmp)
|
||||
|
||||
logfilePath := filepath.Join(tmp, logfileNameSuffix)
|
||||
|
||||
logger = NewLogger(logfilePath)
|
||||
defer cleanup()
|
||||
defer logger.Close()
|
||||
|
||||
if _, err := os.Stat(logfilePath); os.IsNotExist(err) {
|
||||
t.Fatalf("logger should create %s", logfilePath)
|
||||
}
|
||||
|
||||
SetBackend2FrontendMap(&testBackend2FrontendMap)
|
||||
|
||||
r := &http.Request{
|
||||
|
@ -96,11 +101,6 @@ func TestLogger(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func cleanup() {
|
||||
logger.Close()
|
||||
os.Remove(logfilePath)
|
||||
}
|
||||
|
||||
func printLogdata(logdata []byte) string {
|
||||
return fmt.Sprintf(
|
||||
"\nExpected: %s\n"+
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue