Fix logs when running tests from Eclipse

The system property for configuring the flogger log4j backend was not
set and as result of this the default system backend was used which
doesn't consider the log4j configuration and which uses a different log
format.

We must make sure that the system property is set before any class that
uses a FluentLogger is loaded. Since the logger field is usually a
static field loaded the class triggers the creation of the FluentLogger.
To create the FluentLogger Flogger needs to instantiate the logger
backend and to know which logger backend should be used Flogger reads
the system property. The logger backend is instantiated only once, and
then the system property is never read again. This means we must make
sure that the system property is set *before* the first class that uses
a FluentLogger is loaded.

For some tests (e.g. for the query tests) loading such a class happens
very early when config for the ConfigSuite is created. To make sure that
the system property is set before this code is invoked we set the system
property in a static code block in ConfigSuite that is executed as soon
as the ConfigSuite class is loaded.

Change-Id: Id9d8713f5cbb278cb37ea165ef362e2151c5e4cc
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2018-06-06 10:01:30 +02:00
parent f776f12ca0
commit 4dd376395c

View File

@@ -104,6 +104,14 @@ import org.junit.runners.model.InitializationError;
* field annotated with {@code @ConfigSuite.Name}.
*/
public class ConfigSuite extends Suite {
private static final String FLOGGER_BACKEND_PROPERTY = "flogger.backend_factory";
static {
System.setProperty(
FLOGGER_BACKEND_PROPERTY,
"com.google.common.flogger.backend.log4j.Log4jBackendFactory#getInstance");
}
public static final String DEFAULT = "default";
@Target({METHOD})