GerritBaseTests: Activate test logging
Test logging is already activated in test derived from AbstractDaemonTest. Extract the code to activate the test logging in new TestLoggingactivator class and also activate the log for tests derived from GerritBaseTests class. Also document in dev-bazel.txt how to increase log level for tests from IDE and from Bazel. Also remove some superfluous white spaces from dev-bazel.txt. Change-Id: I7641bfcffa35e36c0330dc9244ceafd8366159ca
This commit is contained in:
parent
9fa32699a7
commit
17ef12713b
@ -288,7 +288,7 @@ The WAR file will be placed in:
|
||||
Debugging tests:
|
||||
|
||||
----
|
||||
bazel test --test_output=streamed --test_filter=com.gerrit.TestClass.testMethod testTarget
|
||||
bazel test --test_output=streamed --test_filter=com.gerrit.TestClass.testMethod testTarget
|
||||
----
|
||||
|
||||
Debug test example:
|
||||
@ -389,6 +389,25 @@ If Docker is not available, the Elasticsearch tests will be skipped.
|
||||
Note that Bazel currently does not show
|
||||
link:https://github.com/bazelbuild/bazel/issues/3476[the skipped tests].
|
||||
|
||||
[[logging]]
|
||||
=== Controlling logging level
|
||||
|
||||
Per default, logging level is set to `INFO` level for all tests. The `DEBUG`
|
||||
log level can be enabled for the tests.
|
||||
|
||||
In IDE, set `-Dgerrit.logLevel=debug` as a VM argument. With `bazel`, pass
|
||||
`GERRIT_LOG_LEVEL=debug` environment variable:
|
||||
|
||||
----
|
||||
bazel test --test_filter=com.gerrit.server.notedb.ChangeNotesTest \
|
||||
--test_env=GERRIT_LOG_LEVEL=debug \
|
||||
javatests/com/google/gerrit/server:server_tests
|
||||
----
|
||||
|
||||
The log results can be found in:
|
||||
`bazel-testlogs/javatests/com/google/gerrit/server/server_tests/test.log`.
|
||||
|
||||
|
||||
== Dependencies
|
||||
|
||||
Dependency JARs are normally downloaded as needed, but you can
|
||||
@ -583,7 +602,6 @@ To use the binary from the Bazel build, you need to use the `run_npm_binary.py`
|
||||
wrapper script. For an example, see the use of `crisper` in `tools/bzl/js.bzl`.
|
||||
|
||||
|
||||
|
||||
[[RBE]]
|
||||
== Google Remote Build Support
|
||||
|
||||
@ -618,8 +636,6 @@ bazel test --config=remote \
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
GERRIT
|
||||
------
|
||||
Part of link:index.html[Gerrit Code Review]
|
||||
|
@ -88,8 +88,6 @@ java_library2(
|
||||
"//lib/httpcomponents:httpcore",
|
||||
"//lib/jetty:servlet",
|
||||
"//lib/jgit/org.eclipse.jgit.junit:junit",
|
||||
"//lib/log:impl-log4j",
|
||||
"//lib/log:log4j",
|
||||
"//lib/truth",
|
||||
"//lib/truth:truth-java8-extension",
|
||||
"//prolog:gerrit-prolog-common",
|
||||
|
@ -17,13 +17,11 @@ package com.google.gerrit.acceptance;
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.google.common.base.Preconditions.checkState;
|
||||
import static java.util.Objects.requireNonNull;
|
||||
import static org.apache.log4j.Logger.getLogger;
|
||||
|
||||
import com.google.auto.value.AutoValue;
|
||||
import com.google.common.base.MoreObjects;
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.gerrit.acceptance.testsuite.account.AccountOperations;
|
||||
import com.google.gerrit.acceptance.testsuite.account.AccountOperationsImpl;
|
||||
import com.google.gerrit.acceptance.testsuite.group.GroupOperations;
|
||||
@ -50,6 +48,7 @@ import com.google.gerrit.testing.NoteDbChecker;
|
||||
import com.google.gerrit.testing.NoteDbMode;
|
||||
import com.google.gerrit.testing.SshMode;
|
||||
import com.google.gerrit.testing.TempFileUtil;
|
||||
import com.google.gerrit.testing.TestLoggingActivator;
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Injector;
|
||||
import com.google.inject.Key;
|
||||
@ -71,11 +70,6 @@ import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Stream;
|
||||
import org.apache.log4j.ConsoleAppender;
|
||||
import org.apache.log4j.Level;
|
||||
import org.apache.log4j.LogManager;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.apache.log4j.PatternLayout;
|
||||
import org.eclipse.jgit.lib.Config;
|
||||
import org.eclipse.jgit.lib.RepositoryCache;
|
||||
import org.eclipse.jgit.util.FS;
|
||||
@ -197,54 +191,6 @@ public class GerritServer implements AutoCloseable {
|
||||
}
|
||||
}
|
||||
|
||||
private static final ImmutableMap<String, Level> LOG_LEVELS =
|
||||
ImmutableMap.<String, Level>builder()
|
||||
.put("com.google.gerrit", getGerritLogLevel())
|
||||
|
||||
// Silence non-critical messages from MINA SSHD.
|
||||
.put("org.apache.mina", Level.WARN)
|
||||
.put("org.apache.sshd.common", Level.WARN)
|
||||
.put("org.apache.sshd.server", Level.WARN)
|
||||
.put("org.apache.sshd.common.keyprovider.FileKeyPairProvider", Level.INFO)
|
||||
.put("com.google.gerrit.sshd.GerritServerSession", Level.WARN)
|
||||
|
||||
// Silence non-critical messages from mime-util.
|
||||
.put("eu.medsea.mimeutil", Level.WARN)
|
||||
|
||||
// Silence non-critical messages from openid4java.
|
||||
.put("org.apache.xml", Level.WARN)
|
||||
.put("org.openid4java", Level.WARN)
|
||||
.put("org.openid4java.consumer.ConsumerManager", Level.FATAL)
|
||||
.put("org.openid4java.discovery.Discovery", Level.ERROR)
|
||||
.put("org.openid4java.server.RealmVerifier", Level.ERROR)
|
||||
.put("org.openid4java.message.AuthSuccess", Level.ERROR)
|
||||
|
||||
// Silence non-critical messages from c3p0 (if used).
|
||||
.put("com.mchange.v2.c3p0", Level.WARN)
|
||||
.put("com.mchange.v2.resourcepool", Level.WARN)
|
||||
.put("com.mchange.v2.sql", Level.WARN)
|
||||
|
||||
// Silence non-critical messages from apache.http.
|
||||
.put("org.apache.http", Level.WARN)
|
||||
|
||||
// Silence non-critical messages from Jetty.
|
||||
.put("org.eclipse.jetty", Level.WARN)
|
||||
|
||||
// Silence non-critical messages from JGit.
|
||||
.put("org.eclipse.jgit.transport.PacketLineIn", Level.WARN)
|
||||
.put("org.eclipse.jgit.transport.PacketLineOut", Level.WARN)
|
||||
.put("org.eclipse.jgit.internal.storage.file.FileSnapshot", Level.WARN)
|
||||
.put("org.eclipse.jgit.util.FS", Level.WARN)
|
||||
.build();
|
||||
|
||||
private static Level getGerritLogLevel() {
|
||||
String value = Strings.nullToEmpty(System.getenv("GERRIT_LOG_LEVEL"));
|
||||
if (value.isEmpty()) {
|
||||
value = Strings.nullToEmpty(System.getProperty("gerrit.logLevel"));
|
||||
}
|
||||
return Level.toLevel(value, Level.INFO);
|
||||
}
|
||||
|
||||
private static boolean forceLocalDisk() {
|
||||
String value = Strings.nullToEmpty(System.getenv("GERRIT_FORCE_LOCAL_DISK"));
|
||||
if (value.isEmpty()) {
|
||||
@ -360,7 +306,7 @@ public class GerritServer implements AutoCloseable {
|
||||
throws Exception {
|
||||
checkArgument(site != null, "site is required (even for in-memory server");
|
||||
desc.checkValidAnnotations();
|
||||
configureLogging();
|
||||
TestLoggingActivator.configureLogging();
|
||||
CyclicBarrier serverStarted = new CyclicBarrier(2);
|
||||
Daemon daemon =
|
||||
new Daemon(
|
||||
@ -465,25 +411,6 @@ public class GerritServer implements AutoCloseable {
|
||||
return new GerritServer(desc, site, createTestInjector(daemon), daemon, daemonService);
|
||||
}
|
||||
|
||||
private static void configureLogging() {
|
||||
LogManager.resetConfiguration();
|
||||
|
||||
PatternLayout layout = new PatternLayout();
|
||||
layout.setConversionPattern("%-5p %c %x: %m%n");
|
||||
|
||||
ConsoleAppender dst = new ConsoleAppender();
|
||||
dst.setLayout(layout);
|
||||
dst.setTarget("System.err");
|
||||
dst.setThreshold(Level.DEBUG);
|
||||
dst.activateOptions();
|
||||
|
||||
Logger root = LogManager.getRootLogger();
|
||||
root.removeAllAppenders();
|
||||
root.addAppender(dst);
|
||||
|
||||
LOG_LEVELS.entrySet().stream().forEach(e -> getLogger(e.getKey()).setLevel(e.getValue()));
|
||||
}
|
||||
|
||||
private static void mergeTestConfig(Config cfg) {
|
||||
String forceEphemeralPort = String.format("%s:0", getLocalHost().getHostName());
|
||||
String url = "http://" + forceEphemeralPort + "/";
|
||||
|
@ -46,6 +46,8 @@ java_library(
|
||||
"//lib/guice:guice-servlet",
|
||||
"//lib/jgit/org.eclipse.jgit:jgit",
|
||||
"//lib/jgit/org.eclipse.jgit.junit:junit",
|
||||
"//lib/log:impl-log4j",
|
||||
"//lib/log:log4j",
|
||||
"//lib/truth",
|
||||
],
|
||||
)
|
||||
|
@ -17,6 +17,7 @@ package com.google.gerrit.testing;
|
||||
import com.google.common.base.CharMatcher;
|
||||
import com.google.gwtorm.client.KeyUtil;
|
||||
import com.google.gwtorm.server.StandardKeyEncoder;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Rule;
|
||||
import org.junit.rules.ExpectedException;
|
||||
@ -31,6 +32,11 @@ public abstract class GerritBaseTests {
|
||||
@Rule public ExpectedException exception = ExpectedException.none();
|
||||
@Rule public final TestName testName = new TestName();
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClassTest() {
|
||||
TestLoggingActivator.configureLogging();
|
||||
}
|
||||
|
||||
protected String getSanitizedMethodName() {
|
||||
String name = testName.getMethodName().toLowerCase();
|
||||
name =
|
||||
|
94
java/com/google/gerrit/testing/TestLoggingActivator.java
Normal file
94
java/com/google/gerrit/testing/TestLoggingActivator.java
Normal file
@ -0,0 +1,94 @@
|
||||
// Copyright (C) 2020 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.testing;
|
||||
|
||||
import static org.apache.log4j.Logger.getLogger;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import org.apache.log4j.ConsoleAppender;
|
||||
import org.apache.log4j.Level;
|
||||
import org.apache.log4j.LogManager;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.apache.log4j.PatternLayout;
|
||||
|
||||
public class TestLoggingActivator {
|
||||
private static final ImmutableMap<String, Level> LOG_LEVELS =
|
||||
ImmutableMap.<String, Level>builder()
|
||||
.put("com.google.gerrit", getGerritLogLevel())
|
||||
|
||||
// Silence non-critical messages from MINA SSHD.
|
||||
.put("org.apache.mina", Level.WARN)
|
||||
.put("org.apache.sshd.common", Level.WARN)
|
||||
.put("org.apache.sshd.server", Level.WARN)
|
||||
.put("org.apache.sshd.common.keyprovider.FileKeyPairProvider", Level.INFO)
|
||||
.put("com.google.gerrit.sshd.GerritServerSession", Level.WARN)
|
||||
|
||||
// Silence non-critical messages from mime-util.
|
||||
.put("eu.medsea.mimeutil", Level.WARN)
|
||||
|
||||
// Silence non-critical messages from openid4java.
|
||||
.put("org.apache.xml", Level.WARN)
|
||||
.put("org.openid4java", Level.WARN)
|
||||
.put("org.openid4java.consumer.ConsumerManager", Level.FATAL)
|
||||
.put("org.openid4java.discovery.Discovery", Level.ERROR)
|
||||
.put("org.openid4java.server.RealmVerifier", Level.ERROR)
|
||||
.put("org.openid4java.message.AuthSuccess", Level.ERROR)
|
||||
|
||||
// Silence non-critical messages from c3p0 (if used).
|
||||
.put("com.mchange.v2.c3p0", Level.WARN)
|
||||
.put("com.mchange.v2.resourcepool", Level.WARN)
|
||||
.put("com.mchange.v2.sql", Level.WARN)
|
||||
|
||||
// Silence non-critical messages from apache.http.
|
||||
.put("org.apache.http", Level.WARN)
|
||||
|
||||
// Silence non-critical messages from Jetty.
|
||||
.put("org.eclipse.jetty", Level.WARN)
|
||||
|
||||
// Silence non-critical messages from JGit.
|
||||
.put("org.eclipse.jgit.transport.PacketLineIn", Level.WARN)
|
||||
.put("org.eclipse.jgit.transport.PacketLineOut", Level.WARN)
|
||||
.put("org.eclipse.jgit.internal.storage.file.FileSnapshot", Level.WARN)
|
||||
.put("org.eclipse.jgit.util.FS", Level.WARN)
|
||||
.build();
|
||||
|
||||
private static Level getGerritLogLevel() {
|
||||
String value = Strings.nullToEmpty(System.getenv("GERRIT_LOG_LEVEL"));
|
||||
if (value.isEmpty()) {
|
||||
value = Strings.nullToEmpty(System.getProperty("gerrit.logLevel"));
|
||||
}
|
||||
return Level.toLevel(value, Level.INFO);
|
||||
}
|
||||
|
||||
public static void configureLogging() {
|
||||
LogManager.resetConfiguration();
|
||||
|
||||
PatternLayout layout = new PatternLayout();
|
||||
layout.setConversionPattern("%-5p %c %x: %m%n");
|
||||
|
||||
ConsoleAppender dst = new ConsoleAppender();
|
||||
dst.setLayout(layout);
|
||||
dst.setTarget("System.err");
|
||||
dst.setThreshold(Level.DEBUG);
|
||||
dst.activateOptions();
|
||||
|
||||
Logger root = LogManager.getRootLogger();
|
||||
root.removeAllAppenders();
|
||||
root.addAppender(dst);
|
||||
|
||||
LOG_LEVELS.entrySet().stream().forEach(e -> getLogger(e.getKey()).setLevel(e.getValue()));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user