3 Logging messages with the folly logging library is done with one of a handful
4 of log macros. Macros are used to allow lazily evaluating the log arguments:
5 if the log message is disabled the log message expression will not be executed.
11 In most cases, if you want to log a message you will use the `XLOG()` macro:
14 XLOG(INFO) << "hello world!";
17 This macro is defined in `folly/logging/xlog.h`
21 The `XLOG()` macro automatically chooses the log category based on the current
22 file name. However, if you want to log to an explicit log category, you can
23 use `FB_LOG()`. It behaves like `XLOG()`, except that it requires a
24 `folly::Logger` as is first argument to specify the log category:
27 folly::Logger eventLogger("eden.events");
29 FB_LOG(eventLogger, INFO) << "something happened";
32 `FB_LOG()` is defined in `folly/logging/Logger.h`
36 The `XLOG()` macro takes a log level as its first argument. See the
37 [Log Levels](LogLevels.md) document for a list of available log levels.
39 If you supply additional arguments they will be converted to strings using
40 `folly::to<std::string>()` and concatenated together as part of the log
44 XLOG(INFO, "the number is ", 2 + 2);
47 will result in the message "the number is 4".
49 If desired, you can specify both function argument style and `ostream` style
50 streaming log arguments together:
53 XLOG(INFO, "the number is ") << 2 + 2);
56 The `FB_LOG()` macro accepts requires a `Logger` object as its first argument,
57 and all subsequent arguments behave the same as the arguments to `XLOG()`.
59 ## Python-style string formatting
61 The `XLOGF()` and `FB_LOGF()` macros allow log messages to be formatted using
62 format strings similar to python's
63 [str.format()](https://docs.python.org/3/library/string.html#formatspec)
67 XLOGF(DBG1, "cannot engage {} thruster: {}", thruster.name(), err.what());
70 This uses [`folly::format()`](https://github.com/facebook/folly/blob/master/folly/docs/Format.md)
71 to perform the formatting internally.
73 ## `printf`-style string formatting
75 To help existing projects convert from older logging APIs, `XLOGC()` and
76 `FB_LOGC()` macros exist to support C-style `printf()` format strings.
77 You must include `folly/logging/printf.h` to access these macros.
80 XLOGC(DBG1, "failed to engage thruster %d: %s", thruster.number(), err.what());
83 # Log Category Selection
85 The `XLOG()` macro automatically selects a log category to log to based on the
86 current source file name. Directory separators in the path are replaced with
87 `.` characters to compute the log category name.
89 For instance, in a source file named `src/tiefighter/thruster.cpp` the default
90 `XLOG()` category will be `src.tiefighter.thruster.cpp`
92 Inside `.cpp` files the default `XLOG()` category name can be overridden using
93 the `XLOG_SET_CATEGORY_NAME()` macro. `XLOG_SET_CATEGORY_NAME()` can be
94 specified at top-level scope in the `.cpp` file to specify an alternate
95 category name for all `XLOG()` statements in this file.
96 `XLOG_SET_CATEGORY_NAME()` should not be used in header files, since it would
97 end up affecting all `.cpp` files that include that header.
101 The logging library provides several APIs for configuring log categories and
102 handlers. While you can programmatically configure `LogCategory` and
103 `LogHandler` objects via their public APIs, there are also APIs to configure
104 the logging library via configuration strings.
106 `folly::parseLogConfig()` can parse a configuration string in to a `LogConfig`
107 object. The configuration string syntax is documented in
108 [Config.md](Config.md).
110 You can then apply a `LogConfig` object to the main `LoggerDB` singleton by
111 using `LoggerDB::get()->updateConfig()` to incrementally update the current
112 configuration, or by using `LoggerDB::get()->resetConfig()` to replace all
113 existing settings with the new configuration.
115 The `folly::initLogging()` function provides a convenient API for initially
116 configuring the logging library from a configuration string that was obtained
117 from a command line flag or configuration file.