logging: add a LogHandler::getConfig() method
[folly.git] / folly / experimental / logging / Init.cpp
1 /*
2  * Copyright 2004-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <folly/experimental/logging/Init.h>
17
18 #include <folly/experimental/logging/AsyncFileWriter.h>
19 #include <folly/experimental/logging/GlogStyleFormatter.h>
20 #include <folly/experimental/logging/ImmediateFileWriter.h>
21 #include <folly/experimental/logging/LogCategory.h>
22 #include <folly/experimental/logging/LogHandlerConfig.h>
23 #include <folly/experimental/logging/LoggerDB.h>
24 #include <folly/experimental/logging/StandardLogHandler.h>
25
26 using std::shared_ptr;
27 using std::string;
28 using std::vector;
29
30 namespace folly {
31
32 void initLogLevels(StringPiece configString, LogLevel defaultRootLevel) {
33   // Set the default root category log level first
34   LoggerDB::get()->getCategory(".")->setLevel(defaultRootLevel);
35
36   // Then apply the configuration string
37   if (!configString.empty()) {
38     auto ret = LoggerDB::get()->processConfigString(configString);
39     if (!ret.empty()) {
40       throw LoggingConfigError(ret);
41     }
42   }
43 }
44
45 void initLoggingGlogStyle(
46     StringPiece configString,
47     LogLevel defaultRootLevel,
48     bool asyncWrites) {
49   // Configure log levels
50   initLogLevels(configString, defaultRootLevel);
51
52   // Create the LogHandler
53   std::shared_ptr<LogWriter> writer;
54   folly::File file{STDERR_FILENO, false};
55   LogHandlerConfig handlerConfig{"file", {{"stream", "stderr"}}};
56   if (asyncWrites) {
57     writer = std::make_shared<AsyncFileWriter>(std::move(file));
58     handlerConfig.options.emplace("async", "true");
59   } else {
60     writer = std::make_shared<ImmediateFileWriter>(std::move(file));
61     handlerConfig.options.emplace("async", "false");
62   }
63   auto handler = std::make_shared<StandardLogHandler>(
64       handlerConfig, std::make_shared<GlogStyleFormatter>(), std::move(writer));
65
66   // Add the handler to the root category.
67   LoggerDB::get()->getCategory(".")->addHandler(std::move(handler));
68 }
69
70 LoggingConfigError::LoggingConfigError(const vector<string>& errors)
71     : invalid_argument{computeMessage(errors)} {}
72
73 std::string LoggingConfigError::computeMessage(const vector<string>& errors) {
74   string msg = "error parsing logging configuration:";
75   for (const auto& error : errors) {
76     msg += "\n" + error;
77   }
78   return msg;
79 }
80 } // namespace folly