add code for always opening new with default dir and name. with test.
This commit is contained in:
@@ -184,6 +184,9 @@ func (l *Logger) openNew() (*os.File, error) {
|
|||||||
// no such file or the write would put it over the MaxSize, a new file is
|
// no such file or the write would put it over the MaxSize, a new file is
|
||||||
// created.
|
// created.
|
||||||
func (l *Logger) openExistingOrNew(writeLen int) (*os.File, error) {
|
func (l *Logger) openExistingOrNew(writeLen int) (*os.File, error) {
|
||||||
|
if l.Dir == "" && l.NameFormat == "" {
|
||||||
|
return l.openNew()
|
||||||
|
}
|
||||||
files, err := ioutil.ReadDir(l.dir())
|
files, err := ioutil.ReadDir(l.dir())
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
return l.openNew()
|
return l.openNew()
|
||||||
|
|||||||
+35
-1
@@ -120,13 +120,14 @@ func TestMakeLogDir(t *testing.T) {
|
|||||||
func TestDefaultLogDir(t *testing.T) {
|
func TestDefaultLogDir(t *testing.T) {
|
||||||
currentTime = fakeTime
|
currentTime = fakeTime
|
||||||
dir := os.TempDir()
|
dir := os.TempDir()
|
||||||
defer os.RemoveAll(dir)
|
|
||||||
l := &Logger{
|
l := &Logger{
|
||||||
NameFormat: format,
|
NameFormat: format,
|
||||||
}
|
}
|
||||||
defer l.Close()
|
defer l.Close()
|
||||||
b := []byte("boo!")
|
b := []byte("boo!")
|
||||||
n, err := l.Write(b)
|
n, err := l.Write(b)
|
||||||
|
defer os.Remove(logFile(dir))
|
||||||
|
|
||||||
isNil(err, t)
|
isNil(err, t)
|
||||||
equals(len(b), n, t)
|
equals(len(b), n, t)
|
||||||
existsWithLen(logFile(dir), n, t)
|
existsWithLen(logFile(dir), n, t)
|
||||||
@@ -371,6 +372,39 @@ func TestLocalTime(t *testing.T) {
|
|||||||
existsWithLen(filename, n, t)
|
existsWithLen(filename, n, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDefaultDirAndName(t *testing.T) {
|
||||||
|
currentTime = fakeTime
|
||||||
|
|
||||||
|
l := &Logger{MaxSize: Megabyte}
|
||||||
|
defer l.Close()
|
||||||
|
b := []byte("boo!")
|
||||||
|
n, err := l.Write(b)
|
||||||
|
filename := filepath.Join(os.TempDir(), fakeTime().UTC().Format(defaultNameFormat))
|
||||||
|
defer os.Remove(filename)
|
||||||
|
|
||||||
|
isNil(err, t)
|
||||||
|
equals(len(b), n, t)
|
||||||
|
|
||||||
|
existsWithLen(filename, n, t)
|
||||||
|
|
||||||
|
err = l.Close()
|
||||||
|
isNil(err, t)
|
||||||
|
|
||||||
|
defer newFakeTime(Day)()
|
||||||
|
|
||||||
|
// even though the old file is under MaxSize, we should write a new file
|
||||||
|
// to prevent two processes using lumberjack from writing to the same file.
|
||||||
|
n, err = l.Write(b)
|
||||||
|
|
||||||
|
f2 := filepath.Join(os.TempDir(), fakeTime().UTC().Format(defaultNameFormat))
|
||||||
|
defer os.Remove(f2)
|
||||||
|
|
||||||
|
isNil(err, t)
|
||||||
|
equals(len(b), n, t)
|
||||||
|
|
||||||
|
existsWithLen(f2, n, t)
|
||||||
|
}
|
||||||
|
|
||||||
// makeTempDir creates a file with a semi-unique name in the OS temp directory.
|
// makeTempDir creates a file with a semi-unique name in the OS temp directory.
|
||||||
// It should be based on the name of the test, to keep parallel tests from
|
// It should be based on the name of the test, to keep parallel tests from
|
||||||
// colliding, and must be cleaned up after the test is finished.
|
// colliding, and must be cleaned up after the test is finished.
|
||||||
|
|||||||
Reference in New Issue
Block a user