Skip to content

exceptions during the preparation of the environment are not logged to log4j2 #7758

@shalom938

Description

@shalom938

its actually the same issue as #5271, I think the fix didn't work.
Log4J2LoggingSystem installs a filter that denies all and removes it in Log4J2LoggingSystem#initialize that is invoked in ApplicationEnvironmentPreparedEvent. but if an exception is raised during the preparation of the environment then the filter is not removed yet and the exception is lost. i got it when i had an exception in an EnvironmentPostProcessor.
you can run sample.actuator.log4j2.SampleActuatorLog4J2Application with this application.yml under resources.

---
spring:
    profiles:
         active: local

---
spring:
    profiles: local
a=test

but change the code of sample.actuator.log4j2.SampleActuatorLog4J2Application to look like that:

try {
	SpringApplication.run(SampleActuatorLog4J2Application.class, args);
}catch (Throwable e){
	LoggerFactory.getLogger(SampleActuatorLog4J2Application.class).error("error ",e);
}

this is actually how our code looks like, we don't always have a main class, we initialize contexts in various ways.
the exception is not logged.
only if i remove the filter the exception is logged:

try {
	SpringApplication.run(SampleActuatorLog4J2Application.class, args);
}catch (Throwable e){
	try {
		Filter filter = ((LoggerContext) LogManager.getContext(false)).getConfiguration().getFilter();
		if (filter != null) {
			((LoggerContext) LogManager.getContext(false)).getConfiguration().removeFilter(filter);
		}
	}catch (Throwable t){
	}

	LoggerFactory.getLogger(SampleActuatorLog4J2Application.class).error("error ",e);
}

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions