@@ -161,12 +161,9 @@ func (r *Runner) Start(ctx context.Context, cancelFunc context.CancelFunc) error
161161 for scanner .Scan () {
162162 line := scanner .Text ()
163163 r .state .LastLog = line
164- attrs := append ([]slog.Attr {slog .String ("policy" , r .policyName )}, parseCollectorLog (line )... )
165- args := make ([]any , len (attrs ))
166- for i , attr := range attrs {
167- args [i ] = attr
168- }
169- r .logger .Info ("otelcol-contrib" , args ... )
164+ msg , level , attrs := parseCollectorLog (line )
165+ attrs = append ([]slog.Attr {slog .String ("policy" , r .policyName )}, attrs ... )
166+ r .logger .LogAttrs (r .ctx , level , msg , attrs ... )
170167 if r .cmd .Err != nil {
171168 r .errChan <- r .state .LastLog
172169 }
@@ -229,25 +226,24 @@ func (r *Runner) setStatus(s status) {
229226 r .state .StatusText = mapStatus [s ]
230227}
231228
232- func parseCollectorLog (line string ) []slog.Attr {
229+ func parseCollectorLog (line string ) (string , slog.Level , []slog.Attr ) {
230+ msg := line
231+ level := slog .LevelInfo
232+
233233 if line == "" {
234- return nil
234+ return msg , level , nil
235235 }
236236
237237 parts := strings .SplitN (line , "\t " , 5 )
238238 if len (parts ) == 1 {
239- return []slog. Attr { slog . String ( "log" , line )}
239+ return strings . TrimSpace ( msg ), level , nil
240240 }
241241
242- attrs := make ([]slog.Attr , 0 , len (parts ))
243-
244- if ts := strings .TrimSpace (parts [0 ]); ts != "" {
245- attrs = append (attrs , slog .String ("collector_timestamp" , ts ))
246- }
242+ attrs := make ([]slog.Attr , 0 , len (parts )- 1 )
247243
248244 if len (parts ) > 1 {
249245 if lvl := strings .TrimSpace (parts [1 ]); lvl != "" {
250- attrs = append ( attrs , slog . String ( "collector_level" , lvl ) )
246+ level = mapCollectorLevel ( lvl )
251247 }
252248 }
253249 if len (parts ) > 2 {
@@ -256,13 +252,13 @@ func parseCollectorLog(line string) []slog.Attr {
256252 }
257253 }
258254 if len (parts ) > 3 {
259- if msg := strings .TrimSpace (parts [3 ]); msg != "" {
255+ if msgPart := strings .TrimSpace (parts [3 ]); msgPart != "" {
256+ msgBytes := []byte (msgPart )
260257 var structured any
261- if err := json .Unmarshal ([] byte ( msg ) , & structured ); err == nil {
258+ if err := json .Unmarshal (msgBytes , & structured ); err == nil {
262259 attrs = append (attrs , slog .Any ("collector_message" , structured ))
263- } else {
264- attrs = append (attrs , slog .String ("collector_message" , msg ))
265260 }
261+ msg = msgPart
266262 }
267263 }
268264 if len (parts ) > 4 {
@@ -277,9 +273,20 @@ func parseCollectorLog(line string) []slog.Attr {
277273 }
278274 }
279275
280- if len (attrs ) == 0 {
281- return []slog.Attr {slog .String ("log" , line )}
282- }
276+ return strings .TrimSpace (msg ), level , attrs
277+ }
283278
284- return attrs
279+ func mapCollectorLevel (level string ) slog.Level {
280+ switch strings .ToLower (level ) {
281+ case "debug" :
282+ return slog .LevelDebug
283+ case "warn" , "warning" :
284+ return slog .LevelWarn
285+ case "error" , "err" :
286+ return slog .LevelError
287+ case "fatal" :
288+ return slog .LevelError
289+ default :
290+ return slog .LevelInfo
291+ }
285292}
0 commit comments