1+ // Configures loggers by using the Go driver
12package main
23
34import (
@@ -31,12 +32,14 @@ func main() {
3132}
3233
3334func standardLogging (uri string ) {
35+ // Sets options when configuring the standard logger
3436 // start-standard-logger
3537 loggerOptions := options .
3638 Logger ().
3739 SetMaxDocumentLength (25 ).
3840 SetComponentLevel (options .LogComponentCommand , options .LogLevelDebug )
3941
42+ // Creates options that include the logger specification
4043 clientOptions := options .
4144 Client ().
4245 ApplyURI (uri ).
@@ -63,6 +66,7 @@ func standardLogging(uri string) {
6366 }
6467}
6568
69+ // Creates a struct to model the logger
6670// start-customlogger-struct
6771type CustomLogger struct {
6872 io.Writer
@@ -71,6 +75,7 @@ type CustomLogger struct {
7175
7276// end-customlogger-struct
7377
78+ // Creates functions to specify how the logger generates messages
7479// start-customlogger-funcs
7580func (logger * CustomLogger ) Info (level int , msg string , _ ... interface {}) {
7681 logger .mu .Lock ()
@@ -91,6 +96,7 @@ func (logger *CustomLogger) Error(err error, msg string, _ ...interface{}) {
9196// end-customlogger-funcs
9297
9398func customLogging (uri string ) {
99+ // Sets options when configuring the custom logger
94100 // start-set-customlogger
95101 buf := bytes .NewBuffer (nil )
96102 sink := & CustomLogger {Writer : buf }
@@ -101,11 +107,12 @@ func customLogging(uri string) {
101107 SetComponentLevel (options .LogComponentCommand , options .LogLevelDebug ).
102108 SetComponentLevel (options .LogComponentConnection , options .LogLevelDebug )
103109
110+ // Creates options that include the logger specification
104111 clientOptions := options .
105112 Client ().
106113 ApplyURI (uri ).
107114 SetLoggerOptions (loggerOptions )
108- // end-set-customlogger
115+ // end-set-customlogger
109116 client , err := mongo .Connect (context .TODO (), clientOptions )
110117 if err != nil {
111118 panic (err )
@@ -126,6 +133,7 @@ func customLogging(uri string) {
126133}
127134
128135func thirdPartyLogging (uri string ) {
136+ // Creates a logrus logger
129137 // start-make-logrus
130138 myLogger := & logrus.Logger {
131139 Out : os .Stderr ,
@@ -137,19 +145,22 @@ func thirdPartyLogging(uri string) {
137145 }
138146 // end-make-logrus
139147
148+ // Sets the sink for the logger
140149 // start-set-thirdparty-logger
141150 sink := logrusr .New (myLogger ).GetSink ()
142151
152+ // Sets options when configuring the logrus logger
143153 loggerOptions := options .
144154 Logger ().
145155 SetSink (sink ).
146156 SetComponentLevel (options .LogComponentCommand , options .LogLevelDebug )
147157
158+ // Creates options that include the logger specification
148159 clientOptions := options .
149160 Client ().
150161 ApplyURI (uri ).
151162 SetLoggerOptions (loggerOptions )
152- // end-set-thirdparty-logger
163+ // end-set-thirdparty-logger
153164 client , err := mongo .Connect (context .TODO (), clientOptions )
154165 if err != nil {
155166 panic (err )
0 commit comments