在项目开发时,日志文件可以替我们自动记录项目运行的情况,帮助我们及时定位到错误的位置。那么如何在项目中应用Log4J呢,下面是一些简单的例子。
(1)首先,我们需要下载Apache log4j 1.2.17 (zip):下载地址:http://logging.apache.org/log4j/1.2/download.html点击打开链接。在项目中添加该jar包。
(2)Java代码如下:
a)
import com.foo.Bar; // Import log4j classes. import org.apache.log4j.Logger; import org.apache.log4j.BasicConfigurator; public class MyApp { // Define a static logger variable so that it references the // Logger instance named "MyApp". static Logger logger = Logger.getLogger(MyApp.class); public