import java.util.logging.Level;
import java.util.logging.Logger;
/**
* JDK自带日志包
* 可以定义格式 可以存档 可以根据配置文件调整日志
*/
public class JDKLogger {
public static void main(String[] args) {
Logger logger = Logger.getLogger(JDKLogger.class.getName());
logger.setLevel(Level.INFO);
logger.info("this is a info msg");
logger.warning("this is a warn msg");
logger.severe("this is a error msg");
}
}
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* commons-logging
* 可以挂载不同的日志系统
* 默认使用log4j,再则JDK logger
*
* commons-logging-1.2.jar
*/
public class CommonsLogging {
public static void main(String[] args) {
Log logger = LogFactory.getLog(CommonsLogging.class);
logger.info("this is a info msg");
logger.warn("this is a warn msg");
logger.debug("this is a debug msg");
logger.error("this is a error msg");
logger.error("throw a error", new NullPointerException());
System.out.println(logger.isDebugEnabled());
}
}
import org.apache.log4j.Logger;
/**
* 单纯使用log4j
* 可以配合commons logging一起使用
*
* log4j-1.2.17.jar
*/
public class Log4jLogger {
public static void main(String[] args) {
Logger logger = Logger.getLogger(Log4jLogger.class);
logger.info("this is a info msg");
logger.warn("this is a warn msg");
logger.debug("this is a debug msg");
logger.error("this is a error msg");
logger.error("throw a error", new NullPointerException());
System.out.println(logger.isDebugEnabled());
}
}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* SLF4J --Simple Logging Facade for Java
* 只是一个接口 一个规范
* 和Commons logging接口类似
* log4j只是其中一个实现类,类似的还有logback等等
*
* slf4j-api-1.7.9.jar
* slf4j-log4j12-1.7.9.jar
* log4j-1.2.17.jar
*/
public class SLF4JLog4jLogger {
public static void main(String[] args) {
Logger logger = LoggerFactory.getLogger(SLF4JLog4jLogger.class);
logger.info("this is a info msg");
lo