Skip to content
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

Create log folder if not present #1507

Merged
merged 1 commit into from
May 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions cmd/traefik/traefik.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
fmtlog "log"
"net/http"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
Expand Down Expand Up @@ -206,6 +207,13 @@ func run(traefikConfiguration *server.TraefikConfiguration) {
}
log.SetLevel(level)
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)
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file seems completely untested, but making it properly testable is likely a bigger refactoring we want to source out to a different PR.

Could you verify manually that this part of the code change works as expected please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

fi, err := os.OpenFile(globalConfiguration.TraefikLogsFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
defer func() {
if err := fi.Close(); err != nil {
Expand Down
8 changes: 8 additions & 0 deletions middlewares/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync/atomic"
Expand Down Expand Up @@ -54,6 +55,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)
Expand Down
26 changes: 13 additions & 13 deletions middlewares/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/url"
"os"
"path/filepath"
"runtime"
"testing"

shellwords "github.com/mattn/go-shellwords"
Expand All @@ -18,8 +17,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"
Expand All @@ -39,14 +37,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{
Expand Down Expand Up @@ -86,11 +91,6 @@ func TestLogger(t *testing.T) {
}
}

func cleanup() {
logger.Close()
os.Remove(logfilePath)
}

func printLogdata(logdata []byte) string {
return fmt.Sprintf(
"\nExpected: %s\n"+
Expand Down