No extra logging in the console with log4j

断了今生、忘了曾经 提交于 2019-12-12 01:54:45

问题


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>


来源:https://stackoverflow.com/questions/24478592/no-extra-logging-in-the-console-with-log4j

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!