11
11
import com .newrelic .agent .config .AgentConfig ;
12
12
import com .newrelic .agent .config .AgentConfigImpl ;
13
13
import com .newrelic .agent .config .AgentJarHelper ;
14
+ import com .newrelic .api .agent .NewRelic ;
14
15
import org .apache .logging .log4j .Level ;
16
+ import org .apache .logging .log4j .core .config .Configurator ;
17
+ import org .apache .logging .log4j .core .config .builder .api .ConfigurationBuilder ;
18
+ import org .apache .logging .log4j .core .config .builder .api .ConfigurationBuilderFactory ;
19
+ import org .apache .logging .log4j .core .config .builder .impl .BuiltConfiguration ;
15
20
16
21
import java .io .File ;
22
+ import java .net .MalformedURLException ;
17
23
import java .net .URL ;
18
24
import java .text .MessageFormat ;
19
25
import java .util .HashMap ;
@@ -78,27 +84,6 @@ private Log4jLogger initializeRootLogger(String name) {
78
84
// and we want to clear any other log4j system properties
79
85
clearAllLog4jSystemProperties (systemProps );
80
86
81
- URL log4jConfigXmlUrl = null ;
82
- if (jarFileName .endsWith (".jar" )) {
83
- // it isn't enough to specify the jar, we have to specify the path within the jar
84
- log4jConfigXmlUrl = new URL (new StringBuilder ("jar:file:" )
85
- .append (jarFileName )
86
- .append ("!" )
87
- .append (AGENT_JAR_LOG4J_CONFIG_FILE )
88
- .toString ());
89
- } else {
90
- // we likely have received a path to a set of class files (this happens when running tests)
91
- try {
92
- // guava's Resources class is usually smart enough to figure out where to find the log4j2.xml file
93
- log4jConfigXmlUrl = Resources .getResource (this .getClass (), AGENT_JAR_LOG4J_CONFIG_FILE );
94
- } catch (IllegalArgumentException iae ) {
95
- // fallback on path
96
- log4jConfigXmlUrl = new File (jarFileName ).toURI ().toURL ();
97
- }
98
- }
99
-
100
- System .setProperty (CONFIG_FILE_PROP , log4jConfigXmlUrl .toString ());
101
- System .setProperty (LEGACY_CONFIG_FILE_PROP , log4jConfigXmlUrl .toString ());
102
87
// Log4j won't be able to find log4j-provider.properties because it isn't on the classpath (it's in our agent) so this sets it manually
103
88
System .setProperty (CONTEXT_FACTORY_PROP , "org.apache.logging.log4j.core.impl.Log4jContextFactory" );
104
89
@@ -111,6 +96,16 @@ private Log4jLogger initializeRootLogger(String name) {
111
96
System .setProperty (LEGACY_CLASSLOADER_PROP , "true" );
112
97
System .setProperty (CLASSLOADER_PROP , "true" );
113
98
99
+ // This is in place in case we need to revert the programmatic initialization of our logger to
100
+ // use the log4j XML config file. This has to use a system property or env var because the config
101
+ // service is not properly bootstrapped yet.
102
+ if (Boolean .getBoolean ("newrelic.config.agent_root_logger_init_with_file" ) ||
103
+ Boolean .parseBoolean (System .getenv ("NEW_RELIC_AGENT_ROOT_LOGGER_INIT_WITH_FILE" ))) {
104
+ initLog4jViaFile (jarFileName );
105
+ } else {
106
+ initLog4jViaConfigurationBuilder ();
107
+ }
108
+
114
109
try {
115
110
logger = createRootLogger (name );
116
111
} finally {
@@ -275,6 +270,39 @@ private boolean canWriteLogFile(String logFileName) {
275
270
}
276
271
}
277
272
273
+ private void initLog4jViaConfigurationBuilder () {
274
+ ConfigurationBuilder <BuiltConfiguration > builder = ConfigurationBuilderFactory .newConfigurationBuilder ();
275
+ builder .setConfigurationName ("Default" );
276
+ builder .add (builder .newRootLogger (Level .INFO ));
277
+ builder .setShutdownHook ("disable" );
278
+ builder .add (builder .newLogger ("com.newrelic.agent.deps.org.reflections" , Level .OFF ));
279
+ Configurator .initialize (builder .build ());
280
+ }
281
+
282
+ private void initLog4jViaFile (String jarFileName ) throws MalformedURLException {
283
+ URL log4jConfigXmlUrl = null ;
284
+ if (jarFileName .endsWith (".jar" )) {
285
+ // it isn't enough to specify the jar, we have to specify the path within the jar
286
+ log4jConfigXmlUrl = new URL (new StringBuilder ("jar:file:" )
287
+ .append (jarFileName )
288
+ .append ("!" )
289
+ .append (AGENT_JAR_LOG4J_CONFIG_FILE )
290
+ .toString ());
291
+ } else {
292
+ // we likely have received a path to a set of class files (this happens when running tests)
293
+ try {
294
+ // guava's Resources class is usually smart enough to figure out where to find the log4j2.xml file
295
+ log4jConfigXmlUrl = Resources .getResource (this .getClass (), AGENT_JAR_LOG4J_CONFIG_FILE );
296
+ } catch (IllegalArgumentException iae ) {
297
+ // fallback on path
298
+ log4jConfigXmlUrl = new File (jarFileName ).toURI ().toURL ();
299
+ }
300
+ }
301
+
302
+ System .setProperty (CONFIG_FILE_PROP , log4jConfigXmlUrl .toString ());
303
+ System .setProperty (LEGACY_CONFIG_FILE_PROP , log4jConfigXmlUrl .toString ());
304
+ }
305
+
278
306
@ Override
279
307
public void addConsoleHandler () {
280
308
rootLogger .addConsoleAppender ();
0 commit comments