@@ -91,19 +91,31 @@ on a per-component level, for each component defined below in the `Components`_
91
91
92
92
The default is to not log anything.
93
93
94
- If a variable is set to an invalid value, it MUST be treated as if it were not specified at all.
94
+ If a variable is set to an invalid value, it MUST be treated as if it were not specified at all,
95
+ and the driver MAY attempt to warn the user about the misconfiguration via a log message or otherwise
96
+ but MUST NOT throw an exception.
95
97
96
98
Configurable Log Destination
97
99
----------------------------
98
- Drivers MUST support configuring where log messages should be output, including the options: stdout,
99
- stderr, output file (path MUST be configurable).
100
+ Drivers MUST support configuring where log messages should be output, including the options:
101
+
102
+ * stdout
103
+ * stderr
104
+ * Output file (path MUST be configurable). For languages that are not relying on a logging interface or framework to handle
105
+ file support, the driver can choose to either support this directly (i.e. the driver allows the user to specify
106
+ a path and itself handles writing to that path), or to instead provide a straightforward, idiomatic way to programmatically
107
+ consume the messages and in turn write them to a file, e.g. via a Node.js `stream <https://nodejs.org/api/stream.html >`__, along with a
108
+ documentation example of how to do this.
100
109
101
110
**Fallback implementation method **: If the environment variable ``MONGODB_LOG_PATH `` is provided:
102
111
- If the value is "stdout" (case-insensitive), log to stdout.
103
112
- If the value is "stderr" (case-insensitive), log to stderr.
104
- - Else, log to a file at the specified path. If the file already exists, it MUST be appended to.
113
+ - Else, if direct logging to files is supported, log to a file at the specified path. If the file already exists, it MUST be appended to.
105
114
106
- If the variable is not provided, the driver MUST log to stderr.
115
+ If the variable is not provided or is set to an invalid value (which could be invalid for any
116
+ reason, e.g. the path does not exist or is not writeable), the driver MUST log to stderr and
117
+ the driver MAY attempt to warn the user about the misconfiguration via a log message or otherwise
118
+ but MUST NOT throw an exception.
107
119
108
120
Configurable Max Document Length
109
121
--------------------------------
@@ -125,8 +137,9 @@ truncation occurred. The ellipsis MUST NOT count toward the max length.
125
137
default max length MUST be truncated to that length.
126
138
When set to an integer value, any extended JSON document longer than that value MUST be
127
139
truncated to that length.
128
- If the variable is set to an invalid value, it MUST be treated as if it were not specified at
129
- all.
140
+ If a variable is set to an invalid value, it MUST be treated as if it were not specified at all,
141
+ and the driver MAY attempt to warn the user about the misconfiguration via a log message or otherwise
142
+ but MUST NOT throw an exception.
130
143
131
144
Components
132
145
----------
@@ -315,6 +328,14 @@ Including Timestamps in Log Messages
315
328
------------------------------------
316
329
Drivers MAY add timestamps to their log messages if one will not be added automatically by the logging framework(s) they use.
317
330
331
+ Supporting Both Programmatic and Environment Variable Configuration
332
+ -------------------------------------------------------------------
333
+ If a driver supports configuration via both environment variables and programmatically via API,
334
+ programmatic configuration MUST take precedence over environment variables.
335
+ Drivers supporting both forms of configuration MUST document this behavior and MUST provide an
336
+ example of how users can implement custom logic to allow an environment variable to override
337
+ a programmatic default, so that users who prefer the opposite behavior have a way to achieve it.
338
+
318
339
Test Plan
319
340
=========
320
341
Tests for logging behavior are defined in each corresponding specification. The `unified test
@@ -392,6 +413,47 @@ Additionally, always including null values would increase the size of log messag
392
413
null values is left to the drivers' discretion for any driver-specific logs not covered by common
393
414
specification components.
394
415
416
+ ---------------------------------------
417
+ Invalid Values of Environment Variables
418
+ ---------------------------------------
419
+ For drivers supporting configuration via environment variables, the spec requires that if an environment
420
+ variable is set to an invalid value the driver behaves as if the value were not specified at all, and
421
+ optionally warns the user but does not throw an error. We considered the following alternatives:
422
+
423
+ 1. Drivers could be required to throw an exception if a value is invalid: This was rejected because of concerns
424
+ around the implications for environments/applications where multiple versions of the driver or multiple
425
+ drivers may be present and where the validation logic may not match, meaning a value considered valid for
426
+ one driver/version might not be by another. Additionally, there is no obvious place to throw an exception
427
+ from about invalid environment variables; ``MongoClient `` constructors would be one possibility, but not all
428
+ languages will support per-client configuration so throwing there regarding an environment variable might be
429
+ surprising to users.
430
+
431
+ Note that these same concerns do not apply to logging options that are specified via driver API: there is no
432
+ risk of such options propagating to other drivers/driver versions present, and drivers can report exceptions at
433
+ the point the options are specified, either globally or per-client. Therefore, drivers MUST validate
434
+ programmatic logging options in a manner consistent with how they validate all other programmatic
435
+ options, and if possible SHOULD prefer to throw exceptions for invalid configuration.
436
+
437
+ 1. Drivers could be required to log a warning if a value is invalid: While drivers MAY do this, requiring
438
+ it was rejected because depending on the language/framework log messages may not be a viable way to
439
+ communicate a warning: if a language's default behavior is to log nothing, or only log messages at a
440
+ more severe level than ``warn ``, the user will not actually receive the message unless it is logged at a
441
+ level and component they have successfully enabled.
442
+
443
+ --------------------------------------------
444
+ Programmatic Configuration Taking Precedence
445
+ --------------------------------------------
446
+ We chose to have programmatic configuration win out over environment variables because:
447
+
448
+ 1. This allows applications built atop drivers (e.g. mongosh) to fully control the driver's logging behavior
449
+ by setting options for it programmatically.
450
+ 2. This is consistent with how many drivers treat options specified both in a connection string and programmatically:
451
+ programmatic options win out.
452
+ 3. It is straightforward for users to override this behavior (by writing logic to read in environment variables and override
453
+ programmatic defaults), but if we went with the opposite default, it would be more complicated for users to override:
454
+ not all languages will necessarily have an easy way to override/unset an environment variable from within
455
+ application code.
456
+
395
457
Backwards Compatibility
396
458
=======================
397
459
This specification takes the stance that the contents of log messages (both structured and
@@ -444,3 +506,8 @@ Changelog
444
506
:2022-10-26: Allow drivers to add timestamps to log messages.
445
507
:2022-11-10: Clarify driver-specific null omission.
446
508
:2022-12-29: Fix typo in trace log level example
509
+ :2023-01-04: Elaborate on treatment of invalid values of environment variables.
510
+ Permit drivers to omit direct support for logging to file so long as they provide
511
+ a straightforward way for users to consume the log messages programmatically and
512
+ write to a file themselves.
513
+ Require that programmatic configuration take precedence over environment variables.
0 commit comments