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 the log file along with its parent directory if not present #12675

Merged
merged 27 commits into from Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
dfd8bcf
Remove Feature Flag From Prater (#12082)
nisdas Mar 6, 2023
e2fa7d4
Use Epoch boundary cache to retrieve balances (#12083)
potuz Mar 6, 2023
16f9f7d
Merge tag 'v4.0.2'
prestonvanloon Apr 19, 2023
2674e20
Merge tag 'v4.0.3'
prestonvanloon Apr 20, 2023
967d7d3
create the log file along with its parent directory if not present
bharath-123 Jul 31, 2023
e3a5e73
only give ReadWritePermissions to the log file
bharath-123 Jul 31, 2023
110e81c
Merge branch 'develop' of https://github.com/prysmaticlabs/prysm into…
bharath-123 Aug 5, 2023
386c186
use io/file package to create the parent directories
bharath-123 Aug 5, 2023
0d787bf
Merge branch 'develop' of https://github.com/prysmaticlabs/prysm into…
bharath-123 Aug 6, 2023
f31cea4
fix ci related issues
bharath-123 Aug 6, 2023
0c05a53
Merge branch 'develop' of https://github.com/prysmaticlabs/prysm into…
bharath-123 Aug 21, 2023
c52cd9e
add regression tests
bharath-123 Aug 22, 2023
4d1c3d6
Merge branch 'master' of https://github.com/prysmaticlabs/prysm into …
bharath-123 Aug 22, 2023
bcfbc8d
Merge branch 'develop' of https://github.com/prysmaticlabs/prysm into…
bharath-123 Aug 22, 2023
52c279f
Merge branch 'develop' of https://github.com/prysmaticlabs/prysm into…
bharath-123 Aug 22, 2023
f6d0da3
run gazelle
bharath-123 Aug 22, 2023
b42f576
Merge branch 'develop' of https://github.com/prysmaticlabs/prysm into…
bharath-123 Aug 23, 2023
35ca363
Merge branch 'develop' of https://github.com/prysmaticlabs/prysm into…
bharath-123 Aug 23, 2023
1cd7df8
fix tests
bharath-123 Aug 23, 2023
e4c691b
Merge branch 'develop' into create-log-file
bharath-123 Aug 24, 2023
ef0e7f0
remove print statements
bharath-123 Aug 25, 2023
eaba83c
Merge branch 'develop' of https://github.com/prysmaticlabs/prysm into…
bharath-123 Aug 25, 2023
7991d9c
Merge branch 'create-log-file' of https://github.com/bharath-123/prys…
bharath-123 Aug 25, 2023
be2ee88
Merge branch 'develop' into create-log-file
bharath-123 Aug 28, 2023
5266bcc
Merge branch 'develop' into create-log-file
prestonvanloon Mar 22, 2024
5636470
gazelle
prestonvanloon Mar 22, 2024
6c8719a
Remove failing test for MkdirAll, this failure is not expected
prestonvanloon Mar 22, 2024
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
1 change: 1 addition & 0 deletions io/logs/BUILD.bazel
Expand Up @@ -13,6 +13,7 @@ go_library(
"//cache/lru:go_default_library",
"//config/params:go_default_library",
"//crypto/rand:go_default_library",
"//io/file:go_default_library",
"@com_github_hashicorp_golang_lru//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
],
Expand Down
5 changes: 5 additions & 0 deletions io/logs/logutil.go
Expand Up @@ -6,9 +6,11 @@ import (
"io"
"net/url"
"os"
"path/filepath"
"strings"

"github.com/prysmaticlabs/prysm/v5/config/params"
"github.com/prysmaticlabs/prysm/v5/io/file"
"github.com/sirupsen/logrus"
)

Expand All @@ -20,6 +22,9 @@ func addLogWriter(w io.Writer) {
// ConfigurePersistentLogging adds a log-to-file writer. File content is identical to stdout.
func ConfigurePersistentLogging(logFileName string) error {
logrus.WithField("logFileName", logFileName).Info("Logs will be made persistent")
if err := file.MkdirAll(filepath.Dir(logFileName)); err != nil {
return err
}
f, err := os.OpenFile(logFileName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, params.BeaconIoConfig().ReadWritePermissions) // #nosec G304
if err != nil {
return err
Expand Down
37 changes: 37 additions & 0 deletions io/logs/logutil_test.go
@@ -1,6 +1,8 @@
package logs

import (
"fmt"
"os"
"testing"

"github.com/prysmaticlabs/prysm/v5/testing/require"
Expand All @@ -24,3 +26,38 @@ func TestMaskCredentialsLogging(t *testing.T) {
require.Equal(t, MaskCredentialsLogging(test.url), test.maskedUrl)
}
}

func TestConfigurePersistantLogging(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

For temporary directories for tests use t.TempDir() , this gets cleaned up by the test runner after the test is executed.

testParentDir := t.TempDir()

// 1. Test creation of file in an existing parent directory
logFileName := "test.log"
existingDirectory := "test-1-existing-testing-dir"

err := ConfigurePersistentLogging(fmt.Sprintf("%s/%s/%s", testParentDir, existingDirectory, logFileName))
require.NoError(t, err)

// 2. Test creation of file along with parent directory
nonExistingDirectory := "test-2-non-existing-testing-dir"

err = ConfigurePersistentLogging(fmt.Sprintf("%s/%s/%s", testParentDir, nonExistingDirectory, logFileName))
require.NoError(t, err)

// 3. Test creation of file in an existing parent directory with a non-existing sub-directory
existingDirectory = "test-3-existing-testing-dir"
nonExistingSubDirectory := "test-3-non-existing-sub-dir"
err = os.Mkdir(fmt.Sprintf("%s/%s", testParentDir, existingDirectory), 0700)
if err != nil {
return
}

err = ConfigurePersistentLogging(fmt.Sprintf("%s/%s/%s/%s", testParentDir, existingDirectory, nonExistingSubDirectory, logFileName))
require.NoError(t, err)

//4. Create log file in a directory without 700 permissions
existingDirectory = "test-4-existing-testing-dir"
err = os.Mkdir(fmt.Sprintf("%s/%s", testParentDir, existingDirectory), 0750)
if err != nil {
return
}
}