Skip to content

Commit d6a82d2

Browse files
committed
logging fixes
1 parent 364794c commit d6a82d2

2 files changed

Lines changed: 35 additions & 11 deletions

File tree

pkg/datasources/rtve/datasource.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Package rtve provides a datasource implementation for fetching RTVE (Radio Televisión Española)
22
// TV show episodes and their metadata using the rtve-go library.
33
//
4+
// Logging: This datasource now uses the internal pkg/log wrapper.
5+
// - Routine progress messages -> Debugf (visible only when debug enabled globally or for the "rtve:<instance>" service)
6+
// - Warnings / recoverable issues -> Warnf
7+
//
48
// This datasource allows fetching the latest episodes from RTVE shows by show ID,
59
// with configurable limits on the number of episodes to retrieve.
610
//
@@ -26,12 +30,13 @@ import (
2630
"encoding/json"
2731
"fmt"
2832
"io"
29-
"log"
3033
"net/http"
3134
"regexp"
3235
"strings"
3336
"time"
3437

38+
"github.com/rubiojr/ergs/pkg/log"
39+
3540
"github.com/rubiojr/ergs/pkg/core"
3641
"github.com/rubiojr/rtve-go/api"
3742
)
@@ -65,7 +70,7 @@ func parseVTTSubtitles(url string) (string, error) {
6570
}
6671
defer func() {
6772
if closeErr := resp.Body.Close(); closeErr != nil {
68-
log.Printf("RTVE: Warning: failed to close response body: %v", closeErr)
73+
log.ForService("rtve").Warnf("failed to close response body: %v", closeErr)
6974
}
7075
}()
7176

@@ -296,7 +301,8 @@ func (d *Datasource) GetConfig() interface{} {
296301
// - ctx: Context for cancellation and timeouts
297302
// - blockCh: Channel to send created blocks through
298303
func (d *Datasource) FetchBlocks(ctx context.Context, blockCh chan<- core.Block) error {
299-
log.Printf("RTVE: Fetching latest %d episodes from show '%s'", d.config.MaxEpisodes, d.config.ShowID)
304+
l := log.ForService("rtve:" + d.instanceName)
305+
l.Debugf("Fetching latest %d episodes from show '%s'", d.config.MaxEpisodes, d.config.ShowID)
300306

301307
// Create a visitor function that processes each video result
302308
// and sends it as a block through the channel
@@ -322,13 +328,13 @@ func (d *Datasource) FetchBlocks(ctx context.Context, blockCh chan<- core.Block)
322328
if sub.Lang == "es" && sub.Src != "" {
323329
jsonCues, err := parseVTTSubtitles(sub.Src)
324330
if err != nil {
325-
log.Printf("RTVE: Warning: failed to fetch Spanish subtitles for video %s: %v", result.Metadata.ID, err)
331+
l.Warnf("failed to fetch Spanish subtitles for video %s: %v", result.Metadata.ID, err)
326332
} else {
327333
subtitleText = jsonCues
328334
// Count cues for logging
329335
var cues []VTTCue
330336
if json.Unmarshal([]byte(jsonCues), &cues) == nil {
331-
log.Printf("RTVE: Downloaded Spanish subtitles for '%s' (%d cues)", result.Metadata.LongTitle, len(cues))
337+
l.Debugf("Downloaded Spanish subtitles for '%s' (%d cues)", result.Metadata.LongTitle, len(cues))
332338
}
333339
}
334340
}
@@ -353,7 +359,7 @@ func (d *Datasource) FetchBlocks(ctx context.Context, blockCh chan<- core.Block)
353359
case <-ctx.Done():
354360
return ctx.Err()
355361
case blockCh <- block:
356-
log.Printf("RTVE: Fetched episode '%s' (ID: %s)", result.Metadata.LongTitle, result.Metadata.ID)
362+
l.Debugf("Fetched episode '%s' (ID: %s)", result.Metadata.LongTitle, result.Metadata.ID)
357363
return nil
358364
}
359365
}
@@ -364,19 +370,19 @@ func (d *Datasource) FetchBlocks(ctx context.Context, blockCh chan<- core.Block)
364370
return fmt.Errorf("error fetching RTVE show '%s': %w", d.config.ShowID, err)
365371
}
366372

367-
log.Printf("RTVE: Successfully fetched %d episodes from show '%s' (%d errors)",
373+
l.Debugf("Successfully fetched %d episodes from show '%s' (%d errors)",
368374
stats.VideosProcessed, d.config.ShowID, stats.ErrorCount)
369375

370376
// Log any non-fatal errors that occurred during fetching
371377
if stats.ErrorCount > 0 && len(stats.Errors) > 0 {
372-
log.Printf("RTVE: Encountered %d non-fatal errors:", stats.ErrorCount)
378+
l.Warnf("Encountered %d non-fatal errors:", stats.ErrorCount)
373379
for i, err := range stats.Errors {
374380
if i < 5 { // Limit to first 5 errors to avoid spam
375-
log.Printf(" - %v", err)
381+
l.Warnf(" - %v", err)
376382
}
377383
}
378384
if len(stats.Errors) > 5 {
379-
log.Printf(" ... and %d more errors", len(stats.Errors)-5)
385+
l.Warnf(" ... and %d more errors", len(stats.Errors)-5)
380386
}
381387
}
382388

pkg/log/log.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,23 @@ var (
6767

6868
// outputWriter holds the destination for all loggers (wrapped in writerHolder).
6969
outputWriter atomic.Value // writerHolder
70+
71+
// systemd indicates if we detected execution under systemd/journald.
72+
systemd bool
7073
)
7174

7275
func init() {
7376
outputWriter.Store(writerHolder{w: os.Stderr})
77+
systemd = detectSystemd()
78+
}
79+
80+
// detectSystemd returns true if common journald environment markers are present.
81+
// We purposefully keep detection very lightweight.
82+
func detectSystemd() bool {
83+
if os.Getenv("JOURNAL_STREAM") != "" || os.Getenv("INVOCATION_ID") != "" {
84+
return true
85+
}
86+
return false
7487
}
7588

7689
// ForService returns (and memoizes) a named logger for the given service or datasource.
@@ -83,7 +96,12 @@ func ForService(name string) *Logger {
8396
return l.(*Logger)
8497
}
8598
current := outputWriter.Load().(writerHolder).w
86-
std := log.New(current, "", log.LstdFlags|log.Lmicroseconds)
99+
flags := 0
100+
if !systemd {
101+
// When not under systemd, include timestamps & microseconds (original behavior).
102+
flags = log.LstdFlags | log.Lmicroseconds
103+
}
104+
std := log.New(current, "", flags)
87105
logger := &Logger{name: name, std: std}
88106
actual, _ := loggers.LoadOrStore(name, logger)
89107
return actual.(*Logger)

0 commit comments

Comments
 (0)