forked from Silverfish/proton-bridge
feat(GODT-2691): close logrus output file on exit.
This commit is contained in:
@ -21,6 +21,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
@ -88,7 +89,7 @@ func (cs *coloredStdOutHook) Fire(entry *logrus.Entry) error {
|
||||
|
||||
// Init Initialize logging. Log files are rotated when their size exceeds rotationSize. if pruningSize >= 0, pruning occurs using
|
||||
// the default pruning algorithm.
|
||||
func Init(logsPath string, sessionID SessionID, appName AppName, rotationSize, pruningSize int64, level string) error {
|
||||
func Init(logsPath string, sessionID SessionID, appName AppName, rotationSize, pruningSize int64, level string) (io.Closer, error) {
|
||||
logrus.SetFormatter(&logrus.TextFormatter{
|
||||
DisableColors: true,
|
||||
FullTimestamp: true,
|
||||
@ -99,12 +100,22 @@ func Init(logsPath string, sessionID SessionID, appName AppName, rotationSize, p
|
||||
|
||||
rotator, err := NewDefaultRotator(logsPath, sessionID, appName, rotationSize, pruningSize)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
logrus.SetOutput(rotator)
|
||||
|
||||
return setLevel(level)
|
||||
return rotator, setLevel(level)
|
||||
}
|
||||
|
||||
// Close closes the log file. if closer is nil, no error is reported.
|
||||
func Close(closer io.Closer) error {
|
||||
if closer == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
logrus.SetOutput(os.Stdout)
|
||||
return closer.Close()
|
||||
}
|
||||
|
||||
// ZipLogsForBugReport returns an archive containing the logs for bug report.
|
||||
|
||||
@ -23,6 +23,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/ProtonMail/proton-bridge/v3/internal/constants"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
@ -93,3 +94,12 @@ func TestLogging_GetOrderedLogFileListForBugReport(t *testing.T) {
|
||||
filepath.Join(dir, string(sessionID1)+"_lau_000"+fileSuffix),
|
||||
}, filePaths)
|
||||
}
|
||||
|
||||
func TestLogging_Close(t *testing.T) {
|
||||
d := t.TempDir()
|
||||
closer, err := Init(d, NewSessionID(), constants.AppName, 1, DefaultPruningSize, "debug")
|
||||
require.NoError(t, err)
|
||||
logrus.Debug("Test") // because we set max log file size to 1, this will force a rotation of the log file.
|
||||
require.NotNil(t, closer)
|
||||
require.NoError(t, closer.Close())
|
||||
}
|
||||
|
||||
@ -86,6 +86,14 @@ func (r *Rotator) Write(p []byte) (int, error) {
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (r *Rotator) Close() error {
|
||||
if r.wc != nil {
|
||||
return r.wc.Close()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Rotator) rotate() error {
|
||||
if r.wc != nil {
|
||||
_ = r.wc.Close()
|
||||
|
||||
Reference in New Issue
Block a user