Add log to SpringMVC project

    The log information in the project really cannot be ignored. I continue to follow up on my previous projects, and now I start to add logs. First, look at the error message

11-Apr-2018 17:24:05.786 INFO [RMI TCP Connection(3)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

MLog initialization issue: slf4j found no binding or threatened to use its (dangerously silent) NOPLogger. We consider the slf4j library not found.

    Don't read it directly, just look at the main ones, so why post it? Post it so that the search engine can find what I wrote when you post the wrong message

    Look directly at this paragraph, SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".

    What he means is that this package SLF4J did not find the class org.slf4j.impl.StaticLoggerBinder, that is to say, we do not rely on it very much, so we just need to add dependencies. We add these paragraphs in gradle and wait for the update of gradle. Then we restart tomcat and take a look again

    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
    compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.25'
    compile group: 'org.slf4j', name: 'jcl-over-slf4j', version: '1.7.25'
    compile group: 'org.slf4j', name: 'jul-to-slf4j', version: '1.7.25'

    Now we find that the log information of the console has changed

11-Apr-2018 17:31:37.126 INFO [RMI TCP Connection(3)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.

log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.

log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

    Now let's look at this log4j: WARN Please initialize the log4j system properly.

    What he means is that we need to configure it to use this thing, ok, let’s configure it now

    We create a new log4j.properties file under the reresources folder . The file name is fixed, so don’t think about changing it. The file address is also the address of the classpath, so don’t think about changing it. Of course, you can choose to change it The configuration inside to achieve such a function

    Add the following things in it. These are just some very simple basic configurations. I will not explain them in detail here. If you need more detailed configurations, you can refer to the detailed configuration of log4j. This article writes very well about the configurations inside . Clear, you can go and see if you are interested

### set log levels ###
log4j.rootLogger=INFO , console , debug , error
### console ###
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-d{yyyy-MM-dd HH\:mm\:ss} [%p]-[%c] %m%n
### log file ###
log4j.appender.debug=org.apache.log4j.DailyRollingFileAppender
log4j.appender.debug.File=log.log
log4j.appender.debug.Append=true
log4j.appender.debug.Threshold=INFO
log4j.appender.debug.layout=org.apache.log4j.PatternLayout
log4j.appender.debug.layout.ConversionPattern=%-d{yyyy-MM-dd HH\:mm\:ss} [%p]-[%c] %m%n
### exception ###
log4j.appender.error=org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.File=error-log.log
log4j.appender.error.Append=true
log4j.appender.error.Threshold=ERROR
log4j.appender.error.layout=org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern=%-d{yyyy-MM-dd HH\:mm\:ss} [%p]-[%c] %m%n
###需要声明,然后下方才可以使druid sql输出,否则会抛出log4j.error.key not found
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{ISO8601} %l %c%n%p: %m%n
### druid sql ###
log4j.logger.druid.sql=warn,stdout
log4j.logger.druid.sql.DataSource=warn,stdout
log4j.logger.druid.sql.Connection=warn,stdout
log4j.logger.druid.sql.Statement=warn,stdout
log4j.logger.druid.sql.ResultSet=warn,stdout

    So far our configuration has been completed, we need to test it, I am lazy, I still tested it on my previous Controller facade, I am now following up my project, implementing this function and solving problems , if you are interested, you can go to GitHub to download my project GitHub Portal

    import org.apache.log4j.Logger;//导入这个包
    private final static Logger logger = Logger.getLogger (IndexController.class);//这个里面传入的类就是本类
    @RequestMapping("home")
    public String home() {
        //调用什么方法就是表示日志的级别
        logger.info("this.info-log");
        logger.error("this.error-log");
        logger.debug("this.debug-log");
        logger.trace("this.trace-log");
        return "home";
    }

    After finishing, start tomcat, type in the address and see the log information output by the console


    You can see that I wrote four log outputs but only printed two messages. This is because of the configured log level. I configured INFO, which only outputs INFO and ERROR level logs.

    So far our log has been completed, if there is anything wrong, welcome to come and guide

    Reprint please indicate the source: https://blog.csdn.net/qq_33733799/article/details/79899929


Guess you like

Origin blog.csdn.net/qq_33733799/article/details/79899929