a application of log4j

本文介绍了一种使用Log4j进行日志记录的方法,包括配置文件log4j.properties的具体设置,以及通过Java代码实现日志记录的过程。通过示例展示了如何自定义日志目录并设置日志级别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

log4j.rootLogger=WARN, stdout, logfile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=${log4jDIR}/dialOut.log
log4j.appender.logfile.append=true
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d [%c] - %m%n
log4j.appender.logfile.MaxFileSize=2MB
log4j.appender.logfile.MaxBackupIndex=3

The above code block is log4j.properties, you can save this file anywhere.

Look at the block, you can find a parameter:log4jDIR. This is a user define one. you could understand it at the following code block.

 

package com.rv.stresstest.logger;

import java.io.File;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.rv.stresstest.StressApplication;

public class RVAppLog {
	private static Logger logger;

	@SuppressWarnings("unchecked")
	public static Logger getLogger(Class clazz, Level level) {
		if (logger == null) {
			String path = StressApplication.getInstance().getRootPath();
			System.setProperty("log4jDIR", path+"logs"+File.separator);
			String property = path + "log4j.properties";
			PropertyConfigurator.configure(property);
			logger = Logger.getLogger(clazz);
			logger.setLevel(level);
		}
		return logger;
	}
}

 

In this block, I use a class to find the path. You can review this class in the following .

package com.rv.stresstest;

public class StressApplication {
	private static StressApplication a;

	private StressApplication() {

	}

	public static StressApplication getInstance() {
		if (a == null)
			a = new StressApplication();
		return a;
	}

	public String getClassRootPath() {
		try {
			String result = StressApplication.class.getResource("StressApplication.class").toString();
			int index = result.indexOf("classes");
			result = result.substring(0, index) + "classes";
			if (result.startsWith("jar")) {
				result = result.substring(10);
			} else if (result.startsWith("file")) {
				result = result.substring(6);
			}
			String os = System.getProperty("os.name");
			if (os.indexOf("Windows") >= 0)
				return result;
			else
				return "/" + result;
		} catch (Exception e) {
			return "";
		}
	}

	public String getRootPath() {
		String result = StressApplication.class.getResource("StressApplication.class").toString();
		int index = result.indexOf("WEB-INF");
		if (index == -1) {
			index = result.indexOf("bin");
		}
		result = result.substring(0, index);
		if (result.startsWith("jar")) {
			result = result.substring(10);
		} else if (result.startsWith("file")) {
			result = result.substring(6);
		}
		String os = System.getProperty("os.name");
		if (os.indexOf("Windows") >= 0)
			return result;
		else
			return "/" + result;
	}
}

 

At last, I will give guys a test.

package com.rv.stresstest.logger;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class TestLog {
	public static void main(String[] args) {
		Logger logger = RVAppLog.getLogger(TestLog.class, Level.DEBUG);
		for (int i = 0; i < 111; i++) {
			logger.debug(i);
		}
	}
}

The result log as the following.

 

2009-04-28 17:37:13,123 [com.rv.stresstest.logger.TestLog] - 0
2009-04-28 17:37:13,123 [com.rv.stresstest.logger.TestLog] - 1
2009-04-28 17:37:13,123 [com.rv.stresstest.logger.TestLog] - 2
……
2009-04-28 17:37:13,154 [com.rv.stresstest.logger.TestLog] - 110

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值