Skip to content

Commit 40fb24a

Browse files
henrikedinevergreen
authored andcommitted
SERVER-43744 Add logFormat configure option (logv2 only)
1 parent 2dc8d02 commit 40fb24a

File tree

7 files changed

+101
-2
lines changed

7 files changed

+101
-2
lines changed

src/mongo/db/initialize_server_global_state.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,9 @@ MONGO_INITIALIZER_GENERAL(ServerLogRedirection,
355355
logger::globalLogDomain()->attachAppender(
356356
std::make_unique<RamLogAppender>(RamLog::get("global")));
357357

358+
if (serverGlobalParams.logV2)
359+
lv2Manager.setOutputFormat(serverGlobalParams.logFormat);
360+
358361
return Status::OK();
359362
}
360363

src/mongo/db/server_options.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#pragma once
3131

3232
#include "mongo/db/jsobj.h"
33+
#include "mongo/logv2/log_format.h"
3334
#include "mongo/platform/atomic_word.h"
3435
#include "mongo/platform/process_id.h"
3536
#include "mongo/stdx/variant.h"
@@ -96,7 +97,8 @@ struct ServerGlobalParams {
9697
std::string pidFile; // Path to pid file, or empty if none.
9798
std::string timeZoneInfoPath; // Path to time zone info directory, or empty if none.
9899

99-
std::string logpath; // Path to log file, if logging to a file; otherwise, empty.
100+
std::string logpath; // Path to log file, if logging to a file; otherwise, empty.
101+
logv2::LogFormat logFormat = logv2::LogFormat::kDefault; // Log format to output to
100102
bool logAppend = false; // True if logging to a file in append mode.
101103
bool logRenameOnRotate = true; // True if logging should rename log files on rotate
102104
bool logWithSyslog = false; // True if logging to syslog; must not be set if logpath is set.

src/mongo/db/server_options_base.idl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ configs:
132132
arg_vartype: String
133133
condition:
134134
preprocessor: '!defined(_WIN32)'
135-
135+
'systemLog.logFormat':
136+
description: 'Set the log output format (default|text|json)'
137+
short_name: logFormat
138+
arg_vartype: String
139+
default: "default"
136140
'systemLog.logAppend':
137141
description: 'Append to logpath instead of over-writing'
138142
short_name: logappend

src/mongo/db/server_options_helpers.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,24 @@ Status storeBaseOptions(const moe::Environment& params) {
378378
}
379379
#endif // _WIN32
380380

381+
if (params.count("systemLog.logFormat")) {
382+
std::string formatStr = params["systemLog.logFormat"].as<string>();
383+
if (!serverGlobalParams.logV2 && formatStr != "default")
384+
return Status(ErrorCodes::BadValue,
385+
"Can only use systemLog.logFormat if logv2 is enabled.");
386+
if (formatStr == "default") {
387+
serverGlobalParams.logFormat = logv2::LogFormat::kDefault;
388+
} else if (formatStr == "text") {
389+
serverGlobalParams.logFormat = logv2::LogFormat::kText;
390+
} else if (formatStr == "json") {
391+
serverGlobalParams.logFormat = logv2::LogFormat::kJson;
392+
} else {
393+
return Status(ErrorCodes::BadValue,
394+
"Unsupported value for logFormat: " + formatStr +
395+
". Valid values are: default, text or json");
396+
}
397+
}
398+
381399
if (params.count("systemLog.logAppend") && params["systemLog.logAppend"].as<bool>() == true) {
382400
serverGlobalParams.logAppend = true;
383401
}

src/mongo/logv2/log_format.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright (C) 2019-present MongoDB, Inc.
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the Server Side Public License, version 1,
6+
* as published by MongoDB, Inc.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* Server Side Public License for more details.
12+
*
13+
* You should have received a copy of the Server Side Public License
14+
* along with this program. If not, see
15+
* <http://www.mongodb.com/licensing/server-side-public-license>.
16+
*
17+
* As a special exception, the copyright holders give permission to link the
18+
* code of portions of this program with the OpenSSL library under certain
19+
* conditions as described in each individual source file and distribute
20+
* linked combinations including the program with the OpenSSL library. You
21+
* must comply with the Server Side Public License in all respects for
22+
* all of the code used other than as permitted herein. If you modify file(s)
23+
* with this exception, you may extend this exception to your version of the
24+
* file(s), but you are not obligated to do so. If you do not wish to do so,
25+
* delete this exception statement from your version. If you delete this
26+
* exception statement from all source files in the program, then also delete
27+
* it in the license file.
28+
*/
29+
30+
#pragma once
31+
32+
namespace mongo {
33+
namespace logv2 {
34+
enum class LogFormat { kDefault, kText, kJson };
35+
} // namespace logv2
36+
} // namespace mongo

src/mongo/logv2/log_manager.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,19 @@ struct LogManager::Impl {
145145
_rotatableFileBackend->set_formatter(TextFormatter());
146146
}
147147

148+
template <class Formatter>
149+
void setFormatterToAllBackends() {
150+
_consoleBackend->set_formatter(Formatter());
151+
_globalLogCacheBackend->set_formatter(Formatter());
152+
_startupWarningsBackend->set_formatter(Formatter());
153+
if (_rotatableFileBackend)
154+
_rotatableFileBackend->set_formatter(Formatter());
155+
#ifndef _WIN32
156+
if (_syslogBackend)
157+
_syslogBackend->set_formatter(Formatter());
158+
#endif
159+
}
160+
148161
LogDomain _globalDomain{std::make_unique<LogDomainGlobal>()};
149162
// I think that, technically, these are logging front ends
150163
// and that they get to hold or wrap a backend
@@ -155,6 +168,7 @@ struct LogManager::Impl {
155168
#endif
156169
boost::shared_ptr<RamLogBackend> _globalLogCacheBackend;
157170
boost::shared_ptr<RamLogBackend> _startupWarningsBackend;
171+
LogFormat _format{LogFormat::kDefault};
158172
bool _defaultBackendsAttached{false};
159173
};
160174

@@ -182,6 +196,24 @@ LogDomain& LogManager::getGlobalDomain() {
182196
return _impl->_globalDomain;
183197
}
184198

199+
void LogManager::setOutputFormat(LogFormat format) {
200+
if (_impl->_format != format) {
201+
switch (format) {
202+
case LogFormat::kText:
203+
_impl->setFormatterToAllBackends<TextFormatter>();
204+
break;
205+
206+
case LogFormat::kJson:
207+
_impl->setFormatterToAllBackends<JsonFormatter>();
208+
break;
209+
210+
default:
211+
break;
212+
};
213+
_impl->_format = format;
214+
}
215+
}
216+
185217
void LogManager::detachDefaultBackends() {
186218
invariant(isDefaultBackendsAttached());
187219

src/mongo/logv2/log_manager.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
#pragma once
3131

32+
#include "mongo/logv2/log_format.h"
33+
3234
#include <memory>
3335
#include <string>
3436

@@ -58,6 +60,8 @@ class LogManager {
5860
*/
5961
LogDomain& getGlobalDomain();
6062

63+
void setOutputFormat(LogFormat format);
64+
6165
/**
6266
* Detaches the default log backends
6367
*

0 commit comments

Comments
 (0)