如何在java程序中打log
2016-11-14 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
Java的Log是一个非常强大的写日志的工具包,程序员可以随时把一些想要记录的信息输出到本地文件,可以用于观察程序运行状况等,代码如下:
import org.apache.log4j.FileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.Level;
import org.apache.log4j.SimpleLayout;
public class ApacheLog {
public static void main(String args[]) {
Logger logger = Logger.getLogger(ApacheLog.class);
SimpleLayout layout = new SimpleLayout();
FileAppender appender = null;
try {
appender = new FileAppender(layout, "myApacheLog.log", false);
} catch (Exception e) {
}
logger.addAppender(appender);
// Set the logger level to Level.INFO
logger.setLevel(Level.INFO);
// This request will be disabled since Level.DEBUG < Level.INFO.
logger.debug("This is debug.");
// These requests will be enabled.
logger.info("This is an info.");
logger.warn("This is a warning.");
logger.error("This is an error.");
logger.fatal("This is a fatal error.");
}
}
import org.apache.log4j.FileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.Level;
import org.apache.log4j.SimpleLayout;
public class ApacheLog {
public static void main(String args[]) {
Logger logger = Logger.getLogger(ApacheLog.class);
SimpleLayout layout = new SimpleLayout();
FileAppender appender = null;
try {
appender = new FileAppender(layout, "myApacheLog.log", false);
} catch (Exception e) {
}
logger.addAppender(appender);
// Set the logger level to Level.INFO
logger.setLevel(Level.INFO);
// This request will be disabled since Level.DEBUG < Level.INFO.
logger.debug("This is debug.");
// These requests will be enabled.
logger.info("This is an info.");
logger.warn("This is a warning.");
logger.error("This is an error.");
logger.fatal("This is a fatal error.");
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询