Skip to content

Commit f358e24

Browse files
Logging: Add interface with default glog implementation (prebid#4085)
Co-authored-by: postindustria-code <[email protected]>
1 parent f829bea commit f358e24

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+839
-274
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ An option for developing Prebid Server in a reproducible environment isolated fr
9292

9393
Prebid Server is not currently intended to be imported by other projects. Go Modules is used to manage dependencies, which also makes it possible to import Prebid Server packages. This is not supported. We offer no guarantees regarding the stability of packages and do not adhere to semantic versioning guidelines.
9494

95+
## Swapping Global Dependencies
96+
97+
Logger is a global side-effectful dependency that sometimes needs to be swapped to modify the behavior.
98+
The `Logger` package contains an interface definition for unstructured logging with built-in `glog` implementation.
99+
The interface provides standard logging methods: `Debug`, `Info`, `Warn`, `Error`, and `Fatal`.
100+
The `glog` implementation is based on `github.com/golang/glog` package and serves as the concrete implementation for the logging interface.
101+
By default, the package uses the `glog` logger implementation.
102+
95103
## Contributing
96104
> [!IMPORTANT]
97105
> All contributions must follow the [Prebid Code of Conduct](https://prebid.org/code-of-conduct/) and the [Prebid Module Rules](https://docs.prebid.org/dev-docs/module-rules.html).

analytics/agma/agma_module.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import (
1212

1313
"github.com/benbjohnson/clock"
1414
"github.com/docker/go-units"
15-
"github.com/golang/glog"
1615
"github.com/prebid/go-gdpr/vendorconsent"
1716
"github.com/prebid/prebid-server/v3/analytics"
1817
"github.com/prebid/prebid-server/v3/config"
18+
"github.com/prebid/prebid-server/v3/logger"
1919
"github.com/prebid/prebid-server/v3/openrtb_ext"
2020
)
2121

@@ -93,7 +93,7 @@ func (l *AgmaLogger) start() {
9393
for {
9494
select {
9595
case <-l.sigTermCh:
96-
glog.Infof("[AgmaAnalytics] Received Close, trying to flush buffer")
96+
logger.Infof("[AgmaAnalytics] Received Close, trying to flush buffer")
9797
l.flush()
9898
return
9999
case event := <-l.bufferCh:
@@ -139,7 +139,7 @@ func (l *AgmaLogger) flush() {
139139
if err != nil {
140140
l.reset()
141141
l.mux.Unlock()
142-
glog.Warning("[AgmaAnalytics] fail to copy the buffer")
142+
logger.Warnf("[AgmaAnalytics] fail to copy the buffer")
143143
return
144144
}
145145

@@ -223,7 +223,7 @@ func (l *AgmaLogger) LogAuctionObject(event *analytics.AuctionObject) {
223223
}
224224
data, err := serializeAnayltics(event.RequestWrapper, EventTypeAuction, code, event.StartTime)
225225
if err != nil {
226-
glog.Errorf("[AgmaAnalytics] Error serializing auction object: %v", err)
226+
logger.Errorf("[AgmaAnalytics] Error serializing auction object: %v", err)
227227
return
228228
}
229229
l.bufferCh <- data
@@ -239,7 +239,7 @@ func (l *AgmaLogger) LogAmpObject(event *analytics.AmpObject) {
239239
}
240240
data, err := serializeAnayltics(event.RequestWrapper, EventTypeAmp, code, event.StartTime)
241241
if err != nil {
242-
glog.Errorf("[AgmaAnalytics] Error serializing amp object: %v", err)
242+
logger.Errorf("[AgmaAnalytics] Error serializing amp object: %v", err)
243243
return
244244
}
245245
l.bufferCh <- data
@@ -255,14 +255,14 @@ func (l *AgmaLogger) LogVideoObject(event *analytics.VideoObject) {
255255
}
256256
data, err := serializeAnayltics(event.RequestWrapper, EventTypeVideo, code, event.StartTime)
257257
if err != nil {
258-
glog.Errorf("[AgmaAnalytics] Error serializing video object: %v", err)
258+
logger.Errorf("[AgmaAnalytics] Error serializing video object: %v", err)
259259
return
260260
}
261261
l.bufferCh <- data
262262
}
263263

264264
func (l *AgmaLogger) Shutdown() {
265-
glog.Info("[AgmaAnalytics] Shutdown, trying to flush buffer")
265+
logger.Infof("[AgmaAnalytics] Shutdown, trying to flush buffer")
266266
l.flush() // mutex safe
267267
}
268268

analytics/agma/sender.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"net/url"
1111
"time"
1212

13-
"github.com/golang/glog"
1413
"github.com/prebid/prebid-server/v3/config"
14+
"github.com/prebid/prebid-server/v3/logger"
1515
"github.com/prebid/prebid-server/v3/version"
1616
)
1717

@@ -51,7 +51,7 @@ func createHttpSender(httpClient *http.Client, endpoint config.AgmaAnalyticsHttp
5151
if endpoint.Gzip {
5252
requestBody, err = compressToGZIP(payload)
5353
if err != nil {
54-
glog.Errorf("[agmaAnalytics] Compressing request failed %v", err)
54+
logger.Errorf("[agmaAnalytics] Compressing request failed %v", err)
5555
return err
5656
}
5757
} else {
@@ -60,7 +60,7 @@ func createHttpSender(httpClient *http.Client, endpoint config.AgmaAnalyticsHttp
6060

6161
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.Url, bytes.NewBuffer(requestBody))
6262
if err != nil {
63-
glog.Errorf("[agmaAnalytics] Creating request failed %v", err)
63+
logger.Errorf("[agmaAnalytics] Creating request failed %v", err)
6464
return err
6565
}
6666

@@ -72,18 +72,18 @@ func createHttpSender(httpClient *http.Client, endpoint config.AgmaAnalyticsHttp
7272

7373
resp, err := httpClient.Do(req)
7474
if err != nil {
75-
glog.Errorf("[agmaAnalytics] Sending request failed %v", err)
75+
logger.Errorf("[agmaAnalytics] Sending request failed %v", err)
7676
return err
7777
}
7878
defer func() {
7979
if _, err := io.Copy(io.Discard, resp.Body); err != nil {
80-
glog.Errorf("[agmaAnalytics] Draining response body failed: %v", err)
80+
logger.Errorf("[agmaAnalytics] Draining response body failed: %v", err)
8181
}
8282
resp.Body.Close()
8383
}()
8484

8585
if resp.StatusCode != http.StatusOK {
86-
glog.Errorf("[agmaAnalytics] Wrong code received %d instead of %d", resp.StatusCode, http.StatusOK)
86+
logger.Errorf("[agmaAnalytics] Wrong code received %d instead of %d", resp.StatusCode, http.StatusOK)
8787
return fmt.Errorf("wrong code received %d instead of %d", resp.StatusCode, http.StatusOK)
8888
}
8989
return nil

analytics/build/build.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import (
44
"encoding/json"
55

66
"github.com/benbjohnson/clock"
7-
"github.com/golang/glog"
87
"github.com/prebid/prebid-server/v3/analytics"
98
"github.com/prebid/prebid-server/v3/analytics/agma"
109
"github.com/prebid/prebid-server/v3/analytics/clients"
1110
"github.com/prebid/prebid-server/v3/analytics/filesystem"
1211
"github.com/prebid/prebid-server/v3/analytics/pubstack"
1312
"github.com/prebid/prebid-server/v3/config"
13+
"github.com/prebid/prebid-server/v3/logger"
1414
"github.com/prebid/prebid-server/v3/openrtb_ext"
1515
"github.com/prebid/prebid-server/v3/ortb"
1616
"github.com/prebid/prebid-server/v3/privacy"
@@ -23,7 +23,7 @@ func New(analytics *config.Analytics) analytics.Runner {
2323
if mod, err := filesystem.NewFileLogger(analytics.File.Filename); err == nil {
2424
modules["filelogger"] = mod
2525
} else {
26-
glog.Fatalf("Could not initialize FileLogger for file %v :%v", analytics.File.Filename, err)
26+
logger.Fatalf("Could not initialize FileLogger for file %v :%v", analytics.File.Filename, err)
2727
}
2828
}
2929

@@ -40,7 +40,7 @@ func New(analytics *config.Analytics) analytics.Runner {
4040
if err == nil {
4141
modules["pubstack"] = pubstackModule
4242
} else {
43-
glog.Errorf("Could not initialize PubstackModule: %v", err)
43+
logger.Errorf("Could not initialize PubstackModule: %v", err)
4444
}
4545
}
4646

@@ -52,7 +52,7 @@ func New(analytics *config.Analytics) analytics.Runner {
5252
if err == nil {
5353
modules["agma"] = agmaModule
5454
} else {
55-
glog.Errorf("Could not initialize Agma Anayltics: %v", err)
55+
logger.Errorf("Could not initialize Agma Anayltics: %v", err)
5656
}
5757
}
5858

analytics/filesystem/file_module.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
"fmt"
66

77
cglog "github.com/chasex/glog"
8-
"github.com/golang/glog"
98
"github.com/prebid/openrtb/v20/openrtb2"
109
"github.com/prebid/prebid-server/v3/analytics"
10+
"github.com/prebid/prebid-server/v3/logger"
1111
"github.com/prebid/prebid-server/v3/util/jsonutil"
1212
)
1313

@@ -94,7 +94,7 @@ func (f *FileLogger) LogNotificationEventObject(ne *analytics.NotificationEvent)
9494
// Shutdown the logger
9595
func (f *FileLogger) Shutdown() {
9696
// clear all pending buffered data in case there is any
97-
glog.Info("[FileLogger] Shutdown, trying to flush buffer")
97+
logger.Infof("[FileLogger] Shutdown, trying to flush buffer")
9898
f.Logger.Flush()
9999
}
100100

analytics/pubstack/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"time"
1010

1111
"github.com/docker/go-units"
12-
"github.com/golang/glog"
12+
"github.com/prebid/prebid-server/v3/logger"
1313
)
1414

1515
func fetchConfig(client *http.Client, endpoint *url.URL) (*Configuration, error) {
@@ -21,7 +21,7 @@ func fetchConfig(client *http.Client, endpoint *url.URL) (*Configuration, error)
2121
// read the entire response body to ensure full connection reuse if there's an
2222
// error while decoding the json
2323
if _, err := io.Copy(io.Discard, res.Body); err != nil {
24-
glog.Errorf("[pubstack] Draining config response body failed: %v", err)
24+
logger.Errorf("[pubstack] Draining config response body failed: %v", err)
2525
}
2626
res.Body.Close()
2727
}()

analytics/pubstack/eventchannel/eventchannel.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"time"
88

99
"github.com/benbjohnson/clock"
10-
"github.com/golang/glog"
10+
"github.com/prebid/prebid-server/v3/logger"
1111
)
1212

1313
type Metrics struct {
@@ -66,7 +66,7 @@ func (c *EventChannel) buffer(event []byte) {
6666

6767
_, err := c.gz.Write(event)
6868
if err != nil {
69-
glog.Warning("[pubstack] fail to compress, skip the event")
69+
logger.Warnf("[pubstack] fail to compress, skip the event")
7070
return
7171
}
7272

@@ -104,15 +104,15 @@ func (c *EventChannel) flush() {
104104
// finish writing gzip header
105105
err := c.gz.Close()
106106
if err != nil {
107-
glog.Warning("[pubstack] fail to close gzipped buffer")
107+
logger.Warnf("[pubstack] fail to close gzipped buffer")
108108
return
109109
}
110110

111111
// copy the current buffer to send the payload in a new thread
112112
payload := make([]byte, c.buff.Len())
113113
_, err = c.buff.Read(payload)
114114
if err != nil {
115-
glog.Warning("[pubstack] fail to copy the buffer")
115+
logger.Warnf("[pubstack] fail to copy the buffer")
116116
return
117117
}
118118

analytics/pubstack/eventchannel/sender.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"net/url"
99
"path"
1010

11-
"github.com/golang/glog"
11+
"github.com/prebid/prebid-server/v3/logger"
1212
)
1313

1414
type Sender = func(payload []byte) error
@@ -17,7 +17,7 @@ func NewHttpSender(client *http.Client, endpoint string) Sender {
1717
return func(payload []byte) error {
1818
req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewReader(payload))
1919
if err != nil {
20-
glog.Error(err)
20+
logger.Errorf("%v", err)
2121
return err
2222
}
2323

@@ -30,13 +30,13 @@ func NewHttpSender(client *http.Client, endpoint string) Sender {
3030
}
3131
defer func() {
3232
if _, err := io.Copy(io.Discard, resp.Body); err != nil {
33-
glog.Errorf("[pubstack] Draining sender response body failed: %v", err)
33+
logger.Errorf("[pubstack] Draining sender response body failed: %v", err)
3434
}
3535
resp.Body.Close()
3636
}()
3737

3838
if resp.StatusCode != http.StatusOK {
39-
glog.Errorf("[pubstack] Wrong code received %d instead of %d", resp.StatusCode, http.StatusOK)
39+
logger.Errorf("[pubstack] Wrong code received %d instead of %d", resp.StatusCode, http.StatusOK)
4040
return fmt.Errorf("wrong code received %d instead of %d", resp.StatusCode, http.StatusOK)
4141
}
4242
return nil
@@ -46,7 +46,7 @@ func NewHttpSender(client *http.Client, endpoint string) Sender {
4646
func BuildEndpointSender(client *http.Client, baseUrl string, module string) Sender {
4747
endpoint, err := url.Parse(baseUrl)
4848
if err != nil {
49-
glog.Error(err)
49+
logger.Errorf("%v", err)
5050
}
5151
endpoint.Path = path.Join(endpoint.Path, "intake", module)
5252
return NewHttpSender(client, endpoint.String())

analytics/pubstack/pubstack_module.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ import (
1010
"time"
1111

1212
"github.com/benbjohnson/clock"
13-
"github.com/golang/glog"
14-
1513
"github.com/prebid/prebid-server/v3/analytics"
1614
"github.com/prebid/prebid-server/v3/analytics/pubstack/eventchannel"
1715
"github.com/prebid/prebid-server/v3/analytics/pubstack/helpers"
16+
"github.com/prebid/prebid-server/v3/logger"
1817
)
1918

2019
type Configuration struct {
@@ -64,7 +63,7 @@ func NewModule(client *http.Client, scope, endpoint, configRefreshDelay string,
6463
}
6564

6665
func NewModuleWithConfigTask(client *http.Client, scope, endpoint string, maxEventCount int, maxByteSize, maxTime string, configTask ConfigUpdateTask, clock clock.Clock) (analytics.Module, error) {
67-
glog.Infof("[pubstack] Initializing module scope=%s endpoint=%s\n", scope, endpoint)
66+
logger.Infof("[pubstack] Initializing module scope=%s endpoint=%s\n", scope, endpoint)
6867

6968
// parse args
7069
bufferCfg, err := newBufferConfig(maxEventCount, maxByteSize, maxTime)
@@ -103,7 +102,7 @@ func NewModuleWithConfigTask(client *http.Client, scope, endpoint string, maxEve
103102
configChannel := configTask.Start(pb.stopCh)
104103
go pb.start(configChannel)
105104

106-
glog.Info("[pubstack] Pubstack analytics configured and ready")
105+
logger.Infof("[pubstack] Pubstack analytics configured and ready")
107106
return &pb, nil
108107
}
109108

@@ -118,7 +117,7 @@ func (p *PubstackModule) LogAuctionObject(ao *analytics.AuctionObject) {
118117
// serialize event
119118
payload, err := helpers.JsonifyAuctionObject(ao, p.scope)
120119
if err != nil {
121-
glog.Warning("[pubstack] Cannot serialize auction")
120+
logger.Warnf("[pubstack] Cannot serialize auction")
122121
return
123122
}
124123

@@ -139,7 +138,7 @@ func (p *PubstackModule) LogVideoObject(vo *analytics.VideoObject) {
139138
// serialize event
140139
payload, err := helpers.JsonifyVideoObject(vo, p.scope)
141140
if err != nil {
142-
glog.Warning("[pubstack] Cannot serialize video")
141+
logger.Warnf("[pubstack] Cannot serialize video")
143142
return
144143
}
145144

@@ -157,7 +156,7 @@ func (p *PubstackModule) LogSetUIDObject(so *analytics.SetUIDObject) {
157156
// serialize event
158157
payload, err := helpers.JsonifySetUIDObject(so, p.scope)
159158
if err != nil {
160-
glog.Warning("[pubstack] Cannot serialize video")
159+
logger.Warnf("[pubstack] Cannot serialize video")
161160
return
162161
}
163162

@@ -175,7 +174,7 @@ func (p *PubstackModule) LogCookieSyncObject(cso *analytics.CookieSyncObject) {
175174
// serialize event
176175
payload, err := helpers.JsonifyCookieSync(cso, p.scope)
177176
if err != nil {
178-
glog.Warning("[pubstack] Cannot serialize video")
177+
logger.Warnf("[pubstack] Cannot serialize video")
179178
return
180179
}
181180

@@ -193,7 +192,7 @@ func (p *PubstackModule) LogAmpObject(ao *analytics.AmpObject) {
193192
// serialize event
194193
payload, err := helpers.JsonifyAmpObject(ao, p.scope)
195194
if err != nil {
196-
glog.Warning("[pubstack] Cannot serialize video")
195+
logger.Warnf("[pubstack] Cannot serialize video")
197196
return
198197
}
199198

@@ -203,7 +202,7 @@ func (p *PubstackModule) LogAmpObject(ao *analytics.AmpObject) {
203202
// Shutdown - no op since the analytic module already implements system signal handling
204203
// and trying to close a closed channel will cause panic
205204
func (p *PubstackModule) Shutdown() {
206-
glog.Info("[PubstackModule] Shutdown")
205+
logger.Infof("[PubstackModule] Shutdown")
207206
}
208207

209208
func (p *PubstackModule) start(c <-chan *Configuration) {
@@ -216,7 +215,7 @@ func (p *PubstackModule) start(c <-chan *Configuration) {
216215
return
217216
case config := <-c:
218217
p.updateConfig(config)
219-
glog.Infof("[pubstack] Updating config: %v", p.cfg)
218+
logger.Infof("[pubstack] Updating config: %v", p.cfg)
220219
}
221220
}
222221
}

0 commit comments

Comments
 (0)