Skip to content

Commit 746b21f

Browse files
Vasilii Iakliushine_forbes
authored andcommitted
Merge branch 'ef-adopt-nicer-file-writing' into 'main'
Bumps the LabKit version to adopt the NewWithFile logging constructor See merge request https://gitlab.com/gitlab-org/gitlab-shell/-/merge_requests/1372 Merged-by: Vasilii Iakliushin <viakliushin@gitlab.com> Approved-by: James Fargher <jfargher@gitlab.com> Approved-by: Vasilii Iakliushin <viakliushin@gitlab.com> Reviewed-by: James Fargher <jfargher@gitlab.com> Reviewed-by: Vasilii Iakliushin <viakliushin@gitlab.com> Co-authored-by: e_forbes <eforbes@gitlab.com>
2 parents 406e658 + 9650cad commit 746b21f

5 files changed

Lines changed: 33 additions & 19 deletions

File tree

cmd/gitlab-sshd/main.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func overrideConfigFromEnvironment(cfg *config.Config) {
4343
}
4444
}
4545

46+
// nolint
4647
func main() {
4748
ctx := context.Background()
4849
command.CheckForVersionFlag(os.Args, Version, BuildTime)
@@ -58,7 +59,17 @@ func main() {
5859
))
5960
}
6061
}
61-
log := logger.ConfigureLogger(cfg)
62+
log, logCloser, err := logger.ConfigureLogger(cfg)
63+
if err != nil {
64+
log.ErrorContext(ctx, "failed to log to file, reverting to stderr", slog.String(fields.ErrorMessage, err.Error()))
65+
} else {
66+
// nolint
67+
defer func() {
68+
if err = logCloser.Close(); err != nil {
69+
log.ErrorContext(ctx, "failed to close log file", slog.String(fields.ErrorMessage, err.Error()))
70+
}
71+
}()
72+
}
6273
log.InfoContext(ctx, "gitlab-sshd starting up...")
6374

6475
overrideConfigFromEnvironment(cfg)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ require (
1919
github.com/sirupsen/logrus v1.9.4
2020
github.com/stretchr/testify v1.11.1
2121
gitlab.com/gitlab-org/gitaly/v18 v18.9.0-rc4
22-
gitlab.com/gitlab-org/labkit v1.36.0
22+
gitlab.com/gitlab-org/labkit v1.37.0
2323
golang.org/x/crypto v0.47.0
2424
golang.org/x/sync v0.19.0
2525
google.golang.org/grpc v1.77.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,8 @@ gitlab.com/gitlab-org/gitaly/v18 v18.9.0-rc4 h1:2PQlCQF5ioVdmcjY6Ut8zxwFY2TIgHWn
571571
gitlab.com/gitlab-org/gitaly/v18 v18.9.0-rc4/go.mod h1:ijDPC9GyV3UdmrZG4LiBQJyMjOuzLcbH5AuQ8GpSSIk=
572572
gitlab.com/gitlab-org/go/reopen v1.0.0 h1:6BujZ0lkkjGIejTUJdNO1w56mN1SI10qcVQyQlOPM+8=
573573
gitlab.com/gitlab-org/go/reopen v1.0.0/go.mod h1:D6OID8YJDzEVZNYW02R/Pkj0v8gYFSIhXFTArAsBQw8=
574-
gitlab.com/gitlab-org/labkit v1.36.0 h1:QA60DLXmtOyIRchrxaRwonrtbviWQogA623tR2JijE4=
575-
gitlab.com/gitlab-org/labkit v1.36.0/go.mod h1:JqQLdgjV/KKAZJ6gvNodaLStmWeTT9mxgJPIEi66VHI=
574+
gitlab.com/gitlab-org/labkit v1.37.0 h1:Nk7DIc5QB8QNQgrNhymDACMMSIwjjaHXL4CVSHmXvac=
575+
gitlab.com/gitlab-org/labkit v1.37.0/go.mod h1:JqQLdgjV/KKAZJ6gvNodaLStmWeTT9mxgJPIEi66VHI=
576576
go.etcd.io/raft/v3 v3.6.0 h1:5NtvbDVYpnfZWcIHgGRk9DyzkBIXOi8j+DDp1IcnUWQ=
577577
go.etcd.io/raft/v3 v3.6.0/go.mod h1:nLvLevg6+xrVtHUmVaTcTz603gQPHfh7kUAwV6YpfGo=
578578
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=

internal/logger/logger.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,14 @@ import (
1818
// ConfigureLogger - gitlab-sshd's log output can be configured to text as per the documentation:
1919
// https://docs.gitlab.com/omnibus/settings/logs/#json-logging
2020
// This is currently controlled by the GITLAB_LOG_FORMAT environment variable.
21-
func ConfigureLogger(cfg *config.Config) *slog.Logger {
21+
func ConfigureLogger(cfg *config.Config) (*slog.Logger, io.Closer, error) {
2222
logConfig := &v2log.Config{
2323
LogLevel: parseLogLevel(cfg.LogLevel),
2424
}
25-
logFile, err := os.OpenFile(cfg.LogFile, os.O_APPEND|os.O_WRONLY, 0600)
26-
if err != nil {
27-
syslogError(err)
28-
logConfig.Writer = os.Stderr
29-
} else {
30-
logConfig.Writer = logFile
31-
}
32-
3325
if gitlabLogFormat := os.Getenv("GITLAB_LOG_FORMAT"); gitlabLogFormat == "text" {
3426
logConfig.UseTextFormat = true
3527
}
36-
return v2log.NewWithConfig(logConfig)
28+
return v2log.NewWithFile(cfg.LogFile, logConfig)
3729
}
3830

3931
func parseLogLevel(level string) slog.Level {

internal/logger/logger_test.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package logger
22

33
import (
4+
"io"
45
"os"
56
"regexp"
67
"testing"
@@ -20,7 +21,7 @@ func TestConfigure(t *testing.T) {
2021
}
2122

2223
closer := Configure(&config)
23-
defer closer.Close()
24+
defer MustClose(t, closer)
2425

2526
log.Info("this is a test")
2627
log.WithFields(log.Fields{}).Debug("debug log message")
@@ -43,7 +44,7 @@ func TestConfigureWithDebugLogLevel(t *testing.T) {
4344
}
4445

4546
closer := Configure(&config)
46-
defer closer.Close()
47+
defer MustClose(t, closer)
4748

4849
log.WithFields(log.Fields{}).Debug("debug log message")
4950

@@ -61,7 +62,7 @@ func TestConfigureWithPermissionError(t *testing.T) {
6162
}
6263

6364
closer := Configure(&config)
64-
defer closer.Close()
65+
defer MustClose(t, closer)
6566

6667
log.Info("this is a test")
6768
}
@@ -75,7 +76,7 @@ func TestLogInUTC(t *testing.T) {
7576
}
7677

7778
closer := Configure(&config)
78-
defer closer.Close()
79+
defer MustClose(t, closer)
7980

8081
log.Info("this is a test")
8182

@@ -107,7 +108,9 @@ func TestConfigureLabkitV2Log(t *testing.T) {
107108
LogLevel: "debug",
108109
}
109110

110-
logger := ConfigureLogger(&config)
111+
logger, closer, err := ConfigureLogger(&config)
112+
require.NoError(t, err)
113+
defer MustClose(t, closer)
111114
logger.Info("this is a test")
112115
logger.Debug("debug log message")
113116

@@ -117,3 +120,11 @@ func TestConfigureLabkitV2Log(t *testing.T) {
117120
require.Contains(t, dataStr, `"msg":"this is a test"`)
118121
require.Contains(t, dataStr, `"msg":"debug log message"`)
119122
}
123+
124+
// MustClose calls Close() on the Closer and fails the test in case it returns
125+
// an error. This function is useful when closing via `defer`, as a simple
126+
// `defer require.NoError(t, closer.Close())` would cause `closer.Close()` to
127+
// be executed early already.
128+
func MustClose(tb testing.TB, closer io.Closer) {
129+
require.NoError(tb, closer.Close())
130+
}

0 commit comments

Comments
 (0)