1. Java 日志体系主要分为Facade 和具体的日志实现。当前主流的Facade主要由
- apache-comnons-logging
- slf4j
- log4j2
2. 主流的日志实现由以下几种
- java.util.logging
- log4j
- logback
3. 参考
- http://my.oschina.net/xianggao/blog/515381
- http://www.ibm.com/developerworks/cn/java/j-lo-practicelog/
4. 具体的代码
4.1 JDK自带
import java.io.IOException; import java.io.InputStream; import java.util.logging.Level; import java.util.logging.LogManager; import java.util.logging.Logger; /** * Created by zhaoyu on 16-7-11. */ public class JdkLogging { private static Logger logger = null; private JdkLogging() {} public static Logger getLogger(String name) { InputStream stream = JdkLogging.class.getResourceAsStream("/logging.properties"); if (logger == null) { try { LogManager.getLogManager().readConfiguration(stream); } catch (IOException e) { e.printStackTrace(); } finally { try { stream.close(); } catch (IOException e) { e.printStackTrace(); } } logger = Logger.getLogger(name); } return logger; } public static void main(String[] args) { Logger logger = JdkLogging.getLogger(JdkLogging.class.getName()); //Logger logger = Logger.getLogger(JdkLogging.class.getName(), null); // logger.setLevel(Level.CONFIG); logger.severe("java utils logging level severe."); logger.warning("java utils logging level warning."); logger.info("java utils logging level info."); logger.config("java utils logging level config."); logger.fine("java utils logging level fine."); logger.finer("java utils logging level finer."); logger.finest("java utils logging level finest."); } }
4.2 log4j
import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; import org.apache.log4j.helpers.Loader; /** * Created by zhaoyu on 16-7-11. */ public class Log4jLogging { private static final Logger LOGGER = Logger.getLogger(Log4jLogging.class); public static void main(String[] args) { LOGGER.info("this is log4j info log."); LOGGER.error("this is log4j error log."); // load log4j.properties //PropertyConfigurator.configure(Log4jLogging.class.getResource("/log4jx.properties")); PropertyConfigurator.configure(Loader.getResource("log4jx.properties")); if (LOGGER.isTraceEnabled()) { LOGGER.trace("this is log4j trace log."); } } }
4.3 log4j2
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; /** * Created by zhaoyu on 16-7-11. */ public class Log4j2Logging { private static final Logger LOGGER = LogManager.getLogger(Log4j2Logging.class); public static void main(String[] args) { LOGGER.info("log4j2 info"); LOGGER.debug("log4j2 debug"); } }
4.4 apache-commons-logging
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * Created by zhaoyu on 16-7-11. */ public class ApacheCommonsLog { private static final Log LOG = LogFactory.getLog(ApacheCommonsLog.class); public static void main(String[] args) { LOG.info("apache commons log."); } }
4.5 logback 与slf4j
import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Created by zhaoyu on 16-7-11. */ public class Slf4jLogging { private static final Logger LOGGER = LoggerFactory.getLogger(Slf4jLogging.class); public static void main(String[] args) { // logback LOGGER.info("logback info log"); LOGGER.debug("logback debug log"); } }