java中的日志配置

slf4j是java中常用的日志框架,有许多具体实现,比如slf4j-simple等。

一、maven配置

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.7.26</version>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-simple</artifactId>
  <version>1.7.26</version>
</dependency>

二、配置

在java同级目录下,增加 resources 目录,在其下新建文件 simplelogger.properties.  可以配置打印日志的时间戳等信息。

<!-- lang: java -->
#DEFAULT_LOG_LEVEL_KEY = org.slf4j.simpleLogger.defaultLogLevel
#SHOW_DATE_TIME_KEY = org.slf4j.simpleLogger.showDateTime
#DATE_TIME_FORMAT_KEY = org.slf4j.simpleLogger.dateTimeFormat
#SHOW_THREAD_NAME_KEY = org.slf4j.simpleLogger.showThreadName
#SHOW_LOG_NAME_KEY = org.slf4j.simpleLogger.showLogName
#SHOW_SHORT_LOG_NAME_KEY = org.slf4j.simpleLogger.showShortLogName
#LOG_FILE_KEY = org.slf4j.simpleLogger.logFile
#LEVEL_IN_BRACKETS_KEY = org.slf4j.simpleLogger.levelInBrackets
#WARN_LEVEL_STRING_KEY = org.slf4j.simpleLogger.warnLevelString

org.slf4j.simpleLogger.showDateTime = true
org.slf4j.simpleLogger.dateTimeFormat = HH:mm:ss:SSSS

 

三、使用

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public Class ExampleForLog {
    private Logger logger = LoggerFactory.getLogger(AndroidBaseCase.class);

    logger.info("xxx");  //输出:
}

  

 

posted @ 2021-08-17 13:32  myLittleGarden  阅读(615)  评论(0编辑  收藏  举报