@@ -35,13 +35,17 @@ involving the centralized logging service.
35
35
36
36
## Configuration
37
37
38
- The logging settings should be placed under the ` logging-service ` key of the
39
- ` application.conf ` config. Each of the main components can customize format and
40
- output target via section in ` application.conf ` configuration file. The
38
+ All logging settings are configured via the ` logging-service ` section of the
39
+ ` application.conf ` config file . Each of the main components can customize format
40
+ and output target via section in ` application.conf ` configuration file. The
41
41
configuration is using HOCON-style, as defined by
42
42
[ lightbend/config] ( https://github.com/lightbend/config ) . Individual values
43
43
accepted in the config are inspired by SLF4J's properties, formatting and
44
- implementations.
44
+ implementations. Currently 3 components define logging configuration:
45
+
46
+ - [ ` project-manager ` ] ( ../../lib/scala/project-manager/src/main/resources/application.conf )
47
+ - [ ` launcher ` ] ( ../../engine/launcher/src/main/resources/application.conf )
48
+ - [ CLI] ( ../../engine/runner/src/main/resources/application.conf )
45
49
46
50
The configuration has two main sections:
47
51
@@ -57,8 +61,8 @@ representation is available as an instance of
57
61
programmatically initialize loggers.
58
62
59
63
As per [ configuration schema] ( https://github.com/lightbend/config ) any key can
60
- have a default value that can be overridden by an environment variable. For
61
- example
64
+ be defined with a default value that can be overridden by an environment
65
+ variable. For example
62
66
63
67
```
64
68
{
@@ -72,10 +76,18 @@ it is defined during loading of the config file.
72
76
73
77
### Custom Log Levels
74
78
75
- The ` logging-service.logger ` configuration provides an ability to override the
76
- default application log level for particular loggers. In the ` logger ` subconfig
77
- the key specifies the logger name (or it's prefix) and the value specifies the
78
- log level for that logger.
79
+ Possible log level values are (in the order of precedence):
80
+
81
+ - ` error `
82
+ - ` warn `
83
+ - ` info `
84
+ - ` debug `
85
+ - ` trace `
86
+
87
+ The ` logging-service.logger ` configuration section provides an ability to
88
+ override the default application log level for particular loggers. In the
89
+ ` logger ` subconfig the key specifies the logger name (or it's prefix) and the
90
+ value specifies the log level for that logger.
79
91
80
92
```
81
93
logging-service.logger {
@@ -117,7 +129,7 @@ properties always have a higher priority over those defined in the
117
129
118
130
### Appenders
119
131
120
- Log output target is also configured in the ` application.conf ` files in the
132
+ Log output target is configured in the ` application.conf ` files in the
121
133
"appenders" section ("appender" is equivalent to ` java.util.logging.Handler `
122
134
semantics). Each appender section can provide further required and optional
123
135
key/value pairs, to better customize the log target output.
@@ -133,8 +145,10 @@ Currently supported are
133
145
a sentry.io service
134
146
135
147
The appenders are defined by the ` logging-service.appenders ` . Currently only a
136
- single appender can be selected at a time. The selection may also be done via an
137
- environmental variable but it depends on which component we are executing.
148
+ single appender can be selected at a time, although additional
149
+ [ logging to file] ( #logging-to-file ) is supported. The selection may also be done
150
+ via an environmental variable but it depends on which component we are
151
+ executing.
138
152
139
153
- ` project-manager ` - project manager by default starts a centralized logging
140
154
server that collects logs (as defined in ` logging-service.server ` config key)
@@ -169,6 +183,17 @@ message via `pattern` field e.g.
169
183
]
170
184
```
171
185
186
+ In the above example ` %logger ` format will be substituted with a class name for
187
+ which the logger was created with.
188
+
189
+ By default, console pattern includes ` %nopex ` formatter which means that any
190
+ stacktrace attached to the log message will always be ignored. By default other
191
+ appenders do not have such formatting key. This means that if an exception is
192
+ included in the logged messaged, a full stacktrace will be attached, if present.
193
+
194
+ For a full list of formatting keys please refer to the concrete implementation's
195
+ [ manual] ( https://logback.qos.ch/manual/layouts.html#ClassicPatternLayout ) .
196
+
172
197
#### File Appender
173
198
174
199
Enabled with ` ENSO_APPENDER_DEFAULT=file ` environment variable.
@@ -352,18 +377,83 @@ way it can verify that all logs are being reported within the provided code.
352
377
### Logging to file
353
378
354
379
By default Enso will attempt to persist (verbose) logs into a designated log
355
- file. This means that even though a user might be shown ` WARNING ` level logs in
356
- the console, logs with up to ` DEBUG ` level will be dumped into the log file. A
357
- user can disable this parallel logging to a file by setting the environment
358
- variable:
380
+ file. This means that even though a user might be shown only ` WARNING ` level
381
+ logs in the console, logs with up to ` DEBUG ` or ` TRACE ` level, including full
382
+ stacktraces, can be dumped into the log file. A user can disable this parallel
383
+ logging to a file by setting the environment variable:
359
384
360
385
```
361
386
ENSO_LOG_TO_FILE=false project-manager ...
362
387
```
363
388
364
- Users can also modify the default maximal log level, ` DEBUG ` , used when logging
365
- to a log file by setting the environment variable:
389
+ Users can fully control the maximal log level used when logging to a log file by
390
+ setting the environment variable:
366
391
367
392
```
368
393
ENSO_LOG_TO_FILE_LOG_LEVEL=trace project-manager ...
369
394
```
395
+
396
+ For example, in the above example ` project-manager ` will log events of up-to
397
+ ` trace ` in the log file.
398
+
399
+ ** Note** Logging to a file requires presence of the ` file `
400
+ [ appender] ( #file-appender ) in the ` logging-service.appenders ` section.
401
+
402
+ # How to use logging
403
+
404
+ Logging infrastructure uses a popular SLF4J interface which most of developers
405
+ should be familiar with. In this section we include a only small number of
406
+ examples, full user manual is available at SLF4J's
407
+ [ website] ( https://www.slf4j.org/manual.html ) .
408
+
409
+ ## Log a simple INFO message
410
+
411
+ ```
412
+ import org.slf4j.Logger;
413
+ import org.slf4j.LoggerFactory;
414
+
415
+ public class HelloWorld {
416
+
417
+ public static void main(String[] args) {
418
+ Logger logger = LoggerFactory.getLogger(HelloWorld.class);
419
+ logger.info("Hello World");
420
+ }
421
+ }
422
+ ```
423
+
424
+ ## Log a simple INFO message only if TRACE is enabled
425
+
426
+ ```
427
+ import org.slf4j.Logger;
428
+ import org.slf4j.LoggerFactory;
429
+
430
+ public class HelloWorld {
431
+
432
+ public static void main(String[] args) {
433
+ Logger logger = LoggerFactory.getLogger(HelloWorld.class);
434
+ if (logger.isTraceEnabled()) {
435
+ logger.info("Hello World");
436
+ }
437
+ }
438
+ }
439
+ ```
440
+
441
+ ## Log an exception
442
+
443
+ ```
444
+ import org.slf4j.Logger;
445
+ import org.slf4j.LoggerFactory;
446
+
447
+ public class HelloWorld {
448
+
449
+ public static void main(String[] args) {
450
+ Logger logger = LoggerFactory.getLogger(HelloWorld.class);
451
+ Throwable ex = new RuntimeException("foo")
452
+ logger.error("Hello World", ex);
453
+ }
454
+ }
455
+ ```
456
+
457
+ Note that in order for the full stacktrace to be printed, pattern in the desired
458
+ appender must not contain ` %nopex ` formatting key. See [ formatting] ( #format ) for
459
+ details.
0 commit comments