先引入Logger和LoggerFactory:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
声明logger作为成员变量,并且初始化:
private static Logger logger = LoggerFactory.getLogger(TestDemo.class);
在方法中使用:
logger.info("===发送消息通知===originDocKey:" + originDocKey);
测试代码:
package com.jawasoft.testDemo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class TestDemo { private static Logger logger = LoggerFactory.getLogger(TestDemo.class); @Test public void test4(){ logger.info("开始"); System.out.println("================================"); logger.info("结束"); } }
控制台打印日志如下:
2020-10-30 17:29:10 CST INFO com.jawasoft.testDemo.TestDemo 101 test4 - 开始[] ================================ 2020-10-30 17:29:10 CST INFO com.jawasoft.testDemo.TestDemo 103 test4 - 结束[]
还可以加error类型的日志,如下:
String fileName = fileEntity.getFileName(); if (null == fileName || ("").equals(fileName.trim())) { logger.error("No FileName Found."); throw new Exception("No FileName Found."); }