-
Notifications
You must be signed in to change notification settings - Fork 883
/
logger.go
83 lines (69 loc) · 2.14 KB
/
logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (C) MongoDB, Inc. 2023-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package unified
import (
"go.mongodb.org/mongo-driver/internal/logger"
)
// orderedLogMessage is a logMessage with an "order" field representing the
// order in which the log message was observed.
type orderedLogMessage struct {
*logMessage
order int
}
// Logger is the Sink used to captured log messages for logger verification in
// the unified spec tests.
type Logger struct {
lastOrder int
logQueue chan orderedLogMessage
bufSize int
}
func newLogger(olm *observeLogMessages, bufSize int) *Logger {
if olm == nil {
return nil
}
return &Logger{
lastOrder: 1,
logQueue: make(chan orderedLogMessage, bufSize),
bufSize: bufSize,
}
}
// Info implements the logger.Sink interface's "Info" method for printing log
// messages.
func (log *Logger) Info(level int, msg string, args ...interface{}) {
if log.logQueue == nil {
return
}
defer func() { log.lastOrder++ }()
// If the order is greater than the buffer size, we must return. This
// would indicate that the logQueue channel has been closed.
if log.lastOrder > log.bufSize {
return
}
// Add the Diff back to the level, as there is no need to create a
// logging offset.
level = level + logger.DiffToInfo
logMessage, err := newLogMessage(level, msg, args...)
if err != nil {
panic(err)
}
// Send the log message to the "orderedLogMessage" channel for
// validation.
log.logQueue <- orderedLogMessage{
order: log.lastOrder + 1,
logMessage: logMessage,
}
// If the order has reached the buffer size, then close the channe.
if log.lastOrder == log.bufSize {
close(log.logQueue)
}
}
// Error implements the logger.Sink interface's "Error" method for printing log
// errors. In this case, if an error occurs we will simply treat it as
// informational.
func (log *Logger) Error(err error, msg string, args ...interface{}) {
args = append(args, "error", err)
log.Info(int(logger.LevelInfo), msg, args)
}