Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,6 @@ org.apache.logging.log4j.Logger#error(java.lang.Object)
org.apache.logging.log4j.Logger#error(java.lang.Object, java.lang.Throwable)
org.apache.logging.log4j.Logger#fatal(java.lang.Object)
org.apache.logging.log4j.Logger#fatal(java.lang.Object, java.lang.Throwable)

@defaultMessage Write output to a Logger not the console
org.elasticsearch.bootstrap.ElasticsearchConsole#getInstance()
10 changes: 7 additions & 3 deletions server/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,12 @@ static void init(
final Runnable sysOutCloser = getSysOutCloser();
final Runnable sysErrorCloser = getSysErrorCloser();

// If this is true, we retain access to the original stdout stream for exceptional cases that need to write to the console
final boolean hasConsole = foreground && (quiet == false);
if (hasConsole) {
ElasticsearchConsole.init();
}

LogConfigurator.setNodeName(Node.NODE_NAME_SETTING.get(environment.settings()));
try {
LogConfigurator.configure(environment);
Expand All @@ -342,10 +348,8 @@ static void init(
}
}


try {
final boolean closeStandardStreams = (foreground == false) || quiet;
if (closeStandardStreams) {
if (hasConsole == false) {
final Logger rootLogger = LogManager.getRootLogger();
final Appender maybeConsoleAppender = Loggers.findAppender(rootLogger, ConsoleAppender.class);
if (maybeConsoleAppender != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.bootstrap;

import org.elasticsearch.core.Nullable;
import org.elasticsearch.core.SuppressForbidden;

import java.io.IOException;
import java.io.PrintStream;
import java.util.Locale;

/**
* A basic "console" class for writing to the user's console without writing to the logfile.
* This is to be used in very exceptional circumstances (e.g. printing out sensitive values that ought not to be logged).
* It is not to be confused with the java {@link java.io.Console} (see {@link System#console()}) which will generally be {@code null}
* when running Elasticsearch from the command line
*/
public class ElasticsearchConsole {

private static ElasticsearchConsole instance = null;

private final PrintStream stdout;

/**
* Returns the current console. Will be {@code null} if there is no console.
* It is very common for there not to be a console (for example, because Elasticsearch is running as a service) - callers should be
* prepared to handle a null console instance.
*/
@Nullable
public static ElasticsearchConsole getInstance() {
return instance;
}

ElasticsearchConsole(PrintStream stdout) {
this.stdout = stdout;
}

public void println(String line) throws IOException {
stdout.println(line);
}

public void printf(String format, Object... args) throws IOException {
stdout.printf(Locale.ROOT, format, args);
}

@SuppressForbidden(reason = "Initialize using System.out")
static void init() {
instance = new ElasticsearchConsole(System.out);
}
}