Skip to content

Commit

Permalink
[logs] Add a file logger which only logs to a file (#477)
Browse files Browse the repository at this point in the history
<!--
Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors.
All rights reserved.
SPDX-License-Identifier: Apache-2.0
-->
### Description

- extend the current file logger not to log to stderr or stdout



### Test Coverage

<!--
Please put an `x` in the correct box e.g. `[x]` to indicate the testing
coverage of this change.
-->

- [ ]  This change is covered by existing or additional automated tests.
- [ ] Manual testing has been performed (and evidence provided) as
automated testing was not feasible.
- [x] Additional tests are not required for this change (e.g.
documentation update).
  • Loading branch information
acabarbaye authored Jun 14, 2024
1 parent a02d246 commit 6512809
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 24 deletions.
1 change: 1 addition & 0 deletions changes/20240613181738.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:sparkles: `[logs]` Add a way to use a `Loggers` as a plain `logr.Logger`
1 change: 1 addition & 0 deletions changes/20240614163145.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:sparkles: `[logs]` Add a file logger which only logs to a file
14 changes: 12 additions & 2 deletions utils/logs/file_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,22 @@

package logs

import "github.com/sirupsen/logrus"
import (
"io"

// NewFileLogger creates a logger to a file
"github.com/sirupsen/logrus"
)

// NewFileLogger creates a logger to a file.
func NewFileLogger(logFile string, loggerSource string) (loggers Loggers, err error) {
return NewLogrusLoggerWithFileHook(logrus.New(), loggerSource, logFile)
}

// NewFileOnlyLogger creates a logger to a file such as NewFileLogger but logs are only sent to a file and will not be printed to StdErr or StdOut.
func NewFileOnlyLogger(logFile string, loggerSource string) (loggers Loggers, err error) {
underlying := logrus.New()
underlying.SetOutput(io.Discard)
return NewLogrusLoggerWithFileHook(underlying, loggerSource, logFile)
}

// CreateFileLogger creates a logger to a file
Expand Down
62 changes: 40 additions & 22 deletions utils/logs/file_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package logs

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -14,27 +15,44 @@ import (
)

func TestFileLogger(t *testing.T) {
file, err := filesystem.TempFileInTempDir("test-filelog-*.log")
require.NoError(t, err)
var tests = []struct {
loggerCreationFunc func(path string) (Loggers, error)
}{
{
loggerCreationFunc: func(path string) (Loggers, error) { return NewFileLogger(path, "Test") },
},
{
loggerCreationFunc: func(path string) (Loggers, error) { return NewFileOnlyLogger(path, "Test") },
},
}
for i := range tests {
test := tests[i]
t.Run(fmt.Sprintf("logger %v", i), func(t *testing.T) {
file, err := filesystem.TouchTempFileInTempDir("test-filelog-*.log")
require.NoError(t, err)

defer func() { _ = filesystem.Rm(file) }()

empty, err := filesystem.IsEmpty(file)
require.NoError(t, err)
assert.True(t, empty)

loggers, err := test.loggerCreationFunc(file)
require.NoError(t, err)

testLog(t, loggers)

empty, err = filesystem.IsEmpty(file)
require.NoError(t, err)
assert.False(t, empty)

content, err := filesystem.ReadFile(file)
require.NoError(t, err)
fmt.Println(string(content))

err = filesystem.Rm(file)
require.NoError(t, err)
})
}

err = file.Close()
require.NoError(t, err)

defer func() { _ = filesystem.Rm(file.Name()) }()

empty, err := filesystem.IsEmpty(file.Name())
require.NoError(t, err)
assert.True(t, empty)

loggers, err := NewFileLogger(file.Name(), "Test")
require.NoError(t, err)

testLog(t, loggers)

empty, err = filesystem.IsEmpty(file.Name())
require.NoError(t, err)
assert.False(t, empty)

err = filesystem.Rm(file.Name())
require.NoError(t, err)
}

0 comments on commit 6512809

Please sign in to comment.