@@ -34,6 +34,7 @@ func (w *FileWriteCtx) Write(p []byte) (n int, err error) {
3434 if err := w .Ctx .Err (); err != nil {
3535 return 0 , err
3636 }
37+
3738 return w .Writer .Write (p )
3839}
3940
@@ -89,10 +90,12 @@ func (r *LogRotate) rotateLogs(cfg PluginConfig) error {
8990func (r * LogRotate ) rotateLogFile (logPath string , maxBackups int ) error {
9091 // Rename the current log file
9192 backupPath := logPath + "." + time .Now ().Format ("20060102-150405" )
93+
9294 err := os .Rename (logPath , backupPath )
9395 if err != nil {
9496 return err
9597 }
98+
9699 glob := logPath + ".*"
97100 if r .Compress {
98101 glob = logPath + ".*.gz"
@@ -169,24 +172,30 @@ func compressFile(src string) error {
169172func WriteToFileWithCtx (ctx context.Context , cfg PluginConfig , log string ) error {
170173 FileWriteMutex .Lock ()
171174 defer FileWriteMutex .Unlock ()
175+
172176 originalFileInfo , err := FileWriter .Stat ()
173177 if err != nil {
174178 logger .Error ("Failed to get file info" , "error" , err )
175179 }
180+
176181 currentFileInfo , _ := os .Stat (cfg .LogPath )
177182 if ! os .SameFile (originalFileInfo , currentFileInfo ) {
178183 // The file has been rotated outside our control
179184 logger .Info ("Log file has been rotated or missing attempting to reopen it" )
180185 FileWriter .Close ()
186+
181187 FileWriter , err = os .OpenFile (cfg .LogPath , os .O_APPEND | os .O_CREATE | os .O_WRONLY , 0o644 )
182188 if err != nil {
183189 return err
184190 }
191+
185192 FileInfo , err := FileWriter .Stat ()
186193 if err != nil {
187194 return err
188195 }
196+
189197 FileSize = FileInfo .Size ()
198+
190199 logger .Info ("Log file has been reopened successfully" )
191200 }
192201 n , err := io .WriteString (& FileWriteCtx {Ctx : ctx , Writer : FileWriter }, log )
@@ -204,35 +213,42 @@ func WriteToFileWithCtx(ctx context.Context, cfg PluginConfig, log string) error
204213}
205214
206215func (s * FilePlugin ) Notify (ctx context.Context , notification * protobufs.Notification ) (* protobufs.Empty , error ) {
207- if _ , ok := s .PluginConfigByName [notification .Name ]; ! ok {
208- return nil , fmt .Errorf ("invalid plugin config name %s" , notification .Name )
216+ if _ , ok := s .PluginConfigByName [notification .GetName () ]; ! ok {
217+ return nil , fmt .Errorf ("invalid plugin config name %s" , notification .GetName () )
209218 }
210- cfg := s .PluginConfigByName [notification .Name ]
211219
212- return & protobufs.Empty {}, WriteToFileWithCtx (ctx , cfg , notification .Text )
220+ cfg := s .PluginConfigByName [notification .GetName ()]
221+
222+ return & protobufs.Empty {}, WriteToFileWithCtx (ctx , cfg , notification .GetText ())
213223}
214224
215- func (s * FilePlugin ) Configure (ctx context.Context , config * protobufs.Config ) (* protobufs.Empty , error ) {
225+ func (s * FilePlugin ) Configure (_ context.Context , config * protobufs.Config ) (* protobufs.Empty , error ) {
216226 d := PluginConfig {}
217- err := yaml .Unmarshal (config .Config , & d )
227+
228+ err := yaml .Unmarshal (config .GetConfig (), & d )
218229 if err != nil {
219230 logger .Error ("Failed to parse config" , "error" , err )
220231 return & protobufs.Empty {}, err
221232 }
233+
222234 FileWriteMutex = & sync.Mutex {}
235+
223236 FileWriter , err = os .OpenFile (d .LogPath , os .O_APPEND | os .O_CREATE | os .O_WRONLY , 0o644 )
224237 if err != nil {
225238 logger .Error ("Failed to open log file" , "error" , err )
226239 return & protobufs.Empty {}, err
227240 }
241+
228242 FileInfo , err := FileWriter .Stat ()
229243 if err != nil {
230244 logger .Error ("Failed to get file info" , "error" , err )
231245 return & protobufs.Empty {}, err
232246 }
247+
233248 FileSize = FileInfo .Size ()
234249 s .PluginConfigByName [d .Name ] = d
235250 logger .SetLevel (hclog .LevelFromString (d .LogLevel ))
251+
236252 return & protobufs.Empty {}, err
237253}
238254
0 commit comments