1.引入Logger和Logger工厂类
2.声明logger
3.记录日志
//1. 引入slf4j接口的Logger和LoggerFactory
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class UserService {
//2. 声明一个Logger,这个是static的方式,我比较习惯这么写。
private final static Logger logger = LoggerFactory.getLogger(UserService.class);
public boolean verifyLoginInfo(String userName, String password) {
//3. log it,输出的log信息将会是:"Start to verify User [Justfly]
logger.info("Start to verify User [{}]", userName);
return false;
}
}
这里使用的是静态的logger对象,因为这样更符合语义,可以节省cpu节省内存,不支持注入
public void info(String msg); 输出msg信息
public void info(String format, Object arg); logger.info("开始导入配置文件[{}]","/somePath/config.properties");
public void info(String format, Object arg1, Object arg2); logger.info("开始从配置文件[{}]中读取配置项[{}]的值","/somePath/config.properties","maxSize");
public void info(String msg, Throwable t);logger.info("读取配置文件时出现异常",new FileNotFoundException("File not exists"));记录异常信息
关于分级的log参考:https://blog.csdn.net/zalan01408980/article/details/79653386