`

a application of log4j

    博客分类:
  • java
阅读更多
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

 

分享到:
评论

相关推荐

    erlang日志应用log4erl(非sasl)

    - Support for a log formatter (similar to Layouts in Log4J) - Support for console log - Support for smtp formatter - Support for XML logs - Support for syslog - Support for changing format and level ...

    Java - J2EE Job Interview Companion.pdf

    **Ease of Use:** Since logging is rarely the main focus of an application, the Log4j API strives to be simple to understand and use. Its straightforward configuration and setup process make it easy ...

    tomcat-juli.jar,tomcat-juli-adapters.jar,log4j-1.2.17.jar tomcat 8日志输出

    Tomcat8下使用Log4j接管生成日志文件,按天存放,日志转换成json格式 亲测可用,日志格式如下: {"time":"2017-09-06 10:24:48,356","logtype":"INFO","loginfo":"org.apache.catalina.startup.HostConfig:Deploying...

    Java应用日志框架TNT4J.zip

    TNT4J是一个改进Log4J新的开源Java应用日志框架。用于应用程序活动的跟踪、相关性检查、诊断,可以跨多个应用程序,运行时,服务器,地理的位置。这个API是专门用以解决分布式,并发,多线程,多用户应用,包括活动...

    Java框架知识点学习笔记

    在Java开发中,框架的应用是不可或缺的,这里我们聚焦于Java框架的学习,特别是Spring和相关日志管理的Log4j。下面将详细讲解这些知识点。 首先,Log4j是Java中广泛使用的日志记录框架,它允许开发者自定义日志级别...

    Print-Images-Java.rar_editing_physical

    LOG4J 1.2.13 or higher This is a simple image editing and printing application. The distribution is composed of the following directories: The distribution directory () The Distribution directory ...

    Oracle9i安装OCS4J.properties问题解决

    实际上,`OCS4J.properties.tmp`文件用于定义Oracle Cluster Synchronization Services (CSS)的一些关键参数,而CSS是Oracle Real Application Clusters (RAC)的重要组成部分之一。 ##### 2.2 文件的作用 - **...

    JLink_Windows_V648.zip

    DLL: When using J-Trace PRO with IAR EWARM a "failed to allocate x bytes of memory" error could occur. Fixed. DLL: Windows: Renesas RX: When using FINE interface and disabling ongoining debug mode on ...

    ASP.NET+MVC+ENTLIB+LOG4NET+IOC

    **Log4Net** 是一个开源的日志框架,适用于.NET环境,灵感来源于Java的Log4j。它允许开发者定义不同级别的日志(如DEBUG、INFO、WARN、ERROR和FATAL),并将日志信息写入不同的目标,如控制台、文件、数据库等。通过...

    BURNINTEST--硬件检测工具

    - Note: We have seen a report of the Video Playback failing (crash) due to a faulty video codec, ffdshow.ax. If you are using this we suggest you try a different Video file and codec. Release ...

    SpringMVC项目(MongoDB3.0)

    在本项目中,我们主要探讨的是如何在Spring MVC框架下集成MongoDB 3.0数据库,同时利用log4j2进行日志管理,并且运用注解进行程序的简化与优化。下面将对这些核心知识点进行详细阐述。 首先,Spring MVC是Spring...

    Apache Geronimo 2.1_ Quick Reference.pdf

    Table of Contents Preface 1 Chapter 1: Getting Started with Geronimo ...Logging using the ApplicationLog4jConfigurationGBean 332 Using the Java Logging API 333 Using the SLF4j logging adapter ...

    hibernate_reference.pdf

    - **Logging**: Discusses how to configure logging frameworks like Log4j or SLF4J for Hibernate. - **Implementing a Naming Strategy**: Explains how to customize the naming strategy used by Hibernate ...

    VB编程资源大全(英文源码 控制)

    Access, Excel 95/97 (VBA) Windows Scripting Host (cscript / wscript) Perl 5.x for Win32 Inprise Delphi 4 Microsoft Visual C++ 4.x/5.x/6.x Microsoft Visual J++ 1.x/6.0 (Java) &lt;END&gt;...

    spring-boot-reference.pdf

    19.2. Running as a Packaged Application 19.3. Using the Maven Plugin 19.4. Using the Gradle Plugin 19.5. Hot Swapping 20. Developer Tools 20.1. Property Defaults 20.2. Automatic Restart 20.2.1. ...

    servlet2.4doc

    A B C D E F G H I J L P R S U V -------------------------------------------------------------------------------- A addCookie(Cookie) - Method in class javax.servlet.http.HttpServletResponseWrapper ...

    -Project_DA_Java_EN_Come_to_the_Rescue_of_a_Java_Application-master

    9. **日志和调试**:了解如何使用Log4j、SLF4J等日志框架,以及IDE中的调试工具,帮助定位和解决问题。 10. **测试**:可能包含JUnit或其他测试框架的使用,确保代码的质量和稳定性。 11. **版本控制**:由于提到...

    Expert one-on-one J2EE Design and Development.part1

    What you need to use this book To run the samples in this book you will need: o Java 2 Platform, Standard Edition SDK v...o Apache Log4j 1.2 o An implementation of the JSP Standard Tag Library (JSTL) 1.0

Global site tag (gtag.js) - Google Analytics