-
Notifications
You must be signed in to change notification settings - Fork 323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix logging in CLI #11472
Fix logging in CLI #11472
Changes from all commits
ae905a8
379e845
03fac31
7434319
8a1e716
f4ae9a0
bf837ad
dfdeb4a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,13 +35,17 @@ involving the centralized logging service. | |
|
||
## Configuration | ||
|
||
The logging settings should be placed under the `logging-service` key of the | ||
`application.conf` config. Each of the main components can customize format and | ||
output target via section in `application.conf` configuration file. The | ||
All logging settings are configured via the `logging-service` section of the | ||
`application.conf` config file. Each of the main components can customize format | ||
and output target via section in `application.conf` configuration file. The | ||
configuration is using HOCON-style, as defined by | ||
[lightbend/config](https://github.com/lightbend/config). Individual values | ||
accepted in the config are inspired by SLF4J's properties, formatting and | ||
implementations. | ||
implementations. Currently 3 components define logging configuration: | ||
|
||
- [`project-manager`](../../lib/scala/project-manager/src/main/resources/application.conf) | ||
- [`launcher`](../../engine/launcher/src/main/resources/application.conf) | ||
- [CLI](../../engine/runner/src/main/resources/application.conf) | ||
|
||
The configuration has two main sections: | ||
|
||
|
@@ -57,8 +61,8 @@ representation is available as an instance of | |
programmatically initialize loggers. | ||
|
||
As per [configuration schema](https://github.com/lightbend/config) any key can | ||
have a default value that can be overridden by an environment variable. For | ||
example | ||
be defined with a default value that can be overridden by an environment | ||
variable. For example | ||
|
||
``` | ||
{ | ||
|
@@ -72,10 +76,18 @@ it is defined during loading of the config file. | |
|
||
### Custom Log Levels | ||
|
||
The `logging-service.logger` configuration provides an ability to override the | ||
default application log level for particular loggers. In the `logger` subconfig | ||
the key specifies the logger name (or it's prefix) and the value specifies the | ||
log level for that logger. | ||
Possible log level values are (in the order of precedence): | ||
|
||
- `error` | ||
- `warn` | ||
- `info` | ||
- `debug` | ||
- `trace` | ||
|
||
The `logging-service.logger` configuration section provides an ability to | ||
override the default application log level for particular loggers. In the | ||
`logger` subconfig the key specifies the logger name (or it's prefix) and the | ||
value specifies the log level for that logger. | ||
|
||
``` | ||
logging-service.logger { | ||
|
@@ -117,7 +129,7 @@ properties always have a higher priority over those defined in the | |
|
||
### Appenders | ||
|
||
Log output target is also configured in the `application.conf` files in the | ||
Log output target is configured in the `application.conf` files in the | ||
"appenders" section ("appender" is equivalent to `java.util.logging.Handler` | ||
semantics). Each appender section can provide further required and optional | ||
key/value pairs, to better customize the log target output. | ||
|
@@ -133,8 +145,10 @@ Currently supported are | |
a sentry.io service | ||
|
||
The appenders are defined by the `logging-service.appenders`. Currently only a | ||
single appender can be selected at a time. The selection may also be done via an | ||
environmental variable but it depends on which component we are executing. | ||
single appender can be selected at a time, although additional | ||
[logging to file](#logging-to-file) is supported. The selection may also be done | ||
via an environmental variable but it depends on which component we are | ||
executing. | ||
|
||
- `project-manager` - project manager by default starts a centralized logging | ||
server that collects logs (as defined in `logging-service.server` config key) | ||
|
@@ -169,6 +183,17 @@ message via `pattern` field e.g. | |
] | ||
``` | ||
|
||
In the above example `%logger` format will be substituted with a class name for | ||
which the logger was created with. | ||
|
||
By default, console pattern includes `%nopex` formatter which means that any | ||
stacktrace attached to the log message will always be ignored. By default other | ||
appenders do not have such formatting key. This means that if an exception is | ||
included in the logged messaged, a full stacktrace will be attached, if present. | ||
|
||
For a full list of formatting keys please refer to the concrete implementation's | ||
[manual](https://logback.qos.ch/manual/layouts.html#ClassicPatternLayout). | ||
|
||
#### File Appender | ||
|
||
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. | |
### Logging to file | ||
|
||
By default Enso will attempt to persist (verbose) logs into a designated log | ||
file. This means that even though a user might be shown `WARNING` level logs in | ||
the console, logs with up to `DEBUG` level will be dumped into the log file. A | ||
user can disable this parallel logging to a file by setting the environment | ||
variable: | ||
file. This means that even though a user might be shown only `WARNING` level | ||
logs in the console, logs with up to `DEBUG` or `TRACE` level, including full | ||
stacktraces, can be dumped into the log file. A user can disable this parallel | ||
logging to a file by setting the environment variable: | ||
|
||
``` | ||
ENSO_LOG_TO_FILE=false project-manager ... | ||
``` | ||
|
||
Users can also modify the default maximal log level, `DEBUG`, used when logging | ||
to a log file by setting the environment variable: | ||
Users can fully control the maximal log level used when logging to a log file by | ||
setting the environment variable: | ||
|
||
``` | ||
ENSO_LOG_TO_FILE_LOG_LEVEL=trace project-manager ... | ||
``` | ||
|
||
For example, in the above example `project-manager` will log events of up-to | ||
`trace` in the log file. | ||
|
||
**Note** Logging to a file requires presence of the `file` | ||
[appender](#file-appender) in the `logging-service.appenders` section. | ||
|
||
# How to use logging | ||
|
||
Logging infrastructure uses a popular SLF4J interface which most of developers | ||
should be familiar with. In this section we include a only small number of | ||
examples, full user manual is available at SLF4J's | ||
[website](https://www.slf4j.org/manual.html). | ||
|
||
## Log a simple INFO message | ||
|
||
``` | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class HelloWorld { | ||
|
||
public static void main(String[] args) { | ||
Logger logger = LoggerFactory.getLogger(HelloWorld.class); | ||
logger.info("Hello World"); | ||
} | ||
} | ||
``` | ||
|
||
## Log a simple INFO message only if TRACE is enabled | ||
|
||
``` | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class HelloWorld { | ||
|
||
public static void main(String[] args) { | ||
Logger logger = LoggerFactory.getLogger(HelloWorld.class); | ||
if (logger.isTraceEnabled()) { | ||
logger.info("Hello World"); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this the right pattern? I would expect simple There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You asked for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I have asked for a way to "log a message to a console if a verbosity is increased". Does: logger.trace("Hello World") appear on the console when PS: Using Logger logger = LoggerFactory.getLogger(HelloWorld.class);
if (logger.isTraceEnabled()) {
logger.trace("Hello World");
} e.g. using |
||
} | ||
} | ||
``` | ||
|
||
## Log an exception | ||
|
||
``` | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class HelloWorld { | ||
|
||
public static void main(String[] args) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, example of logging from Enso (including how to launch the CLI) would be more useful than standalone class unrelated to Enso. Is it guaranteed that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You clearly haven't read the documentation. We don't log stacktraces in console, on purpose. You can of course change that by removing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Then your answer should be: It is not possible. I am not OK with such an answer, but that's a unrelated to the answer. |
||
Logger logger = LoggerFactory.getLogger(HelloWorld.class); | ||
Throwable ex = new RuntimeException("foo") | ||
logger.error("Hello World", ex); | ||
} | ||
} | ||
``` | ||
|
||
Note that in order for the full stacktrace to be printed, pattern in the desired | ||
appender must not contain `%nopex` formatting key. See [formatting](#format) for | ||
details. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.enso.runner; | ||
|
||
import org.enso.distribution.Environment; | ||
|
||
public class RunnerEnvironment implements Environment {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# CLI's application.conf | ||
|
||
logging-service { | ||
logger { | ||
akka.actor = info | ||
akka.event = error | ||
akka.io = error | ||
akka.stream = error | ||
} | ||
appenders = [ | ||
{ | ||
name = "file", | ||
pattern = "[%level{lowercase=true}] [%d{yyyy-MM-dd'T'HH:mm:ssXXX}] [%logger] %msg%n" | ||
}, | ||
{ | ||
name = "console" | ||
pattern = "[%level{lowercase=true}] [%d{yyyy-MM-dd'T'HH:mm:ssXXX}] [%logger] %msg%n%nopex" | ||
} | ||
] | ||
default-appender = console | ||
default-appender = ${?ENSO_APPENDER_DEFAULT} | ||
log-to-file { | ||
enable = true | ||
enable = ${?ENSO_LOG_TO_FILE} | ||
log-level = trace | ||
log-level = ${?ENSO_LOG_TO_FILE_LOG_LEVEL} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am probably going to use this from an Enso code, not for a a separate class with
main
main method.Where will this message appear when I run
./enso --run hello_world.enso
? Console? Somewhere else?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really don't understand your request. You asked for examples of using logging infrastructure so I essentially copy pasted API examples from SLF4J because that's it is to it.
Now you ask for logging in Enso which is a different thing and has been quickly developed by libs team and has not been integrated with backend at all AFAIK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I beg your pardon. I mean logging from Enso runtime code (like PassManager).
I am sorry for causing confusion. No, logging from Enso code isn't what I am interested in right now.