From cb3f240f4466ee9db61d67353ff2e08c982ba1d1 Mon Sep 17 00:00:00 2001 From: GuyCheung Date: Tue, 21 Jun 2016 15:52:31 +0800 Subject: [PATCH 1/2] add os.Chmod when create log file --- logs/file.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/logs/file.go b/logs/file.go index 7798a221..beaa9171 100644 --- a/logs/file.go +++ b/logs/file.go @@ -159,6 +159,10 @@ func (w *fileLogWriter) createLogFile() (*os.File, error) { return nil, err } fd, err := os.OpenFile(w.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.FileMode(perm)) + if err == nil { + // Make sure file perm is user set perm cause of `os.OpenFile` will obey umask + os.Chmod(w.Filename, w.Perm) + } return fd, err } From 9572fdcf9a3f8cbedc9786e8ffbcc40cae2fb804 Mon Sep 17 00:00:00 2001 From: GuyCheung Date: Wed, 22 Jun 2016 09:57:16 +0800 Subject: [PATCH 2/2] update error on type of w.Perm; change unit test perm value --- logs/file.go | 2 +- logs/file_test.go | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/logs/file.go b/logs/file.go index beaa9171..42146dae 100644 --- a/logs/file.go +++ b/logs/file.go @@ -161,7 +161,7 @@ func (w *fileLogWriter) createLogFile() (*os.File, error) { fd, err := os.OpenFile(w.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.FileMode(perm)) if err == nil { // Make sure file perm is user set perm cause of `os.OpenFile` will obey umask - os.Chmod(w.Filename, w.Perm) + os.Chmod(w.Filename, os.FileMode(perm)) } return fd, err } diff --git a/logs/file_test.go b/logs/file_test.go index 23370947..69a66d84 100644 --- a/logs/file_test.go +++ b/logs/file_test.go @@ -26,7 +26,8 @@ import ( func TestFilePerm(t *testing.T) { log := NewLogger(10000) - log.SetLogger("file", `{"filename":"test.log", "perm": "0600"}`) + // use 0666 as test perm cause the default umask is 022 + log.SetLogger("file", `{"filename":"test.log", "perm": "0666"}`) log.Debug("debug") log.Informational("info") log.Notice("notice") @@ -39,7 +40,7 @@ func TestFilePerm(t *testing.T) { if err != nil { t.Fatal(err) } - if file.Mode() != 0600 { + if file.Mode() != 0666 { t.Fatal("unexpected log file permission") } os.Remove("test.log")