??xml version="1.0" encoding="utf-8" standalone="yes"?>成人一区二区三区中文字幕,第一福利在线,亚洲一区二区三区四区在线免费观看http://www.aygfsteel.com/aldreamlau/category/27235.htmlmezh-cnMon, 12 Nov 2007 22:04:13 GMTMon, 12 Nov 2007 22:04:13 GMT60eclipse Junit试向导http://www.aygfsteel.com/aldreamlau/articles/159974.htmlaldreamaldreamMon, 12 Nov 2007 07:10:00 GMThttp://www.aygfsteel.com/aldreamlau/articles/159974.htmlhttp://www.aygfsteel.com/aldreamlau/comments/159974.htmlhttp://www.aygfsteel.com/aldreamlau/articles/159974.html#Feedback0http://www.aygfsteel.com/aldreamlau/comments/commentRss/159974.htmlhttp://www.aygfsteel.com/aldreamlau/services/trackbacks/159974.html 原文出处Q?a >http://www.laliluna.de/assets/tutorials/junit-testing-en.pdf

 

JUnit is a simple Java testing framework to write tests for you Java application. This tutorial gives you an overview of the features of JUnit and shows a little example how you can write tests for your Java application.

General

Author:

Sascha Wolski

Sebastian Hennebrueder

http://www.laliluna.de/tutorials.html ? Tutorials for Struts, EJB, xdoclet and eclipse.


Date:

April, 12 2005


Software:

Eclipse 3.x

Junit 2.x


Source code:

http://www.laliluna.de/assets/tutorials/junit-testing-source.zip

PDF Version

http://www.laliluna.de/assets/tutorials/junit-testing-en.pdf

What is JUnit

JUnit is a simple open source Java testing framework used to write and run repeatable automated tests. It is an instance of the xUnit architecture for unit testing framework. Eclipse supports creating test cases and running test suites, so it is easy to use for your Java applications.

JUnit features include:

  • Assertions for testing expected results

  • Test fixtures for sharing common test data

  • Test suites for easily organizing and running tests

  • Graphical and textual test runners

What is a test case

A test case is a class which holds a number of test methods. For example if you want to test some methods of a class Book you create a class BookTest which extends the JUnit TestCase class and place your test methods in there.

How you write and run a simple test

  1. Create a subclass of TestCase:


public class BookTest extends TestCase{ 
//..
}


  1. Write a test method to assert expected results on the object under test:

Note: The naming convention for a test method is testXXX()

public void testCollection() {
Collection collection = new ArrayList();
assertTrue(collection.isEmpty());
}


  1. Write a suite() method that uses reflection to dynamically create a test suite containing all the testXXX() methods:


public static Test suite(){
return new TestSuite(BookTest.class);
}



  1. Activate the JUnit view in Eclipse (Window > Show View > Other.. > Java > JUnit).




You find the JUnit tab near the Package Explorer tab. You can change the position of the tab by drag and drop it.




  1. Right click on the subclass of TestCase and choose Run > JUnit Test to run the test.

Using a test fixture

A test fixture is useful if you have two or more tests for a common set of objects. Using a test fixture avoids duplicating the test code necessary to initialize and cleanup those common objects for each test.

To create a test fixture, define a setUp() method that initializes common object and a tearDown() method to cleanup those objects. The JUnit framework automatically invokes the setUp() method before a each test is run and the tearDown() method after each test is run.

The following test uses a test fixture:

public class BookTest2 extends TestCase {

private Collection collection;

protected void setUp() {
collection = new ArrayList();
}

protected void tearDown() {
collection.clear();
}

public void testEmptyCollection(){
assertTrue(collection.isEmpty());
}
}

Dynamic and static way of running single tests

JUnit supports two ways (static and dynamic) of running single tests.

In static way you override the runTest() method inherited form TestCase class and call the desired test case. A convenient way to do this is with an anonymous inner class.

Note: Each test must be given a name, so you can identify it if it fails.

TestCase test = new BookTest("equals test") {
public void runTest() {
testEquals();
}
};



The dynamic way to create a test case to be run uses reflection to implement runTest. It assumes the name of the test is the name of the test case method to invoke. It dynamically finds and invokes the test method. The dynamic way is more compact to write but it is less static type safe. An error in the name of the test case goes unnoticed until you run it and get a NoSuchMethodException. We leave the choice of which to use up to you.

TestCast test = new BookTest("testEquals");

What is a TestSuite

If you have two tests and you'll run them together you could run the tests one at a time yourself, but you would quickly grow tired of that. Instead, JUnit provides an object TestSuite which runs any number of test cases together. The suite method is like a main method that is specialized to run tests.

Create a suite and add each test case you want to execute:

public static void suite(){
TestSuite suite = new TestSuite();
suite.addTest(new BookTest("testEquals"));
suite.addTest(new BookTest("testBookAdd"));
return suite;
}



Since JUnit 2.0 there is an even simpler way to create a test suite, which holds all testXXX() methods. You only pass the class with the tests to a TestSuite and it extracts the test methods automatically.

Note: If you use this way to create a TestSuite all test methods will be added. If you do not want all test methods in the TestSuite use the normal way to create it.

Example:

public static void suite(){
return new TestSuite(BookTest.class);
}

A little example

Create a new Java project named JUnitExample.

Add a package de.laliluna.tutorial.junitexample where you place the example classes and a package test.laliluna.tutorial.junitexample where you place your test classes.

The class Book

Create a new class Book in the package de.laliluna.tutorial.junitexample.

Add two properties title of type String and price of type double.

Add a constructor to set the two properties.

Provide a getter- and setter-method for each of them.

Add a method trunk for a method equals(Object object) which checks if the object is an instance of the class Book and the values of the object are equal. The method return a boolean value.

Note: Do not write the logic of the equals(..) method, we do it after finish creating the test method.

The following source code shows the class Book.

public class Book {

private String title;
private double price;

/**
* Constructor
*
* @param title
* @param price
*/
public Book(String title,
double price) {
this.title = title;
this.price = price;
}

/**
* Check if an object is an instance of book
* and the values of title and price are equal
* then return true, otherwise return false
*/
public boolean equals(Object object) {

return false;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}
}

The test case BookTest

Create a new test case BookTest in the package test.laliluna.tutorial.junitexample Right click on the package and choose New > JUnit Test Case.

In the wizard choose the methods stubs setUp(), tearDown() and constructor().






The following source code shows the class BookTest

public class BookTest extends TestCase {

/**
* setUp() method that initializes common objects
*/
protected void setUp() throws Exception {
super.setUp();
}

/**
* tearDown() method that cleanup the common objects
*/
protected void tearDown() throws Exception {
super.tearDown();
}

/**
* Constructor for BookTest.
* @param name
*/
public BookTest(String name) {
super(name);
}

}



Now we want to write a test for the equals(..) method of the class Book. We provide three private properties, book1, book2 and book3 of type Book.

private Book book1;
private Book book2;
private Book book3;



Within the setUp() method we initializes the three properties with some values. Property book1 and book3 are the same.

protected void setUp() throws Exception {
super.setUp();
book1 = new Book("ES", 12.99);
book2 = new Book("The Gate", 11.99);
book3 = new Book("ES", 12.99);
}



Within the tearDown() method we cleanup the properties:

protected void tearDown() throws Exception {
super.tearDown();
book1 = null;
book2 = null;
book3 = null;
}



Now, add a test method testEquals() to the test case. Within the method we use the assertFalse() method of the JUnit framework to test if the return-value of the equals(..) method is false, because book1 and book2 are not the same. If the return-value is false the logic of the equals() method is correct, otherwise there is a logical problem while comparing the objects. We want to test if the method compares the objects correctly by using the assertTrue() method. Book1 and Book3 are the same, because both are an instance of the class Book and have the same values.

The following source code shows the testEquals() method:

public void testEquals(){
assertFalse(book2.equals(book1));
assertTrue(book1.equals(book1));
}



Writing the logic of the equals() method

We have finished the test and now we can add the logic to the equals() method stub. Open the class Book and add the logic to the equals() method. First we check if the object given by the method is an instance of Book. Then compare the properties title and price, if they are equal return true.

public boolean equals(Object object) {
if (object instanceof Book) {
Book book = (Book) object;
return getTitle().equals(book.getTitle())
&& getPrice() == book.getPrice();
}
return false;
}


Create the suite() method

In order to run the test method testEquals() add a method suite() to the class BookTest.

Note: You can also create a separate class where you add the suite() method.

Within the method create a new instance of TestSuite and use the method addTest(..) to add a test. Here we use the dynamically way to add a test to a TestSuite.

The method looks like the follows:

public static Test suite(){
TestSuite suite = new TestSuite();
suite.addTest(new BookTest("testEquals"));
return suite;
}



Run the test

After finishing all test methods we want to run the JUnit test case. Right mouse button on the class BookTest and choose Run As > JUnit Test.

On the JUnit view (Menu Windows -> show view) of Eclipse you can see how many runs, errors and failures occurred.




aldream 2007-11-12 15:10 发表评论
]]>
在eclipse调试JSP和javaE序http://www.aygfsteel.com/aldreamlau/articles/159969.htmlaldreamaldreamMon, 12 Nov 2007 06:53:00 GMThttp://www.aygfsteel.com/aldreamlau/articles/159969.htmlhttp://www.aygfsteel.com/aldreamlau/comments/159969.htmlhttp://www.aygfsteel.com/aldreamlau/articles/159969.html#Feedback0http://www.aygfsteel.com/aldreamlau/comments/commentRss/159969.htmlhttp://www.aygfsteel.com/aldreamlau/services/trackbacks/159969.html原文出处Q?a >http://www.laliluna.de/assets/tutorials/debugging-jsp-java-tutorial-en.pdf

This tutorial gives you an overview of how to use the debugging feature of eclipse to debug your web or Java projects.

General

Author:

Sascha Wolski

Sebastian Hennebrueder

http://www.laliluna.de/tutorial.html ? Tutorials for Struts, JSF, EJB, Hibernate, xdoclet, eclipse and more.

Datum:

January, 25 2005
Development Tools

Eclipse 3.x

MyEclipse plugin 3.8

(A cheap and quite powerful Extension to Eclipse to develop Web Applications and EJB (J2EE) Applications. I think that there is a test version availalable at MyEclipse.)


PDF Version des Tutorials

http://www.laliluna.de/assets/tutorials/debugging-jsp-java-tutorial-en.pdf

Introduction

The Eclipse Platform features a Java debugger that provides all standard debugging functionality, including the ability to perform step execution, setting of breakpoints and values, inspecting variables and values, and to suspend and resume threads. With the extension MyEclipse you are able to debug JSP and included files too.



The debug perspective

First we will explain the debug perspective. You can activate the perspective under Windows > Open Perspective > Other...




Alternatively, you can click on the debug icon of the perspective tool bar.




Debug view

The Debug view displays the stack trace for the suspended threads for each target you are debugging. Each entry is the variable state of a method right when it called the next method. The view allows you to manage the debugging of a program in the workbench.




Variables view

The view displays information about the variables in the selected class. You can get information about the variable like value, size and more from this view.




Breakpoints view

The view lists all breakpoints you have set in the workbench project. In this view, you can enable or disable breakpoints, remove them, or add a new ones. You can also double-click a breakpoint to display its location in the editor.

This view also lists Java exception breakpoints, which suspend execution at the point where the exception is thrown. You can add or remove exceptions.






Expressions view

You can inspect data from each class of a suspended thread, and other places in this view. It opens automatically when an item is added to the view.




Debug a web project

In the next steps I will explain how you can debug a project. I will refer to the project JSP + Servlet you can download on our site. http://www.laliluna.de/assets/tutorials/java-servlets-jsp-tutorial.zip

Open the class BookEdit.java and set a breakpoint in the method doGet(..) at line 72. You can set a breakpoint by double-click on the info bar on the left edge of the editor, or right click Toggle Breakpoint.




Set the second breakpoint in the bookEdit.jsp file on the HTML start tag at line 14.

Note: If you don't have installed MyEclipse, you can't set a breakpoint in jsp and included files.




To see all breakpoints you can use the Breakpoints view in the debug perspective.

Breakpoint Properties

You can apply some properties to each breakpoint, for example how many times a breakpoint can hit before it suspends the thread or on which condition it suspends the thread.

Select the breakpoint right mouse button and choose the option Breakpoint Properties.









Hit Count

The hit count sets a number of times the breakpoint can be executed before the thread suspends. Very helpful in a loop expression or if you want to know the value of a expression after some hits.

Enable Condition

There are two options to suspend a thread by using a condition.

  • if the enabled condition is true

  • if the enabled condition changes

If you want that the condition suspends the thread when it is true select the option condition is true on Suspend when.

Example:

action.equals("edit");



If you want that the condition suspends the thread when the value of the condition changes select the option value of the condition changes on Suspend when.





Suspend Policy

You can define if the breakpoint suspends only the thread or the complete virtual machine.

Deploy and Debug

Now we want to debug the project, so deploy it and call it in your web browser.
Choose Edit on the list of books.




After you clicked the Edit link, eclipse shows you the following confirm window. You can choose if you want to switch to the debug perspective. Yes, you want it ;- )




The first entry in the debug view represents the state of the method where the breakpoint was set. You can preview the state of a entry simply by clicking on it, also the file will be open where the method is placed.




You can see the values of all variables of the selected entry in the Variables view.




A marked line and an arrow at the breakpoint shows that the debugger is suspended on this line.




If you've decided that you missed an important place in the execution process, perhaps the breakpoint was in the wrong place, or maybe you accidentally stepped over some code you wanted to inspect earlier in the process. Eclipse has a feature called Drop to frame, that essentially lets you 'rewind' the execution to the beginning of any method in the stack. This is especially useful when you perform variable modification or code hot swapping.
Right click on the frame and choose the option
Drop to Frame.





Inspect expressions

To get informations about expression, you can inspect a expression. Right click on the marked expression, you want to inspect and choose Inspect or press Ctrl + Shift + I. A pop-up window appears that holds the informations about the expression.






Watch expressions

Watch is similar to inspect an expression. Watch means that the expression will be added to the Expressions view, where you can watch the informations about the expression. Add two expressions to the Expressions view.

Mark the expressions, right click and choose Watch.




Now the two expression are in the Expressions view.






Display expressions

If you want to display the type and the value of an expression, mark the expression and choose the Display option from the context menu (right mouse button) or press Ctrl + Shift + D.


Run to Line

If you set a breakpoint and somewhere under the breakpoint is a line you want to go, you can use the option Run to Line. The code between the breakpoint and the selected line will be executed. Select the line you want to go, press the right mouse button and choose the option Run to Line or use the key binding Ctrl + R.


Debugging JSP files (supported by MyEclipse)


Debugging a JSP file is the same like debugging a Java class, but the most features (Watch, Inspect, etc) are not implemented. The only way to get the values of variables is the Variables view.



That's it. You will only need to debug, when you make mistakes. Avoid them and forget the tutorial.




aldream 2007-11-12 14:53 发表评论
]]>
嵌套诊断环境NDC http://www.aygfsteel.com/aldreamlau/articles/159731.htmlaldreamaldreamSun, 11 Nov 2007 06:51:00 GMThttp://www.aygfsteel.com/aldreamlau/articles/159731.htmlhttp://www.aygfsteel.com/aldreamlau/comments/159731.htmlhttp://www.aygfsteel.com/aldreamlau/articles/159731.html#Feedback0http://www.aygfsteel.com/aldreamlau/comments/commentRss/159731.htmlhttp://www.aygfsteel.com/aldreamlau/services/trackbacks/159731.html? 多用户ƈ发的环境下,通常是由不同的线E分别处理不同的客户端请求。此时要在日志信息中区分Z同的客户端,你可以ؓ每一个线E生成一个LoggerQ从 而从一堆日志信息中区分出哪些信息是属于哪个U程的,但这U方式ƈ不高效。Log4J巧妙C用了Neil Harrison提出?#8220;NDCQ嵌套诊断环境)”机制来解册个问题。Log4J为同一cd的线E生成一个LoggerQ多个线E共享用,而它仅在? 志信息中d能够区分不同U程的信息。NDC是什么?举例来说Q如果一个Servlet接到q发hӞ为每一个客L创徏一个新的线E,然后分配一个用 于保存该h上下文的NDC堆栈。该上下文可能是发出h的主机名、IP地址或其它Q何可以用于标识该h的信息。这P׃不同的客L处理U程h? 同的NDC堆栈Q即使这个Servlet同时生成多个U程处理不同的请求,q些日志信息仍然可以区分出来Q就好像Log4J为每一个线E都单独生成了一? Logger实例一栗在Log4J中是通过org.apache.log4j.NDC实现q种机制的。用NDC的方法也很简单,步骤如下Q?

1. 在进入一个环境时调用NDC.push(String)Q然后创Z个NDCQ?

2. 所做的日志操作输出中包括了NDC的信息;

3. d该环境时调用NDC.popҎQ?

4. 当从一个线E中退出时调用NDC.removeҎQ以侉K放资源?

下面是一个模拟记录来自不同客Lh事g的例子,代码如下Q?br />
import org.apache.log4j.Logger;
import org.apache.log4j.NDC;
public class TestNDC {
 
static Logger log = Logger.getLogger(TestNDC.class.getName());
 
public static void main(String[] args) {
  log.info(
"Make sure %x is in your layout pattern!");
  
// 从客L获得IP地址的例?/span>
  String[] ips = {"192.168.0.10","192.168.0.27"};
  
for (int i = 0; i<ips.length ; i++)  // 模拟一个运行方?/span>
  {
   
// IP放进 NDC?/span>
   NDC.push(ips[i]);
   log.info(
"A NEW client connected, who's ip should appear in this log message.");
   NDC.pop();
  }
  NDC.remove();
  log.info(
"Finished.");
 }
}
注意配置文g中的布局格式中一定要加上%x。系l输出如下:
INFO   - Make sure %x is in your layout pattern!
INFO  
192.168.0.10 - A NEW client connected, who's ip should appear in this log message.
INFO  
192.168.0.27 - A NEW client connected, who's ip should appear in this log message.
INFO   
- Finished.



aldream 2007-11-11 14:51 发表评论
]]>
一个Junit例子(转自caterpillar.onlyfun.net)http://www.aygfsteel.com/aldreamlau/articles/159653.htmlaldreamaldreamSat, 10 Nov 2007 16:55:00 GMThttp://www.aygfsteel.com/aldreamlau/articles/159653.htmlhttp://www.aygfsteel.com/aldreamlau/comments/159653.htmlhttp://www.aygfsteel.com/aldreamlau/articles/159653.html#Feedback0http://www.aygfsteel.com/aldreamlau/comments/commentRss/159653.htmlhttp://www.aygfsteel.com/aldreamlau/services/trackbacks/159653.html
在測試一個單元方法時Q有時您會需要它一些物件作為運行時的資料,例如您撰寫下面這個測試案例:

MaxMinTest.java
 1 package onlyfun.caterpillar.test;
 2 
 3 import onlyfun.caterpillar.MaxMinTool;
 4 import junit.framework.TestCase;
 5  
 6 public class MaxMinTest extends TestCase {
 7     public void testMax() {
 8         int[] arr = {-5-4-3-2-1012345};
 9         assertEquals(5, MaxMinTool.getMax(arr));
10     }
11 
12     public void testMin() {
13         int[] arr = {-5-4-3-2-1012345};
14         assertEquals(-5, MaxMinTool.getMin(arr));
15     }
16     
17     public static void main(String[] args) {
18         junit.swingui.TestRunner.run(MaxMinTest.class);
19     }
20 }

您將a計的MaxMinTool包括靜態ҎgetMax()與getMin()Q當您它一個整敔R列,它們將個別傛_陣列中的最大D最|然 的,您所準備的陣列重複出珑֜兩個單元測試之中,重複的程式碼在設a中可以減少儘量減,在這兩個單元測試中Q整敔R列的準備是單元方法所需要的資源Q? 我們稱之為fixtureQ也是一個測試時所需要的資源集合?br />
fixture必須與上下文QContextQ無關,也就是與E式埯前後無關Q這樣才符合單元測試的意涵Q為此,通常所需的fixture撰寫在單元方法之中,如此在單元測試開始時創徏fixtureQ並於結束後hfixture?br />
然而對於重複出珑֜各個單元測試中的fixtureQ您可以集中加以理Q您可以在繼承TestCase之後Q重新定?span style="font-weight: bold;">setUp()?span style="font-weight: bold;">tearDown()ҎQ將數個單元測試所需要的fixture在setUp()中創建,並在tearDown()中銷毀Q例如:

MaxMinTest.java
 1 package onlyfun.caterpillar.test;
 2 
 3 import onlyfun.caterpillar.MaxMinTool;
 4 import junit.framework.TestCase;
 5 
 6 public class MaxMinTest extends TestCase {
 7     private int[] arr;
 8 
 9     protected void setUp() throws Exception {
10         super.setUp();
11         arr = new int[]{-5-4-3-2-1012345};
12     }
13 
14     protected void tearDown() throws Exception {
15         super.tearDown();
16         arr = null;
17     }
18     
19     public void testMax() {
20         assertEquals(5, MaxMinTool.getMax(arr));
21     }
22 
23     public void testMin() {
24         assertEquals(-5, MaxMinTool.getMin(arr));
25     }
26     
27     public static void main(String[] args) {
28         junit.swingui.TestRunner.run(MaxMinTest.class);
29     }
30 }

setUp()Ҏ會在每一個單元測試testXXX()Ҏ開始前被呼叫Q因而整敔R列會被徏立,而tearDown()會在每一個單元測? testXXX()Ҏi束後被呼叫Q因而整敔R列參考名E將會參考至nullQ如此一來,您可以將fixture的管理集中在 setUp()與tearDown()Ҏ之後?br />
最後按照測試案例的內容Q您完成MaxMinTool別Q?

MaxMinTool.java
 1 package onlyfun.caterpillar;
 2 
 3 public class MaxMinTool {
 4     public static int getMax(int[] arr) {
 5         int max = Integer.MIN_VALUE;
 6         
 7         for(int i = 0; i < arr.length; i++) {
 8             if(arr[i] > max)
 9                 max = arr[i];
10         }
11         
12         return max;
13     }
14     
15     public static int getMin(int[] arr) {
16         int min = Integer.MAX_VALUE;
17         
18         for(int i = 0; i < arr.length; i++) {
19             if(arr[i] < min)
20                 min = arr[i];
21         }
22         
23         return min;
24     }
25 }

Swing介面的TestRunner在測試失敗時會顯C紅色的子Q而在測試成功後會示E色的棒子,?"Keep the bar green to keep the code clean." 正是JUnit的名aQ也是測試的最i目的?br />

aldream 2007-11-11 00:55 发表评论
]]>
Log4j优化http://www.aygfsteel.com/aldreamlau/articles/159642.htmlaldreamaldreamSat, 10 Nov 2007 15:31:00 GMThttp://www.aygfsteel.com/aldreamlau/articles/159642.htmlhttp://www.aygfsteel.com/aldreamlau/comments/159642.htmlhttp://www.aygfsteel.com/aldreamlau/articles/159642.html#Feedback0http://www.aygfsteel.com/aldreamlau/comments/commentRss/159642.htmlhttp://www.aygfsteel.com/aldreamlau/services/trackbacks/159642.html 用户应该注意随后的优化徏议?br /> 9.1 日志为禁用时Q日志的优化
当日志被d的关闭,一个日志请求的p{于一个方法的调用加上整数的比较时间?br /> ?33mhz的Pentium II 机器上这个花贚w常?-50U秒之间?br /> 然而,Ҏ调用包括参数构徏的隐藏花贏V?br /> 例如Q对于logger catQ?br />
logger.debug("Entry number: " + i + " is " + String.valueOf(entry));
引v了构Z息参数的pQ例如,转化整数i和entryC个stringQƈ且连接中间字W串Q不信息是否被输出。这个参数的构徏p可能是很高,它主要决定于被调用的参数的大?br /> 避免参数构徏的花费应如下Q?br />
if(logger.isDebugEnabled() {
logger.debug(
"Entry number: " + i + " is " + String.valueOf(entry[i]));
}

如果logger的debug被关闭这不会招致参数构建的p。另一斚wQ如果logger是debug的话Q它生两ơ判?logger是否能用的花贏V一ơ是在debugenabledQ一ơ是debug。这是无关紧要的Q因为判断日志是否可用只占日志实际花Ҏ间的U?%?br /> 在Log4j里,日志h在Logger cȝ实例里。Logger 是一个类Q而不是一个接口。这大量的减了在方法调用上的弹性化的花贏V?br /> 当然用户采用预处理或~译旉技术去~译出所有的日志声明。这导致完的执行成效?br /> 然而因Zq制应用E序不包括Q何的日志声明的结果,日志不可能对那个二进制程序开启。以我的观点Q以q种较大的代h换取较小的性能优化是不值得的?br /> 9.2 当日志状态ؓ启用Ӟ日志的优?/strong>
q是本质上的优化logger的层ơ。当日志状态ؓ开QLog4j依然需要比较请求的U别与logger的别。然而,logger可能没有被安排一个别;它们从它们的fatherl承。这P在承之前,logger可能需要搜索它的祖先?br /> q里有一个认真的努力使层ơ的搜烦可能的快。例如,子logger仅仅q接到它的存在的father logger?br /> 在先前展C的BasicConfigurator 例子中,名ؓcom.foo.bar 的logger是连接到根loggerQ因此绕q了不存在的logger com和com.foo。这显著的改善执行的速度Q特别是解析logger的层l构时?br /> 典型的层ơ结构的解析的花Ҏloggerd关闭时的三倍?br /> 9.3 日志信息的输出时Q日志的优化
q是主要p在日志输出的格式化和发送它到它的输出源上。这里我们再一ơ的付出努力以格式化执行的可能快。同appender一栗实际上典型的花费大U是100-300毫秒?br /> 详情看org.apache.log4.performance.Logging?br /> 虽然Log4j有许多特点,但是它的W一个设计目标还是速度。一些Log4j的组件已l被重写q很多次以改善性能。不q,投稿者经常提Z新的优化。你应该满意的知道,以SimpleLayout的配|执行测试已l展CZLog4j的输出同System.out.println一样快?br />

aldream 2007-11-10 23:31 发表评论
]]>
Log4j配置文ghttp://www.aygfsteel.com/aldreamlau/articles/159483.htmlaldreamaldreamFri, 09 Nov 2007 16:43:00 GMThttp://www.aygfsteel.com/aldreamlau/articles/159483.htmlhttp://www.aygfsteel.com/aldreamlau/comments/159483.htmlhttp://www.aygfsteel.com/aldreamlau/articles/159483.html#Feedback0http://www.aygfsteel.com/aldreamlau/comments/commentRss/159483.htmlhttp://www.aygfsteel.com/aldreamlau/services/trackbacks/159483.html下面的Log4J配置文g实现了输出到控制台、文件、回滚文件、发送日志邮件、输出到数据库日志表、自定义标签{全套功能?/span>
 1log4j.rootLogger=DEBUG,CONSOLE,A1,im
 2log4j.addivity.org.apache=true
 3# 应用于控制台
 4log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
 5log4j.appender.Threshold=DEBUG
 6log4j.appender.CONSOLE.Target=System.out
 7log4j.appender.CONSOLE.Encoding=GBK
 8log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
 9log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
10#log4j.appender.CONSOLE.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD] n%c[CATEGORY]%n%m[MESSAGE]%n%n
11#应用于文?br /> 12log4j.appender.FILE=org.apache.log4j.FileAppender
13log4j.appender.FILE.File=file.log
14log4j.appender.FILE.Append=false
15log4j.appender.FILE.Encoding=GBK
16log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
17log4j.appender.FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
18# Use this layout for LogFactor 5 analysis
19# 应用于文件回?br /> 20log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender
21log4j.appender.ROLLING_FILE.Threshold=ERROR
22log4j.appender.ROLLING_FILE.File=rolling.log
23log4j.appender.ROLLING_FILE.Append=true
24log4j.appender.CONSOLE_FILE.Encoding=GBK
25log4j.appender.ROLLING_FILE.MaxFileSize=10KB
26log4j.appender.ROLLING_FILE.MaxBackupIndex=1
27log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout
28log4j.appender.ROLLING_FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
29#应用于socket
30log4j.appender.SOCKET=org.apache.log4j.RollingFileAppender
31log4j.appender.SOCKET.RemoteHost=localhost
32log4j.appender.SOCKET.Port=5001
33log4j.appender.SOCKET.LocationInfo=true
34# Set up for Log Facter 5
35log4j.appender.SOCKET.layout=org.apache.log4j.PatternLayout
36log4j.appender.SOCET.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD]%n%c[CATEGORY]%n%m[MESSAGE]%n%n
37# Log Factor 5 Appender
38log4j.appender.LF5_APPENDER=org.apache.log4j.lf5.LF5Appender
39log4j.appender.LF5_APPENDER.MaxNumberOfRecords=2000
40# 发送日志给邮g
41log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender
42log4j.appender.MAIL.Threshold=FATAL
43log4j.appender.MAIL.BufferSize=10
44log4j.appender.MAIL.From=web@www.wuset.com
45log4j.appender.MAIL.SMTPHost=www.wusetu.com
46log4j.appender.MAIL.Subject=Log4J Message
47log4j.appender.MAIL.To=web@www.wusetu.com
48log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout
49log4j.appender.MAIL.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
50# 用于数据?br /> 51log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender
52log4j.appender.DATABASE.URL=jdbc:mysql://localhost:3306/test
53log4j.appender.DATABASE.driver=com.mysql.jdbc.Driver
54log4j.appender.DATABASE.user=root
55log4j.appender.DATABASE.password=
56log4j.appender.DATABASE.sql=INSERT INTO LOG4J (Message) VALUES ('[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n')
57log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout
58log4j.appender.DATABASE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
59# 每天新徏日志
60log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender
61log4j.appender.A1.File=log
62log4j.appender.A1.Encoding=GBK
63log4j.appender.A1.DatePattern='.'yyyy-MM-dd
64log4j.appender.A1.layout=org.apache.log4j.PatternLayout
65log4j.appender.A1.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L : %m%n
66#自定义Appender
67log4j.appender.im = net.cybercorlin.util.logger.appender.IMAppender
68log4j.appender.im.host = mail.cybercorlin.net
69log4j.appender.im.username = username
70log4j.appender.im.password = password
71log4j.appender.im.recipient = corlin@cybercorlin.net
72log4j.appender.im.layout=org.apache.log4j.PatternLayout
73log4j.appender.im.layout.ConversionPattern =[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n


aldream 2007-11-10 00:43 发表评论
]]>
վ֩ģ壺 | | | ξ| | ҽ| | ҵ| ɣ| ʡ| ˮ| | ̶| ͷ| | Ӣ| ˱| °Ͷ| | Т| ʡ| | | | | | ƽ| ӱ| | | ո| ʯ̨| Ҧ| غ| | | | ɣֲ| ֶ| | |