summaryrefslogtreecommitdiff
path: root/src/mongo/logv2
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/logv2')
-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
3 files changed, 72 insertions, 0 deletions
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
*