proxygen
proxygen/folly/folly/logging/docs/Config.md
Go to the documentation of this file.
1 Logging Configuration
2 =====================
3 
4 Overview
5 --------
6 
7 The logging library is normally configured using configuration strings.
8 
9 In its most basic format, the configuration string consists of a comma
10 separated list of `CATEGORY=LEVEL` pairs, e.g.:
11 
12 ```
13 folly=INFO,folly.io.async=DBG2
14 ```
15 
16 A log level name can also be specified by itself to affect the root log
17 category:
18 
19 ```
20 WARN
21 ```
22 
23 These are the two forms that users will probably use most often for customizing
24 log levels via command line arguments. Additional settings, including log
25 handler settings, can also be included. The syntax is documented more
26 completely in the [Basic Configuration Syntax](#basic-configuration-syntax)
27 section.
28 
29 Log configuration can also be specified using JSON as well. If the log
30 configuration string starts with a leading `{` character (optionally after
31 leading whitespace), it is parsed as a JSON object. The JSON configuration
32 format is documented in the
33 [JSON Configuration Syntax](#json-configuration-syntax) section.
34 
35 In general the basic configuration syntax is convenient for controlling log
36 levels, and making minor log handler setting changes (such as controlling if
37 logging goes to stdout or stderr, and whether it is logged asynchronously or
38 not). However the JSON format is easier to use to describe more complicated
39 settings.
40 
41 
42 Basic Configuration Syntax
43 --------------------------
44 
45 The basic configuration format is parsed using `parseLogConfig()`.
46 
47 The basic format string is separated with semicolons. Everything up to the
48 first semicolon specifies LogCategory configurations. Each remaining
49 semicolon-separated section defines a LogHandler configuration.
50 
51 To keep the basic format simple, it does not support any form of character
52 escape sequences. If you need to define a log category whose name includes a
53 special character like a comma or semicolon use the JSON format instead.
54 
55 ### Grammar Overview
56 
57 ```
58 <config> ::= <category_configs> <handler_configs>
59 <category_configs> ::= <category_config>
60  | <category_config> "," <category_configs>
61  | <empty_string>
62 <handler_configs> ::= ";" <handler_config>
63  | ";" <handler_config> <handler_configs>
64  | <empty_string>
65 
66 <category_config> ::= <cat_level_config> <handler_list>
67 <cat_level_config> ::= <level>
68  | <catgory_name> "=" <level>
69  | <catgory_name> ":=" <level>
70 <handler_list> ::= ":" <handler_name> <handler_list>
71  | <empty_string>
72 
73 <handler_config> ::= <handler_name> "=" <handler_type> ":" <handler_options>
74  | <handler_name> ":" <handler_options>
75 <handler_options> ::= "," <option_name> "=" <option_value> <handler_options>
76  | <empty_string>
77 
78 <catgory_name> ::= <atom>
79 <handler_name> ::= <atom>
80 <handler_type> ::= <atom>
81 <option_name> ::= <atom>
82 <option_value> ::= <atom>
83 <atom> ::= any sequence of characters except ";", ",", "=", or ":",
84  with leading and trailing whitespace ignored
85 
86 <level> ::= <log_level_string>
87  | <positive_integer>
88 <log_level_string> ::= any one of the strings accepted by logLevelToString()
89 ```
90 
91 ### Log Category Configuration
92 
93 The log category configurations are a comma-separated list. Each element in
94 this list has the form
95 
96 ```
97 NAME=LEVEL:HANDLER1:HANDLER2
98 ```
99 
100 The log category name and '=' sign can be omitted, in which case the setting
101 applies to the root log category. The root log category can also be
102 explicitly named either using the empty string or the name ".".
103 
104 The NAME and LEVEL can also be separated with ":=" instead of "=",
105 which disables log level inheritance for this category. This forces
106 category's effective log level to be the exact level specified, even if its
107 parent category has a more verbose level setting.
108 
109 The log handler settings for a log category can be omitted, in which case
110 the existing log handlers for this category will be left unchanged when
111 updating the LoggerDB settings. Specifying an empty log handler list (a
112 trailing ':' with no log handlers following) will cause the log handler list
113 for this category to be cleared instead.
114 
115 ### Log Handler Configuration
116 
117 Each log handler configuration section takes the form
118 
119 ```
120 NAME=TYPE:OPTION1=VALUE1,OPTION2=VALUE2
121 ```
122 
123 NAME specifies the log handler name, and TYPE specifies the log handler
124 type. A comma separated list of name=value options may follow the log
125 handler name and type. The option list will be passed to the
126 LogHandlerFactory for the specified handler type.
127 
128 The log handler type may be omitted to update the settings of an existing log
129 handler object:
130 
131 ```
132 NAME:OPTION1=VALUE1
133 ```
134 
135 A log handler with this name must already exist. Options specified in the
136 configuration will be updated with their new values, and any option names not
137 mentioned will be left unchanged.
138 
139 
140 ### Examples
141 
142 Example log configuration strings:
143 
144 * `ERROR`
145 
146  Sets the root log category level to ERR. (Note that `ERROR` is allowed in
147  configuration strings as an alias for the `LogLevel::ERR` value.)
148 
149 * `folly=INFO,folly.io=DBG2`
150 
151  Sets the "folly" log category level to INFO, and the "folly.io" log
152  category level to DBG2.
153 
154 * `folly=DBG2,folly.io:=INFO`
155 
156  Sets the "folly" log category level to DBG2, and the "folly.io" log
157  category level to INFO, and prevent it from inheriting its effective log
158  level from its parent category. DBG2 log messages sent to "folly.io" will
159  therefore be discarded, even though they are enabled for one of its parent
160  categories.
161 
162 * `ERROR:stderr, folly=INFO; stderr=stream:stream=stderr`
163 
164  Sets the root log category level to ERROR, and sets its handler list to
165  use the "stderr" handler. Sets the folly log level to INFO. Defines
166  a log handler named "stderr" which writes to stderr.
167 
168 * `ERROR:x,folly=INFO:y;x=stream:stream=stderr;y=file:path=/tmp/y.log`
169 
170  Defines two log handlers: "x" which writes to stderr and "y" which
171  writes to the file /tmp/y.log
172  Sets the root log catgory level to ERROR, and configures it to use the
173  "x" handler. Sets the log level for the "folly" category to INFO and
174  configures it to use the "y" handler.
175 
176 * `ERROR:default:x; default=stream:stream=stderr; x=file:path=/tmp/x.log`
177 
178  Defines two log handlers: "default" which writes to stderr and "x" which
179  writes to the file /tmp/x.log
180  Sets the root log catgory level to ERROR, and configures it to use both
181  the "default" and "x" handlers.
182 
183 * `ERROR:`
184 
185  Sets the root log category level to ERR, and removes any log handlers
186  configured for it. Explicitly specifying an empty list of handlers (with
187  a ':' followed by no handlers) will update the handlers for this category
188  to the empty list. Not specifying handler information at all (no ':')
189  will leave any pre-existing handlers as-is.
190 
191 * `;default=stream:stream=stdout`
192 
193  Does not change any log category settings, and defines a "default" handler
194  that writes to stdout. This format is useful to update log handler settings
195  if the "default" handler already exists and is attached to existing log
196  categories.
197 
198 * `ERROR; stderr:async=true`
199 
200  Sets the root log category level to ERR, and sets the "async" property to
201  true on the "stderr" handler. A log handler named "stderr" must already
202  exist. Therefore this configuration string is only valid to use with
203  `LoggerDB::updateConfig()`, and cannot be used with
204  `LoggerDB::resetConfig()`.
205 
206 * `INFO; default:async=true,sync_level=WARN`
207 
208  Sets the root log category level to INFO, and sets the "async" property to
209  true and "sync_level" property to WARN. Setting "async" property ensures that
210  we enable asynchronous logging but the "sync_level" flag specifies that all
211  logs of the level WARN and above are processed synchronously. This can help
212  ensure that all logs of the level WARN or above are persisted before a
213  potential crash while ensuring that all logs below the level WARN are
214  non-blocking.
215 
216 JSON Configuration Syntax
217 -------------------------
218 
219 The `parseLogConfig()` function, which parses the basic configuration string
220 syntax, will also accept a JSON object string as input. However, you can also
221 use `parseLogConfigJson()` to explicitly parse the input as JSON, and not
222 accept the basic configuration string syntax.
223 
224 The input string is parsed using relaxed JSON parsing, allowing C and C++ style
225 comments, as well as trailing commas.
226 
227 The JSON configuration string must be a JSON object data type, with two
228 optional members: `categories` and `handlers`. Any additional members besides
229 these two are ignored.
230 
231 ### Log Category Configuration
232 
233 If present, the `categories` member of the top-level object should be a JSON
234 object mapping log category names to configuration settings for that log
235 category.
236 
237 The value of each element in `categories` should also be a JSON object with the
238 following fields:
239 
240 * `level`
241 
242  This field is required. It should be a string or positive integer value
243  specifying the log level for this category.
244 
245 * `inherit`
246 
247  This should be a boolean value indicating if this category should inherit its
248  effective log level from its parent category if its parent has a more verbose
249  log level setting.
250 
251  This field is optional, and defaults to true if not present.
252 
253 Alternatively, the value for a log category may be a plain string or integer
254 instead of a JSON object, in which case case the string or integer is treated
255 as the log level for that category, with the inherit setting enabled.
256 
257 ### Log Handler Configuration
258 
259 If present, the `handlers` member of the top-level object should be a JSON
260 object mapping log handler names to configuration settings for that log
261 handler.
262 
263 The value of each element in `handlers` should also be a JSON object with the
264 following fields:
265 
266 * `type`
267 
268  This field should be a string containing the name of the log handler type.
269  This type name must correspond to `LogHandlerFactory` type registered with
270  the `LoggerDB`.
271 
272  If this field is not present then this configuration will be used to update
273  an existing log handler. A log handler with this name must already exist.
274  The values from the `options` field will be merged into the existing log
275  handler options.
276 
277 * `options`
278 
279  This field is optional. If present, it should be a JSON object containing
280  string-to-string mappings to be passed to the `LogHandlerFactory` for
281  constructing this log handler.
282 
283 ### Example
284 
285 ```javascript
286 {
287  "categories": {
288  "foo": { "level": "INFO", "handlers": ["stderr"] },
289  "foo.only_fatal": { "level": "FATAL", "inherit": false }
290  }
291  "handlers": {
292  "stderr": {
293  "type": "stream",
294  "options": {
295  "stream": "stderr",
296  "async": true,
297  "sync_level": "WARN",
298  "max_buffer_size": 4096000
299  }
300  }
301  }
302 }
303 ```
304 
305 
306 Custom Configuration Mechanisms
307 -------------------------------
308 
309 Internally the the `LogConfig` class represents configuration settings for the
310 folly logging library. Users of the logging library can also programmatically
311 construct their own `LogConfig` objects and use the `LoggerDB::updateConfig()`
312 and `LoggerDB::resetConfig()` APIs to apply the configuration changes.
313 
314 You can also directly manipulate the log level and other settings on
315 `LogCategory` objects.
316 
317 While it is possible to also manually create new `LogHandler` objects, it is
318 generally preferred to do this using the `LoggerDB::updateConfig()` and
319 `LoggerDB::resetConfig()` APIs. If you manually create a new `LogHandler` and
320 directly attach it to some categories the `LoggerDB::getConfig()` call will not
321 be able to return complete information for your manually created log handler,
322 since it does not have a name or handler type that can be included in the
323 configuration.