Skip to content

Commit d4592d4

Browse files
authored
fix EXP-19: allow access to supervisor logs within a workspace (#18322)
1 parent 8f15603 commit d4592d4

File tree

5 files changed

+68
-7
lines changed

5 files changed

+68
-7
lines changed

components/supervisor/cmd/init.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ var initCmd = &cobra.Command{
3030
Use: "init",
3131
Short: "init the supervisor",
3232
Run: func(cmd *cobra.Command, args []string) {
33-
log.Init(ServiceName, Version, true, false)
34-
log.Log.Logger.AddHook(fatalTerminationLogHook{})
33+
logFile := initLog(true)
34+
defer logFile.Close()
3535

3636
cfg, err := supervisor.GetConfig()
3737
if err != nil {

components/supervisor/cmd/root.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ package cmd
66

77
import (
88
"fmt"
9+
"io"
910
"os"
11+
"time"
1012

1113
"github.com/sirupsen/logrus"
1214
"github.com/spf13/cobra"
1315

1416
"github.com/gitpod-io/gitpod/common-go/log"
17+
prefixed "github.com/x-cray/logrus-prefixed-formatter"
1518
)
1619

1720
// rootCmd represents the base command when called without any subcommands.
@@ -52,3 +55,48 @@ func (fatalTerminationLogHook) Fire(e *logrus.Entry) error {
5255

5356
return os.WriteFile("/dev/termination-log", []byte(msg), 0o644)
5457
}
58+
59+
const (
60+
gitpodLogDir = "/var/log/gitpod"
61+
supervisorLogFilePath = gitpodLogDir + "/supervisor.log"
62+
)
63+
64+
func initLog(json bool) io.Closer {
65+
log.Init(ServiceName, Version, json, os.Getenv("SUPERVISOR_DEBUG_ENABLE") == "true")
66+
log.Log.Logger.AddHook(fatalTerminationLogHook{})
67+
if err := os.MkdirAll(gitpodLogDir, 0755); err != nil {
68+
log.WithError(err).Error("cannot create gitpod log directory")
69+
return io.NopCloser(nil)
70+
}
71+
supervisorLogFile, err := os.OpenFile(supervisorLogFilePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
72+
if err != nil {
73+
log.WithError(err).Error("cannot open supervisor log file")
74+
return io.NopCloser(nil)
75+
}
76+
log.Log.Logger.AddHook(&FileHook{
77+
Writer: supervisorLogFile,
78+
Formatter: &prefixed.TextFormatter{
79+
TimestampFormat: time.RFC3339Nano,
80+
FullTimestamp: true,
81+
},
82+
})
83+
return supervisorLogFile
84+
}
85+
86+
type FileHook struct {
87+
Writer io.Writer
88+
Formatter logrus.Formatter
89+
}
90+
91+
func (hook *FileHook) Fire(entry *logrus.Entry) error {
92+
formatted, err := hook.Formatter.Format(entry)
93+
if err != nil {
94+
return err
95+
}
96+
_, err = hook.Writer.Write(formatted)
97+
return err
98+
}
99+
100+
func (hook *FileHook) Levels() []logrus.Level {
101+
return logrus.AllLevels
102+
}

components/supervisor/cmd/run.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@
55
package cmd
66

77
import (
8-
"os"
9-
108
"github.com/spf13/cobra"
119

1210
common_grpc "github.com/gitpod-io/gitpod/common-go/grpc"
13-
"github.com/gitpod-io/gitpod/common-go/log"
1411
"github.com/gitpod-io/gitpod/supervisor/pkg/supervisor"
1512
)
1613

@@ -23,8 +20,8 @@ var runCmd = &cobra.Command{
2320
Short: "starts the supervisor",
2421

2522
Run: func(cmd *cobra.Command, args []string) {
26-
log.Init(ServiceName, Version, !runOpts.RunGP, os.Getenv("SUPERVISOR_DEBUG_ENABLE") == "true")
27-
log.Log.Logger.AddHook(fatalTerminationLogHook{})
23+
logFile := initLog(!runOpts.RunGP)
24+
defer logFile.Close()
2825

2926
common_grpc.SetupLogging()
3027
supervisor.Version = Version

components/supervisor/go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ require (
4444
gopkg.in/yaml.v3 v3.0.1
4545
)
4646

47+
require (
48+
github.com/mattn/go-colorable v0.1.8 // indirect
49+
github.com/mattn/go-isatty v0.0.14 // indirect
50+
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
51+
)
52+
4753
require (
4854
github.com/gitpod-io/gitpod/components/scrubber v0.0.0-00010101000000-000000000000 // indirect
4955
github.com/mitchellh/reflectwalk v1.0.2 // indirect
@@ -131,6 +137,7 @@ require (
131137
github.com/stretchr/testify v1.8.1 // indirect
132138
github.com/uber/jaeger-client-go v2.29.1+incompatible // indirect
133139
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
140+
github.com/x-cray/logrus-prefixed-formatter v0.5.2
134141
github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c // indirect
135142
go.opencensus.io v0.24.0 // indirect
136143
go.uber.org/atomic v1.4.0 // indirect

components/supervisor/go.sum

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)