No extra logging in the console with log4j

2019-09-18 06:09发布

问题:

I'm new at this logging stuff and i want see logging from spring to see all the beans created.
So I want to try logging with log4j but no extra loggin appear in the console.
I follow some example to make my logging.
Here's my configuration :

pom.xml

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.0-rc2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.0-rc2</version>
    </dependency>   

log4j.properies

# Root logger option
log4j.rootLogger=INFO, stdout

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

log4j.logger.org.springframework=INFO,stdout

My Controller Class

//Import log4j classes.
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

@Transactional
@Controller
public class Inscription {

    ...

    static final Logger logger = LogManager.getLogger(Inscription.class);

    ...

    @RequestMapping(value="/") 
    public String Test(ModelMap model) {

        ...


        //log it via log4j
        logger.debug(model);

        ...

    }
}

is the log4j.property loaded ? (i put it twice to make sure)

what mean this logger.debug(model); is it requierd to make logging or just log4j.property is enough ?

回答1:

I have created a log4j2.xml in classpath (src folder) it will detect automatically by log4j

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Appenders>
        <Console name="CONSOLE" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
    </Appenders>
    <Loggers>

        <Root level="ALL">
            <AppenderRef ref="CONSOLE"/>
        </Root>

        <Logger name="controller" level="ALL" >
            <AppenderRef ref="CONSOLE"/>
        </Logger>

    </Loggers>
</Configuration>