Respect log.textLogging and log.jsonLogging using --console-log

The gerrit.war daemon provides a --console-log flag to send the
error logs to stderr. However, when using this flag, no logs were
written to the error_log or error_log.json files, even if the
corresponding option, log.textLogging or log.jsonLogging, was set
in the gerrit.config file.

On the one hand, the configuration should be respected and on the
other hand, there are usecases, where both output channels are
useful. Mainly, if Gerrit is run in a docker container, it is
useful to be able to use 'docker logs' to view the current logs
and to have the logs persisted at the same time. While one can
tail the log-file to do so, Gerrit is then not the main process
of the container and will thus not be gracefully shutdown, when
stopping the container. Use the --console-log option together
with {text|json}Logging to log to stderr and the error_log file
without the need to tail the file to stdout.

Note, that this will as a side effect change the default behaviour
of the --console-log flag. Since the log.textLogging option
is true by default, using the --console-log flag will now by
default log to stderr and the error_log file. This can be
prevented by setting log.textLogging to 'false'.

Bug: Issue 13184
Change-Id: I5c3dfa531a6ccd397d3ceb60defc6e83943e95f1
This commit is contained in:
Thomas Draebing
2020-07-27 12:44:10 +02:00
committed by Thomas Dräbing
parent ddfe7765d4
commit 773777e613
3 changed files with 22 additions and 12 deletions

View File

@@ -49,8 +49,9 @@ per the local copy of link:config-gerrit.html[gerrit.config] located under
This option automatically implies '--enable-sshd'. This option automatically implies '--enable-sshd'.
--console-log:: --console-log::
Send log messages to the console, instead of to the standard Send log messages to the console. Log files will still be written to
log file '$site_path/logs/error_log'. the error log file, if log.textLogging and/or log.jsonLogging is set to
'true'.
--headless:: --headless::
Don't start the default Gerrit UI. May be useful when Gerrit is Don't start the default Gerrit UI. May be useful when Gerrit is

View File

@@ -331,9 +331,7 @@ public class Daemon extends SiteProgram {
sysInjector.getInstance(PluginGuiceEnvironment.class).setDbCfgInjector(dbInjector, cfgInjector); sysInjector.getInstance(PluginGuiceEnvironment.class).setDbCfgInjector(dbInjector, cfgInjector);
manager.add(dbInjector, cfgInjector, sysInjector); manager.add(dbInjector, cfgInjector, sysInjector);
if (!consoleLog) { manager.add(ErrorLogFile.start(getSitePath(), config, consoleLog));
manager.add(ErrorLogFile.start(getSitePath(), config));
}
sshd &= !sshdOff(); sshd &= !sshdOff();
if (sshd) { if (sshd) {

View File

@@ -49,11 +49,12 @@ public class ErrorLogFile {
root.addAppender(dst); root.addAppender(dst);
} }
public static LifecycleListener start(Path sitePath, Config config) throws IOException { public static LifecycleListener start(Path sitePath, Config config, boolean consoleLog)
throws IOException {
Path logdir = Path logdir =
FileUtil.mkdirsOrDie(new SitePaths(sitePath).logs_dir, "Cannot create log directory"); FileUtil.mkdirsOrDie(new SitePaths(sitePath).logs_dir, "Cannot create log directory");
if (SystemLog.shouldConfigure()) { if (SystemLog.shouldConfigure()) {
initLogSystem(logdir, config); initLogSystem(logdir, config, consoleLog);
} }
return new LifecycleListener() { return new LifecycleListener() {
@@ -67,18 +68,28 @@ public class ErrorLogFile {
}; };
} }
private static void initLogSystem(Path logdir, Config config) { private static void initLogSystem(Path logdir, Config config, boolean consoleLog) {
Logger root = LogManager.getRootLogger(); Logger root = LogManager.getRootLogger();
root.removeAllAppenders(); root.removeAllAppenders();
PatternLayout errorLogLayout = new PatternLayout("[%d] [%t] %-5p %c %x: %m%n");
if (consoleLog) {
ConsoleAppender dst = new ConsoleAppender();
dst.setLayout(errorLogLayout);
dst.setTarget("System.err");
dst.setThreshold(Level.INFO);
dst.activateOptions();
root.addAppender(dst);
}
boolean json = config.getBoolean("log", "jsonLogging", false); boolean json = config.getBoolean("log", "jsonLogging", false);
boolean text = config.getBoolean("log", "textLogging", true) || !json; boolean text = config.getBoolean("log", "textLogging", true) || !(json || consoleLog);
boolean rotate = config.getBoolean("log", "rotate", true); boolean rotate = config.getBoolean("log", "rotate", true);
if (text) { if (text) {
root.addAppender( root.addAppender(SystemLog.createAppender(logdir, LOG_NAME, errorLogLayout, rotate));
SystemLog.createAppender(
logdir, LOG_NAME, new PatternLayout("[%d] [%t] %-5p %c %x: %m%n"), rotate));
} }
if (json) { if (json) {