Skip to content

Commit

Permalink
Print suggestion to view log on fatal error (elastic#75418)
Browse files Browse the repository at this point in the history
Closes elastic#61767.

When Elasticsearch fails to start up, it tries to print a message to
suggest to the user that they consult the log for more information.
However, if a fatal error is encountered, Elasticsearch exits directly and
no such message is printed.

Improve this situation by printing the same message when Elasticsearch
is about to exit due to a fatal unexpected error.
  • Loading branch information
pugnascotia authored and ywangd committed Jul 30, 2021
1 parent 2076cb6 commit 572cfb7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
30 changes: 19 additions & 11 deletions server/src/main/java/org/elasticsearch/bootstrap/Elasticsearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,29 @@ public void checkPermission(Permission perm) {
final Elasticsearch elasticsearch = new Elasticsearch();
int status = main(args, elasticsearch, Terminal.DEFAULT);
if (status != ExitCodes.OK) {
final String basePath = System.getProperty("es.logs.base_path");
// It's possible to fail before logging has been configured, in which case there's no point
// suggesting that the user look in the log file.
if (basePath != null) {
Terminal.DEFAULT.errorPrintln(
"ERROR: Elasticsearch did not exit normally - check the logs at "
+ basePath
+ System.getProperty("file.separator")
+ System.getProperty("es.logs.cluster_name") + ".log"
);
}
printLogsSuggestion();
exit(status);
}
}

/**
* Prints a message directing the user to look at the logs. A message is only printed if
* logging has been configured.
*/
static void printLogsSuggestion() {
final String basePath = System.getProperty("es.logs.base_path");
// It's possible to fail before logging has been configured, in which case there's no point
// suggesting that the user look in the log file.
if (basePath != null) {
Terminal.DEFAULT.errorPrintln(
"ERROR: Elasticsearch did not exit normally - check the logs at "
+ basePath
+ System.getProperty("file.separator")
+ System.getProperty("es.logs.cluster_name") + ".log"
);
}
}

private static void overrideDnsCachePolicyProperties() {
for (final String property : new String[] {"networkaddress.cache.ttl", "networkaddress.cache.negative.ttl" }) {
final String overrideProperty = "es." + property;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ void onFatalUncaught(final String threadName, final Throwable t) {
t.printStackTrace(Terminal.DEFAULT.getErrorWriter());
// Without a final flush, the stacktrace may not be shown before ES exits
Terminal.DEFAULT.flush();

Elasticsearch.printLogsSuggestion();
}

void onNonFatalUncaught(final String threadName, final Throwable t) {
Expand Down

0 comments on commit 572cfb7

Please sign in to comment.