You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The right part of the expression is calculated only if the logging level is less or equal to the macro log level.
24
-
I.e., the right part of the expression `LOG_DEBUG ()<< ...` is calculated only in the case of the `DEBUG`
24
+
I.e., the right part of the expression `LOG_DEBUG() << ...` is calculated only in the case of the `DEBUG`
25
25
or `TRACE` logging level.
26
26
27
27
Sometimes it is useful to set the logging level of a given log entry dynamically:
@@ -33,8 +33,8 @@ Sometimes it is useful to set the logging level of a given log entry dynamically
33
33
Not all logs get into the log file, but only those that are not lower than the logger's log level. The logger log level
34
34
is set in static config (see components::Logging).
35
35
36
-
The log level can be changed in the static config for a particular handle. In this case, the log level of the logger is changed only
37
-
for the request handling task of the handle and for all the subtasks:
36
+
The log level can be changed in the static config for a particular handle. In this case, the log level of the logger
37
+
is changed only for the request handling task of the handle and for all the subtasks:
38
38
39
39
```
40
40
yaml
@@ -44,21 +44,29 @@ yaml
44
44
log-level: WARNING
45
45
```
46
46
47
+
Logging per line and file can be overriden at runtime using @ref USERVER_LOG_DYNAMIC_DEBUG dynamic config.
48
+
47
49
48
50
### Dynamic change of the logging level
49
51
50
52
The logging `level` that was set in the static config of the components::Logging
51
53
component for the entire service can be changed on the fly.
54
+
52
55
See @ref scripts/docs/en/userver/log_level_running_service.md for more info.
53
56
54
57
### Limit log length of the requests and responses
55
58
56
-
For per-handle limiting of the request body or response data logging you can use the `request_body_size_log_limit` and `request_headers_size_log_limit` and `response_data_size_log_limit` static options of the handler (see server::handlers::HandlerBase). Or you could override the server::handlers::HttpHandlerBase::GetRequestBodyForLogging and server::handlers::HttpHandlerBase::GetResponseDataForLogging functions.
59
+
For per-handle limiting of the request body or response data logging you can use the `request_body_size_log_limit` and
60
+
`request_headers_size_log_limit` and `response_data_size_log_limit` static options of the handler
61
+
(see server::handlers::HandlerBase). Or you could override the
62
+
server::handlers::HttpHandlerBase::GetRequestBodyForLogging and
If some line of code generates too many logs and a small number of them is enough, then `LOG_*` should be
61
-
replaced with `LOG_LIMITED_*`. For example, `LOG(lvl)` should be replaced with `LOG_LIMITED(lvl)`; `LOG_DEBUG()` should be replaced with `LOG_LIMITED_DEBUG()`, etc.
68
+
replaced with `LOG_LIMITED_*`. For example, `LOG(lvl)` should be replaced with `LOG_LIMITED(lvl)`; `LOG_DEBUG()` should
69
+
be replaced with `LOG_LIMITED_DEBUG()`, etc.
62
70
63
71
In this case, the log message is written only if the message index is a power of two. The counter is reset every second.
64
72
@@ -99,7 +107,7 @@ to use `tracing::Span`, which implicitly adds tags to the log.
99
107
### Stacktrace
100
108
101
109
Sometimes it is useful to write a full stacktrace to the log. Typical use case is for logging a "never should happen happened"
102
-
situation. Use `logging::LogExtra::Stacktrace()` for such cases:
110
+
situation. Use logging::LogExtra::Stacktrace() for such cases:
103
111
104
112
@snippet logging/log_extra_test.cpp Example using stacktrace in log
105
113
@@ -128,23 +136,23 @@ LOG_INFO_TO(*my_logger) << "Look, I am a new logger!";
128
136
129
137
Note: do not forget to configure the logrotate for your new log file!
130
138
139
+
131
140
## Tracing
132
141
The userver implements a request tracing mechanism that is compatible with the
thereby building a trace of requests and interactions.
138
-
It can be used to identify slow query stages, bottlenecks,
144
+
It allows you to save dependencies between tasks, between requests through several services,
145
+
thereby building a trace of requests and interactions. It can be used to identify slow query stages, bottlenecks,
139
146
sequential queries, etc.
140
147
141
148
See tracing::DefaultTracingManagerLocator for more info.
142
149
143
150
### tracing::Span
144
151
145
-
When processing a request, you can create a `tracking::Span` object that measures the execution time of the current code block (technically, the time between its constructor and destructor) and stores the resulting time in the log:
152
+
When processing a request, you can create a tracing::Span object that measures the execution time of the current code
153
+
block (technically, the time between its constructor and destructor) and stores the resulting time in the log:
146
154
147
-
Example log `tracing::Span span("cache_invalidate")`:
155
+
Example log for `tracing::Span span{"cache_invalidate"}`:
`tracing::Span` can only be created on stack. Currently, the ability to create `tracing::Span` as a member of a class whose objects can be passed between tasks is not supported.
167
+
tracing::Span can only be created on stack. Currently, the ability to create `tracing::Span` as a member of a class
168
+
whose objects can be passed between tasks is not supported.
160
169
161
170
### Tags
162
171
163
-
In addition to `trace_id`, `span_id`, `parent_id` and other tags specific to opentracing, the `tracing::Span` class can store arbitrary custom tags. To do this, Span implicitly contains LogExtra. You can add tags like this:
172
+
In addition to `trace_id`, `span_id`, `parent_id` and other tags specific to opentracing, the `tracing::Span` class can
173
+
store arbitrary custom tags. To do this, Span implicitly contains LogExtra. You can add tags like this:
164
174
165
175
@snippet tracing/span_test.cpp Example using Span tracing
166
176
167
-
Unlike simple `LogExtra`, tags from `Span` are automatically logged when using `LOG_XXX()`. If you create a `Span`, and you already have a `Span`, then `LogExtra` is copied from the old one to the new one (except for the tags added via `AddNonInheritableTag`).
177
+
Unlike simple `LogExtra`, tags from `Span` are automatically logged when using `LOG_XXX()`. If you create a `Span`, and
178
+
you already have a `Span`, then `LogExtra` is copied from the old one to the new one (except for the tags added via
179
+
`AddNonInheritableTag`).
168
180
169
181
### Built-in tag semantics
170
182
171
183
- `TraceId` propagates both to sub-spans within a single service, and from client to server
172
-
- `Link` propagates within a single service, but not from client to server. A new link is generated for the "root" request handling task
184
+
- `Link` propagates within a single service, but not from client to server. A new link is generated for the "root"
185
+
request handling task
173
186
- `SpanId` identifies a specific span. It does not propagate
174
187
- For "root" request handling spans, there are additionally:
175
188
- `ParentSpanId`, which is the inner-most `SpanId` of the client
176
189
- `ParentLink`, which is the `Link` of the client
177
190
178
191
### Span hierarchy and logging
179
192
180
-
All logs in the current task are implicitly linked to the current `Span` for the user, and tags of this `Span` are added to them (trace_id, span_id, etc.). All `Span` in the current task are implicitly stacked, and if there are several similar `Span`, the last created One (i.e., located at the top of the `Span` stack of the current task) will be used for logging.
193
+
All logs in the current task are implicitly linked to the current `Span` for the user, and tags of this `Span` are added
194
+
to them (trace_id, span_id, etc.). All `Span` in the current task are implicitly stacked, and if there are several
195
+
similar `Span`, the last created One (i.e., located at the top of the `Span` stack of the current task) will be used for
196
+
logging.
181
197
182
198
@snippet tracing/span_test.cpp Example span hierarchy
183
199
184
-
If you want to get the current `Span` (for example, you want to write something to `LogExtra`, but do not want to create an additional `Span`), then you can use the following approach:
200
+
If you want to get the current `Span` (for example, you want to write something to `LogExtra`, but do not want to create
201
+
an additional `Span`), then you can use the following approach:
185
202
186
203
@snippet core/src/tracing/span_test.cpp Example get current span
187
204
188
205
### Creating a Span
189
206
190
-
A `Span` is automatically created for each request, handled by the server::handlers::HttpHandlerBase inherited handles. `trace_id`, `parent_id` and `link` are automatically populated for the request headers (if any).
207
+
A `Span` is automatically created for each request, handled by the server::handlers::HttpHandlerBase inherited handles.
208
+
`trace_id`, `parent_id` and `link` are automatically populated for the request headers (if any).
191
209
192
-
Cache handlers do not have a parent Span, so a Span without a parent is automatically created for them, with a new `trace_id` for each update.
193
-
The situation is similar for all utils::PeriodicTask.
210
+
Cache handlers do not have a parent Span, so a Span without a parent is automatically created for them, with a new
211
+
`trace_id` for each update. The situation is similar for all utils::PeriodicTask.
194
212
195
213
When creating a new task via `utils::Async`, a new `Span` is created and linked with the current `Span` of the current task.
196
214
@@ -214,9 +232,12 @@ X-YaTraceId
214
232
215
233
### Selectively disabling Span logging
216
234
217
-
Using the server dynamic config @ref USERVER_NO_LOG_SPANS, you can set names and prefixes of Span names that do not need to be logged. If the span is not logged, then the ScopeTime of this span and any custom tags attached to the span via the methods of the `Add*Tag*()` are not put into the logs.
235
+
Using the server dynamic config @ref USERVER_NO_LOG_SPANS, you can set names and prefixes of Span names that do not need
236
+
to be logged. If the span is not logged, then the tracing::ScopeTime of this span and any custom tags attached to the
237
+
span via the methods of the `Add*Tag*()` are not put into the logs.
218
238
219
-
For example, this is how you can disable logging of all Span for MongoDB (that is, all Span with `stopwatch_name` starting with `mongo`) and `Span` with `stopwatch_name=test`:
239
+
For example, this is how you can disable logging of all Span for MongoDB (that is, all Span with `stopwatch_name`
240
+
starting with `mongo`) and `Span` with `stopwatch_name=test`:
220
241
221
242
```json
222
243
{
@@ -234,8 +255,9 @@ For example, this is how you can disable logging of all Span for MongoDB (that i
234
255
## OpenTelemetry protocol
235
256
236
257
It is possible to use OpenTelemetry protocol for logs and tracing exporting.
237
-
To use it, register all prerequisites for gRPC client and register `otlp::LoggerComponent` in component list in your `main` function.
238
-
The static config file should contain a component section with OTLP logger.
258
+
To use it, register all prerequisites for gRPC client and register `otlp::LoggerComponent` in component list in your
259
+
`main` function. The static config file should contain a component section with OTLP logger.
260
+
239
261
Also remove `default` logger from `logging.loggers` as the default logger is handled by otlp component from now on.
240
262
241
263
The related part of static config:
@@ -259,11 +281,15 @@ The service buffers not-yet-sent logs and traces in memory, but drops them on ov
259
281
260
282
### Separate Sinks for Logs and Tracing
261
283
262
-
In certain environments, such as Kubernetes, applications typically write logs to stdout/stderr, while traces are sent efficiently through the 'push' model (via OTLP transport). Kubernetes stores the container's stdout/stderr in files on nodes, making logs available for log collectors using the 'pull' model. This approach ensures that logs remain accessible even if the application fails, capturing the most critical information.
284
+
In certain environments, such as Kubernetes, applications typically write logs to stdout/stderr, while traces are sent
285
+
efficiently through the 'push' model (via OTLP transport). Kubernetes stores the container's stdout/stderr in files on
286
+
nodes, making logs available for log collectors using the `pull` model. This approach ensures that logs remain
287
+
accessible even if the application fails, capturing the most critical information.
263
288
264
-
To configure separate sinks for logs and traces, use the optional 'sinks' block in the 'otlp-logger' configuration:
289
+
To configure separate sinks for logs and traces, use the optional `sinks` block in the `otlp-logger` configuration:
265
290
266
-
```yaml
291
+
```
292
+
yaml
267
293
otlp-logger:
268
294
endpoint: $otlp-endpoint
269
295
service-name: $service-name
@@ -273,21 +299,24 @@ otlp-logger:
273
299
tracing: default | otlp | both
274
300
```
275
301
276
-
If the 'sinks' block is not present in the 'otlp-logger' configuration, both logs and traces use the OTLP transport and are delivered to an OpenTelemetry-compatible collector by a background task in userver.
302
+
If the 'sinks' block is not present in the 'otlp-logger' configuration, both logs and traces use the OTLP transport and
303
+
are delivered to an OpenTelemetry-compatible collector by a background task in userver.
277
304
278
305
In the `sinks` block, you can set a value for each stream. See: `otlp::LoggerComponent`.
279
306
280
307
If you choose `otlp` for both streams, ensure that `logging.loggers` is empty:
281
308
282
-
```yaml
309
+
```
310
+
yaml
283
311
logging:
284
312
fs-task-processor: fs-task-processor
285
313
loggers: {}
286
314
```
287
315
288
316
Otherwise, add the _default_ logger in the `logging` component's `loggers` field:
0 commit comments