@@ -6,12 +6,15 @@ package cmd
66
77import (
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+ }
0 commit comments