@@ -2,9 +2,11 @@ package collector
22
33import (
44 "context"
5- "os"
5+ "fmt"
6+ "log/slog"
67
78 "github.com/codex-team/hawk.collector/pkg/accounts"
9+ log "github.com/codex-team/hawk.collector/pkg/logger"
810
911 "github.com/caarlos0/env/v6"
1012 "github.com/codex-team/hawk.collector/cmd"
@@ -15,7 +17,6 @@ import (
1517 "github.com/codex-team/hawk.collector/pkg/redis"
1618 "github.com/codex-team/hawk.collector/pkg/server"
1719 "github.com/joho/godotenv"
18- log "github.com/sirupsen/logrus"
1920)
2021
2122// RunCommand - Run server in the production mode
@@ -27,48 +28,49 @@ type RunCommand struct {
2728
2829// Execute Run server - Load configuration file and start server
2930func (x * RunCommand ) Execute (args []string ) error {
30- if err := godotenv .Load (); err != nil {
31- log .Println ("File .env not found, reading configuration from ENV" )
31+ envLoadErr := godotenv .Load ()
32+ var err error
33+
34+ // setup logging as early as possible so all logs go to stdout + OTEL
35+ otelShutdown := log .SetupFromEnv (context .Background ())
36+ defer func () {
37+ if shutdownErr := otelShutdown (context .Background ()); shutdownErr != nil {
38+ slog .Warn ("failed to shutdown OTLP logger" , "error" , shutdownErr )
39+ }
40+ }()
41+
42+ if envLoadErr != nil {
43+ slog .Info ("File .env not found, reading configuration from ENV" )
3244 }
3345
3446 // load config from .env
3547 var cfg cmd.Config
3648 if err := env .Parse (& cfg ); err != nil {
37- log .Fatalf ("Failed to parse ENV" )
38- }
39-
40- // setup logging and set log level from config
41- level , err := log .ParseLevel (cfg .LogLevel )
42- if err != nil {
43- level = log .ErrorLevel
49+ slog .Error ("Failed to parse ENV" , "error" , err )
50+ return err
4451 }
45- log .SetFormatter (& log.TextFormatter {
46- FullTimestamp : true ,
47- })
48- log .SetOutput (os .Stdout )
49- log .SetLevel (level )
50- log .Infof ("✓ Log level set on %s" , level )
52+ slog .InfoContext (context .Background (), fmt .Sprintf ("collector started on %s" , cfg .Listen ), "event" , "startup" )
5153
5254 // Initialize Hawk Catcher
5355 if cfg .HawkEnabled {
5456 err = hawk .Init ()
5557 if err != nil {
56- log . Errorf ("✗ Cannot initialize Hawk Catcher: %s " , err )
58+ slog . Error ("✗ Cannot initialize Hawk Catcher" , "error " , err )
5759 } else {
5860 go hawk .HawkCatcher .Run ()
5961 defer hawk .HawkCatcher .Stop ()
60- log . Infof ("✓ Hawk Catcher initialized on %s " , hawk .HawkCatcher .GetURL ())
62+ slog . Info ("✓ Hawk Catcher initialized" , "url " , hawk .HawkCatcher .GetURL ())
6163 }
6264 }
6365
6466 // connect to AMQP broker with retries
65- log . Infof ("Connecting to RabbitMQ %s " , cfg .BrokerURL )
67+ slog . Info ("Connecting to RabbitMQ" , "url " , cfg .BrokerURL )
6668 brokerObj := broker .New (cfg .BrokerURL , cfg .Exchange )
6769 brokerObj .Init ()
68- log . Infof ("✓ Broker initialized on %s " , cfg .BrokerURL )
70+ slog . Info ("✓ Broker initialized" , "url " , cfg .BrokerURL )
6971
7072 // connect to Redis
71- log . Infof ("Connecting to Redis %s " , cfg .RedisURL )
73+ slog . Info ("Connecting to Redis" , "url " , cfg .RedisURL )
7274 ctx := context .Background ()
7375 ctx , cancel := context .WithCancel (ctx )
7476 defer cancel ()
@@ -84,7 +86,7 @@ func (x *RunCommand) Execute(args []string) error {
8486
8587 err = redisClient .LoadBlockedIDs ()
8688 if err != nil {
87- log . Errorf ("failed to load blocked IDs from Redis" )
89+ slog . Error ("failed to load blocked IDs from Redis" , "error" , err )
8890 }
8991
9092 // connect to accounts MongoDB
@@ -93,12 +95,12 @@ func (x *RunCommand) Execute(args []string) error {
9395
9496 err = accountsClient .UpdateTokenCache ()
9597 if err != nil {
96- log . Errorf ("failed to update token cache: %s " , err )
98+ slog . Error ("failed to update token cache" , "error " , err )
9799 }
98100
99101 err = accountsClient .UpdateProjectsLimitsCache ()
100102 if err != nil {
101- log . Errorf ("failed to update projects limits cache: %s " , err )
103+ slog . Error ("failed to update projects limits cache" , "error " , err )
102104 }
103105
104106 go periodic .RunPeriodically (accountsClient .UpdateTokenCache , cfg .TokenUpdatePeriod , doneAccountsContext )
@@ -112,7 +114,7 @@ func (x *RunCommand) Execute(args []string) error {
112114 go periodic .RunPeriodically (redisClient .LoadBlockedIDs , cfg .BlockedIDsLoad , done )
113115 go periodic .RunPeriodically (serverObj .UpdateBlacklist , cfg .BlacklistUpdatePeriod , done )
114116 defer close (done )
115- log .Info ("✓ Redis client initialized" )
117+ slog .Info ("✓ Redis client initialized" )
116118
117119 // listen and serve prometheus metrics
118120 go metrics .RunServer (cfg .MetricsListen )
0 commit comments