在项目中日志信息真的是不容忽视,我继续跟进我之前的项目,现在开始来添加日志了,首先看一下错误信息
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.
直接不看了,只看主要的,那为啥要贴出来?贴出来搜索引擎才能在你贴错误信息的时候能搜出我写的啊
直接看这一段,SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
他的意思是这个包SLF4J没有找到org.slf4j.impl.StaticLoggerBinder这个类,也就是说我们确少依赖,所以我们添加依赖就行了,我们在gradle里面加上这样的几段,等待gradle的更新,然后我们重启tomcat再来看看
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'
现在我们发现控制台的日志信息改了
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 Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
现在我们来看这一段log4j:WARN Please initialize the log4j system properly.
他的意思是要使用这个东西我们还需要对他进行配置,ok,我们现在就来配置他
我们在reresources文件夹下面新建一个 log4j.properties 文件,这个文件名是固定的,所以就不要想着改啦,文件地址也是classpath的地址,所以也不要想着变啦,当然你可以选择改掉它里面的配置去实现这样的功能
在里面加上下面的这些东西,这些只是一些很简单的基本的配置,这里我也不做详细的解释了,需要更详细的配置可以参考log4j详细配置,这篇文章对里面的配置写的很清楚,感兴趣的可以去看看
### 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
到这里我们的配置已经完成,我们需要对他进行测试才行,我就很懒,还是在我之前的Controller立面进行测试了,我现在是在跟进我的项目,实现这个功能和解决问题,大家如果感兴趣的话可以去GitHub下载我的项目GitHub传送门
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";
}
弄完之后,启动tomcat,敲入地址然后看控制台输出的日志信息
可以看到我写了四个日志输出但是只打印出来了两条信息,这个是因为配置的日志级别,我配置的是INFO,他只输出INFO和ERROR级别的日志
到现在为止我们的日志也就完成了,有什么不正确的欢迎前来指导
转载请标明出处:https://blog.csdn.net/qq_33733799/article/details/79899929