@@ -64,7 +64,7 @@ func (ctrl *LogPersistenceController) Outputs() []controller.Output {
6464 }
6565}
6666
67- func (ctrl * LogPersistenceController ) filenameForId (id string ) string {
67+ func (ctrl * LogPersistenceController ) filenameForID (id string ) string {
6868 return filepath .Join (constants .LogMountPoint , id + ".log" )
6969}
7070
@@ -77,7 +77,7 @@ func (ctrl *LogPersistenceController) getLogFile(id string, overwrite bool) (*os
7777 return f , nil
7878 }
7979
80- f , err = os .OpenFile (ctrl .filenameForId (id ), os .O_APPEND | os .O_CREATE | os .O_WRONLY , 0o644 )
80+ f , err = os .OpenFile (ctrl .filenameForID (id ), os .O_APPEND | os .O_CREATE | os .O_WRONLY , 0o644 )
8181 if err != nil {
8282 return nil , fmt .Errorf ("error opening log file for %q: %w" , id , err )
8383 }
@@ -89,6 +89,7 @@ func (ctrl *LogPersistenceController) getLogFile(id string, overwrite bool) (*os
8989 return f , nil
9090}
9191
92+ // WriteLog writes a single log line into the corresponding file.
9293func (ctrl * LogPersistenceController ) WriteLog (id string , line []byte ) error {
9394 var err error
9495
@@ -116,13 +117,15 @@ func (ctrl *LogPersistenceController) stopLogging() error {
116117 // Stop all logging activities, close files
117118 // after this call we should not hold /var/log
118119 ctrl .canLog .Lock ()
120+
119121 ctrl .filesMutex .Lock ()
120122 defer ctrl .filesMutex .Unlock ()
121123
122124 for id := range ctrl .files {
123125 if err := ctrl .files [id ].Close (); err != nil {
124126 return fmt .Errorf ("error closing log file for %q: %w" , id , err )
125127 }
128+
126129 delete (ctrl .files , id )
127130 }
128131
@@ -142,15 +145,16 @@ func (ctrl *LogPersistenceController) rotate(id string) error {
142145 return fmt .Errorf ("error closing log file for %q: %w" , id , err )
143146 }
144147
145- filename := ctrl .filenameForId (id )
148+ filename := ctrl .filenameForID (id )
149+
146150 err := os .Rename (filename , filename + ".1" )
147151 if err != nil {
148- return fmt .Errorf ("rename : %w" , err )
152+ return fmt .Errorf ("error renaming log file for %q : %w" , id , err )
149153 }
150154
151155 _ , err = ctrl .getLogFile (id , true )
152156 if err != nil {
153- return fmt .Errorf ("create : %w" , err )
157+ return fmt .Errorf ("error creating log file for %q : %w" , id , err )
154158 }
155159
156160 return nil
@@ -175,10 +179,11 @@ func (ctrl *LogPersistenceController) Run(ctx context.Context, r controller.Runt
175179 return nil
176180 case <- tickerC :
177181 for id := range ctrl .files {
178- st , err := os .Stat (ctrl .filenameForId (id ))
182+ st , err := os .Stat (ctrl .filenameForID (id ))
179183 if err != nil {
180184 return fmt .Errorf ("error stat logfile %s: %w" , id , err )
181185 }
186+
182187 if st .Size () >= 512 * 1024 {
183188 err = ctrl .rotate (id )
184189 if err != nil {
0 commit comments