1 // Copyright (C) MongoDB, Inc. 2023-present. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may 4 // not use this file except in compliance with the License. You may obtain 5 // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 6 7 package options 8 9 import ( 10 "go.mongodb.org/mongo-driver/internal/logger" 11 ) 12 13 // LogLevel is an enumeration representing the supported log severity levels. 14 type LogLevel int 15 16 const ( 17 // LogLevelInfo enables logging of informational messages. These logs 18 // are high-level information about normal driver behavior. 19 LogLevelInfo LogLevel = LogLevel(logger.LevelInfo) 20 21 // LogLevelDebug enables logging of debug messages. These logs can be 22 // voluminous and are intended for detailed information that may be 23 // helpful when debugging an application. 24 LogLevelDebug LogLevel = LogLevel(logger.LevelDebug) 25 ) 26 27 // LogComponent is an enumeration representing the "components" which can be 28 // logged against. A LogLevel can be configured on a per-component basis. 29 type LogComponent int 30 31 const ( 32 // LogComponentAll enables logging for all components. 33 LogComponentAll LogComponent = LogComponent(logger.ComponentAll) 34 35 // LogComponentCommand enables command monitor logging. 36 LogComponentCommand LogComponent = LogComponent(logger.ComponentCommand) 37 38 // LogComponentTopology enables topology logging. 39 LogComponentTopology LogComponent = LogComponent(logger.ComponentTopology) 40 41 // LogComponentServerSelection enables server selection logging. 42 LogComponentServerSelection LogComponent = LogComponent(logger.ComponentServerSelection) 43 44 // LogComponentConnection enables connection services logging. 45 LogComponentConnection LogComponent = LogComponent(logger.ComponentConnection) 46 ) 47 48 // LogSink is an interface that can be implemented to provide a custom sink for 49 // the driver's logs. 50 type LogSink interface { 51 // Info logs a non-error message with the given key/value pairs. This 52 // method will only be called if the provided level has been defined 53 // for a component in the LoggerOptions. 54 // 55 // Here are the following level mappings for V = "Verbosity": 56 // 57 // - V(0): off 58 // - V(1): informational 59 // - V(2): debugging 60 // 61 // This level mapping is taken from the go-logr/logr library 62 // specifications, specifically: 63 // 64 // "Level V(0) is the default, and logger.V(0).Info() has the same 65 // meaning as logger.Info()." 66 Info(level int, message string, keysAndValues ...interface{}) 67 68 // Error logs an error message with the given key/value pairs 69 Error(err error, message string, keysAndValues ...interface{}) 70 } 71 72 // LoggerOptions represent options used to configure Logging in the Go Driver. 73 type LoggerOptions struct { 74 // ComponentLevels is a map of LogComponent to LogLevel. The LogLevel 75 // for a given LogComponent will be used to determine if a log message 76 // should be logged. 77 ComponentLevels map[LogComponent]LogLevel 78 79 // Sink is the LogSink that will be used to log messages. If this is 80 // nil, the driver will use the standard logging library. 81 Sink LogSink 82 83 // MaxDocumentLength is the maximum length of a document to be logged. 84 // If the underlying document is larger than this value, it will be 85 // truncated and appended with an ellipses "...". 86 MaxDocumentLength uint 87 } 88 89 // Logger creates a new LoggerOptions instance. 90 func Logger() *LoggerOptions { 91 return &LoggerOptions{ 92 ComponentLevels: map[LogComponent]LogLevel{}, 93 } 94 } 95 96 // SetComponentLevel sets the LogLevel value for a LogComponent. 97 func (opts *LoggerOptions) SetComponentLevel(component LogComponent, level LogLevel) *LoggerOptions { 98 opts.ComponentLevels[component] = level 99 100 return opts 101 } 102 103 // SetMaxDocumentLength sets the maximum length of a document to be logged. 104 func (opts *LoggerOptions) SetMaxDocumentLength(maxDocumentLength uint) *LoggerOptions { 105 opts.MaxDocumentLength = maxDocumentLength 106 107 return opts 108 } 109 110 // SetSink sets the LogSink to use for logging. 111 func (opts *LoggerOptions) SetSink(sink LogSink) *LoggerOptions { 112 opts.Sink = sink 113 114 return opts 115 } 116