Skip to content

Log4j2 prints error when log4j2-spring.xml is used #4809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
cforce opened this issue Dec 18, 2015 · 10 comments
Closed

Log4j2 prints error when log4j2-spring.xml is used #4809

cforce opened this issue Dec 18, 2015 · 10 comments
Assignees
Milestone

Comments

@cforce
Copy link

cforce commented Dec 18, 2015

According to doc https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html the name shall work with "log4j2-spring.xml". but only does with "log4j2.xml"

as proposed from spring-cloud/spring-cloud-commons#72 (comment)

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 18, 2015
@snicoll
Copy link
Member

snicoll commented Dec 21, 2015

It does work but logs this error on startup

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

I assume that's what confused you in the first place, I don't know if this is new or not at this point but that extra statement is the only problem as far as I can see.

@snicoll snicoll added unknown and removed status: waiting-for-triage An issue we've not yet triaged labels Dec 21, 2015
@philwebb philwebb removed the unknown label Aug 30, 2016
@yairogen
Copy link

yairogen commented Sep 5, 2016

Any updates? I could only suppress this error print by adding:

-Dlog4j.configurationFile=log4j2-spring.xml

@philwebb philwebb changed the title No log4j2 configuration file found if name is log4j2-spring.xml Log4j2 prints error when log4j2-spring.xml is used Sep 6, 2016
@RTM945
Copy link

RTM945 commented Nov 4, 2016

get the same problem

@RestController
@EnableAutoConfiguration
@SpringBootApplication

public class Example {

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }


    public static void main(String[] args) throws Exception {
        new SpringApplicationBuilder(Example.class).listeners(new LoadAdditionalProperties()).run(args);
    }

    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory("/project", 9000);
        factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/notfound.html"));
        return factory;
    }

}

//copy from https://github.com/spring-projects/spring-boot/issues/6220
@Component
public class LoadAdditionalProperties implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {

    private ResourceLoader loader = new DefaultResourceLoader();

    @Override
    public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
        try {
            Resource resource = loader.getResource("file:conf/project/application.yml");
            PropertySource<?> propertySource = new PropertySourcesLoader().load(resource);
            event.getEnvironment().getPropertySources().addLast(propertySource);
        } catch (IOException ex) {
            throw new IllegalStateException(ex);
        }
    }

}

My application.yml:

logging: 
    config: file:conf/project/log4j2.xml

my log4j.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="60">
    <Appenders>
        <Console name="Console-Appender" target="SYSTEM_OUT">
            <PatternLayout>
                <pattern>
                    %d{ABSOLUTE} %5p %t %c{2}:%L - %m%n
                </pattern>>
            </PatternLayout>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console-Appender"/>
        </Root>
    </Loggers>
</Configuration>

run with this error on startup:

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

and spring uses default logger like:

INFO 50608 --- [ main] c.w.a.Example : Starting Example on....

then I set properties to springApplicationBuilder:

new SpringApplicationBuilder(Example.class)
.properties("logging.config=file:conf/project/log4j2.xml")
.listeners(new LoadAdditionalProperties()).run(args);

log4j2 works but also the same error on startup:

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

@snicoll snicoll added the for: team-attention An issue we'd like other members of the team to review label Nov 4, 2016
@philwebb philwebb self-assigned this Nov 23, 2016
@wilkinsona
Copy link
Member

If you don't want to use a system property, you can suppress the error with a file named log4j2.component.properties with the contents:

log4j.configurationFile=log4j2-spring.xml

@wilkinsona
Copy link
Member

@philwebb Perhaps we could include such a properties file in our log4j2 starter? The value can be a comma-separated list so it could be:

log4j.configurationFile=log4j2.xml,log4j2-spring.xml

@philwebb
Copy link
Member

@wilkinsona That would be nice. I'll target this to 1.5 to investigate since it seems to be causing grief for people.

@philwebb philwebb removed the for: team-attention An issue we'd like other members of the team to review label Nov 23, 2016
@philwebb philwebb added this to the 1.5.0 RC1 milestone Nov 23, 2016
@philwebb philwebb modified the milestones: 1.5.0 RC1, 1.5.0 M1, 1.5.0 Dec 20, 2016
@yeta-io-ceo
Copy link

Hello,

After this patch my log4j2.xml project file configuration is ignored by spring, I see nothing in console. Only after rename log4j2.xml to log4j2-spring.xml it started work again. So, it's looks like regression introduced by this patch.

@philwebb
Copy link
Member

@yeta-io Thanks, we discovered this ourselves today (see #7783). I've got a different fix I'm about to commit.

@philwebb
Copy link
Member

@yeta-io If you've got time to test the latest snapshot, I'd appreciate it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants