Create log folder if not present

This commit is contained in:
tanyadegurechaff 2017-04-28 00:36:04 +09:00 committed by Emile Vauge
parent c1220b8765
commit 987ae92f53
3 changed files with 29 additions and 13 deletions

View file

@ -7,6 +7,7 @@ import (
fmtlog "log" fmtlog "log"
"net/http" "net/http"
"os" "os"
"path/filepath"
"reflect" "reflect"
"runtime" "runtime"
"strings" "strings"
@ -206,6 +207,13 @@ func run(traefikConfiguration *server.TraefikConfiguration) {
} }
log.SetLevel(level) log.SetLevel(level)
if len(globalConfiguration.TraefikLogsFile) > 0 { if len(globalConfiguration.TraefikLogsFile) > 0 {
dir := filepath.Dir(globalConfiguration.TraefikLogsFile)
err := os.MkdirAll(dir, 0755)
if err != nil {
log.Errorf("Failed to create log path %s: %s", dir, err)
}
fi, err := os.OpenFile(globalConfiguration.TraefikLogsFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) fi, err := os.OpenFile(globalConfiguration.TraefikLogsFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
defer func() { defer func() {
if err := fi.Close(); err != nil { if err := fi.Close(); err != nil {

View file

@ -7,6 +7,7 @@ import (
"net" "net"
"net/http" "net/http"
"os" "os"
"path/filepath"
"strconv" "strconv"
"strings" "strings"
"sync/atomic" "sync/atomic"
@ -54,6 +55,13 @@ type logInfoResponseWriter struct {
// NewLogger returns a new Logger instance. // NewLogger returns a new Logger instance.
func NewLogger(file string) *Logger { func NewLogger(file string) *Logger {
if len(file) > 0 { 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) fi, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil { if err != nil {
log.Error("Error opening file", err) log.Error("Error opening file", err)

View file

@ -7,7 +7,6 @@ import (
"net/url" "net/url"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"testing" "testing"
shellwords "github.com/mattn/go-shellwords" shellwords "github.com/mattn/go-shellwords"
@ -18,8 +17,7 @@ type logtestResponseWriter struct{}
var ( var (
logger *Logger logger *Logger
logfileName = "traefikTestLogger.log" logfileNameSuffix = "/traefik/logger/test.log"
logfilePath string
helloWorld = "Hello, World" helloWorld = "Hello, World"
testBackendName = "http://127.0.0.1/testBackend" testBackendName = "http://127.0.0.1/testBackend"
testFrontendName = "testFrontend" testFrontendName = "testFrontend"
@ -39,14 +37,21 @@ var (
) )
func TestLogger(t *testing.T) { func TestLogger(t *testing.T) {
if runtime.GOOS == "windows" { tmp, err := ioutil.TempDir("", "testlogger")
logfilePath = filepath.Join(os.Getenv("TEMP"), logfileName) if err != nil {
} else { t.Fatalf("failed to create temp dir: %s", err)
logfilePath = filepath.Join("/tmp", logfileName)
} }
defer os.RemoveAll(tmp)
logfilePath := filepath.Join(tmp, logfileNameSuffix)
logger = NewLogger(logfilePath) 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) SetBackend2FrontendMap(&testBackend2FrontendMap)
r := &http.Request{ r := &http.Request{
@ -86,11 +91,6 @@ func TestLogger(t *testing.T) {
} }
} }
func cleanup() {
logger.Close()
os.Remove(logfilePath)
}
func printLogdata(logdata []byte) string { func printLogdata(logdata []byte) string {
return fmt.Sprintf( return fmt.Sprintf(
"\nExpected: %s\n"+ "\nExpected: %s\n"+