summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHenrik Edin <henrik.edin@mongodb.com>2019-10-08 21:53:47 +0000
committerevergreen <evergreen@mongodb.com>2019-10-08 21:53:47 +0000
commit40fb24a73ce0c8c1092dda3f2631648990f4587a (patch)
treede79a829b7b5147a793d122256e956c6bf112ae8
parent2dc8d02b22117c2503385d8bdd434ac5cb7f7f86 (diff)
downloadmongo-40fb24a73ce0c8c1092dda3f2631648990f4587a.tar.gz
SERVER-43744 Add logFormat configure option (logv2 only)
-rw-r--r--src/mongo/db/initialize_server_global_state.cpp3
-rw-r--r--src/mongo/db/server_options.h4
-rw-r--r--src/mongo/db/server_options_base.idl6
-rw-r--r--src/mongo/db/server_options_helpers.cpp18
-rw-r--r--src/mongo/logv2/log_format.h36
-rw-r--r--src/mongo/logv2/log_manager.cpp32
-rw-r--r--src/mongo/logv2/log_manager.h4
7 files changed, 101 insertions, 2 deletions
diff --git a/src/mongo/db/initialize_server_global_state.cpp b/src/mongo/db/initialize_server_global_state.cpp
index 3ea79435897..1e703ba3618 100644
--- a/src/mongo/db/initialize_server_global_state.cpp
+++ b/src/mongo/db/initialize_server_global_state.cpp
@@ -355,6 +355,9 @@ MONGO_INITIALIZER_GENERAL(ServerLogRedirection,
logger::globalLogDomain()->attachAppender(
std::make_unique<RamLogAppender>(RamLog::get("global")));
+ if (serverGlobalParams.logV2)
+ lv2Manager.setOutputFormat(serverGlobalParams.logFormat);
+
return Status::OK();
}
diff --git a/src/mongo/db/server_options.h b/src/mongo/db/server_options.h
index ed333a8ed13..f6d060837a3 100644
--- a/src/mongo/db/server_options.h
+++ b/src/mongo/db/server_options.h
@@ -30,6 +30,7 @@
#pragma once
#include "mongo/db/jsobj.h"
+#include "mongo/logv2/log_format.h"
#include "mongo/platform/atomic_word.h"
#include "mongo/platform/process_id.h"
#include "mongo/stdx/variant.h"
@@ -96,7 +97,8 @@ struct ServerGlobalParams {
std::string pidFile; // Path to pid file, or empty if none.
std::string timeZoneInfoPath; // Path to time zone info directory, or empty if none.
- std::string logpath; // Path to log file, if logging to a file; otherwise, empty.
+ std::string logpath; // Path to log file, if logging to a file; otherwise, empty.
+ logv2::LogFormat logFormat = logv2::LogFormat::kDefault; // Log format to output to
bool logAppend = false; // True if logging to a file in append mode.
bool logRenameOnRotate = true; // True if logging should rename log files on rotate
bool logWithSyslog = false; // True if logging to syslog; must not be set if logpath is set.
diff --git a/src/mongo/db/server_options_base.idl b/src/mongo/db/server_options_base.idl
index bbfafc21c9e..db78d3a725b 100644
--- a/src/mongo/db/server_options_base.idl
+++ b/src/mongo/db/server_options_base.idl
@@ -132,7 +132,11 @@ configs:
arg_vartype: String
condition:
preprocessor: '!defined(_WIN32)'
-
+ 'systemLog.logFormat':
+ description: 'Set the log output format (default|text|json)'
+ short_name: logFormat
+ arg_vartype: String
+ default: "default"
'systemLog.logAppend':
description: 'Append to logpath instead of over-writing'
short_name: logappend
diff --git a/src/mongo/db/server_options_helpers.cpp b/src/mongo/db/server_options_helpers.cpp
index 25b0db6bfac..17914692b35 100644
--- a/src/mongo/db/server_options_helpers.cpp
+++ b/src/mongo/db/server_options_helpers.cpp
@@ -378,6 +378,24 @@ Status storeBaseOptions(const moe::Environment& params) {
}
#endif // _WIN32
+ if (params.count("systemLog.logFormat")) {
+ std::string formatStr = params["systemLog.logFormat"].as<string>();
+ if (!serverGlobalParams.logV2 && formatStr != "default")
+ return Status(ErrorCodes::BadValue,
+ "Can only use systemLog.logFormat if logv2 is enabled.");
+ if (formatStr == "default") {
+ serverGlobalParams.logFormat = logv2::LogFormat::kDefault;
+ } else if (formatStr == "text") {
+ serverGlobalParams.logFormat = logv2::LogFormat::kText;
+ } else if (formatStr == "json") {
+ serverGlobalParams.logFormat = logv2::LogFormat::kJson;
+ } else {
+ return Status(ErrorCodes::BadValue,
+ "Unsupported value for logFormat: " + formatStr +
+ ". Valid values are: default, text or json");
+ }
+ }
+
if (params.count("systemLog.logAppend") && params["systemLog.logAppend"].as<bool>() == true) {
serverGlobalParams.logAppend = true;
}
diff --git a/src/mongo/logv2/log_format.h b/src/mongo/logv2/log_format.h
new file mode 100644
index 00000000000..cfd575af6ef
--- /dev/null
+++ b/src/mongo/logv2/log_format.h
@@ -0,0 +1,36 @@
+/**
+ * Copyright (C) 2019-present MongoDB, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the Server Side Public License, version 1,
+ * as published by MongoDB, Inc.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Server Side Public License for more details.
+ *
+ * You should have received a copy of the Server Side Public License
+ * along with this program. If not, see
+ * <http://www.mongodb.com/licensing/server-side-public-license>.
+ *
+ * As a special exception, the copyright holders give permission to link the
+ * code of portions of this program with the OpenSSL library under certain
+ * conditions as described in each individual source file and distribute
+ * linked combinations including the program with the OpenSSL library. You
+ * must comply with the Server Side Public License in all respects for
+ * all of the code used other than as permitted herein. If you modify file(s)
+ * with this exception, you may extend this exception to your version of the
+ * file(s), but you are not obligated to do so. If you do not wish to do so,
+ * delete this exception statement from your version. If you delete this
+ * exception statement from all source files in the program, then also delete
+ * it in the license file.
+ */
+
+#pragma once
+
+namespace mongo {
+namespace logv2 {
+enum class LogFormat { kDefault, kText, kJson };
+} // namespace logv2
+} // namespace mongo
diff --git a/src/mongo/logv2/log_manager.cpp b/src/mongo/logv2/log_manager.cpp
index 420665fa62f..b7887864867 100644
--- a/src/mongo/logv2/log_manager.cpp
+++ b/src/mongo/logv2/log_manager.cpp
@@ -145,6 +145,19 @@ struct LogManager::Impl {
_rotatableFileBackend->set_formatter(TextFormatter());
}
+ template <class Formatter>
+ void setFormatterToAllBackends() {
+ _consoleBackend->set_formatter(Formatter());
+ _globalLogCacheBackend->set_formatter(Formatter());
+ _startupWarningsBackend->set_formatter(Formatter());
+ if (_rotatableFileBackend)
+ _rotatableFileBackend->set_formatter(Formatter());
+#ifndef _WIN32
+ if (_syslogBackend)
+ _syslogBackend->set_formatter(Formatter());
+#endif
+ }
+
LogDomain _globalDomain{std::make_unique<LogDomainGlobal>()};
// I think that, technically, these are logging front ends
// and that they get to hold or wrap a backend
@@ -155,6 +168,7 @@ struct LogManager::Impl {
#endif
boost::shared_ptr<RamLogBackend> _globalLogCacheBackend;
boost::shared_ptr<RamLogBackend> _startupWarningsBackend;
+ LogFormat _format{LogFormat::kDefault};
bool _defaultBackendsAttached{false};
};
@@ -182,6 +196,24 @@ LogDomain& LogManager::getGlobalDomain() {
return _impl->_globalDomain;
}
+void LogManager::setOutputFormat(LogFormat format) {
+ if (_impl->_format != format) {
+ switch (format) {
+ case LogFormat::kText:
+ _impl->setFormatterToAllBackends<TextFormatter>();
+ break;
+
+ case LogFormat::kJson:
+ _impl->setFormatterToAllBackends<JsonFormatter>();
+ break;
+
+ default:
+ break;
+ };
+ _impl->_format = format;
+ }
+}
+
void LogManager::detachDefaultBackends() {
invariant(isDefaultBackendsAttached());
diff --git a/src/mongo/logv2/log_manager.h b/src/mongo/logv2/log_manager.h
index 69df533ab9a..7e8f1499154 100644
--- a/src/mongo/logv2/log_manager.h
+++ b/src/mongo/logv2/log_manager.h
@@ -29,6 +29,8 @@
#pragma once
+#include "mongo/logv2/log_format.h"
+
#include <memory>
#include <string>
@@ -58,6 +60,8 @@ public:
*/
LogDomain& getGlobalDomain();
+ void setOutputFormat(LogFormat format);
+
/**
* Detaches the default log backends
*