1
+ // Configures loggers by using the Go driver
1
2
package main
2
3
3
4
import (
@@ -31,12 +32,14 @@ func main() {
31
32
}
32
33
33
34
func standardLogging (uri string ) {
35
+ // Sets options when configuring the standard logger
34
36
// start-standard-logger
35
37
loggerOptions := options .
36
38
Logger ().
37
39
SetMaxDocumentLength (25 ).
38
40
SetComponentLevel (options .LogComponentCommand , options .LogLevelDebug )
39
41
42
+ // Creates options that include the logger specification
40
43
clientOptions := options .
41
44
Client ().
42
45
ApplyURI (uri ).
@@ -63,6 +66,7 @@ func standardLogging(uri string) {
63
66
}
64
67
}
65
68
69
+ // Creates a struct to model the logger
66
70
// start-customlogger-struct
67
71
type CustomLogger struct {
68
72
io.Writer
@@ -71,6 +75,7 @@ type CustomLogger struct {
71
75
72
76
// end-customlogger-struct
73
77
78
+ // Creates functions to specify how the logger generates messages
74
79
// start-customlogger-funcs
75
80
func (logger * CustomLogger ) Info (level int , msg string , _ ... interface {}) {
76
81
logger .mu .Lock ()
@@ -91,6 +96,7 @@ func (logger *CustomLogger) Error(err error, msg string, _ ...interface{}) {
91
96
// end-customlogger-funcs
92
97
93
98
func customLogging (uri string ) {
99
+ // Sets options when configuring the custom logger
94
100
// start-set-customlogger
95
101
buf := bytes .NewBuffer (nil )
96
102
sink := & CustomLogger {Writer : buf }
@@ -101,11 +107,12 @@ func customLogging(uri string) {
101
107
SetComponentLevel (options .LogComponentCommand , options .LogLevelDebug ).
102
108
SetComponentLevel (options .LogComponentConnection , options .LogLevelDebug )
103
109
110
+ // Creates options that include the logger specification
104
111
clientOptions := options .
105
112
Client ().
106
113
ApplyURI (uri ).
107
114
SetLoggerOptions (loggerOptions )
108
- // end-set-customlogger
115
+ // end-set-customlogger
109
116
client , err := mongo .Connect (context .TODO (), clientOptions )
110
117
if err != nil {
111
118
panic (err )
@@ -126,6 +133,7 @@ func customLogging(uri string) {
126
133
}
127
134
128
135
func thirdPartyLogging (uri string ) {
136
+ // Creates a logrus logger
129
137
// start-make-logrus
130
138
myLogger := & logrus.Logger {
131
139
Out : os .Stderr ,
@@ -137,19 +145,22 @@ func thirdPartyLogging(uri string) {
137
145
}
138
146
// end-make-logrus
139
147
148
+ // Sets the sink for the logger
140
149
// start-set-thirdparty-logger
141
150
sink := logrusr .New (myLogger ).GetSink ()
142
151
152
+ // Sets options when configuring the logrus logger
143
153
loggerOptions := options .
144
154
Logger ().
145
155
SetSink (sink ).
146
156
SetComponentLevel (options .LogComponentCommand , options .LogLevelDebug )
147
157
158
+ // Creates options that include the logger specification
148
159
clientOptions := options .
149
160
Client ().
150
161
ApplyURI (uri ).
151
162
SetLoggerOptions (loggerOptions )
152
- // end-set-thirdparty-logger
163
+ // end-set-thirdparty-logger
153
164
client , err := mongo .Connect (context .TODO (), clientOptions )
154
165
if err != nil {
155
166
panic (err )
0 commit comments