KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > webman > util > log4j > WebmanRollingFileAppender


1 package de.webman.util.log4j;
2
3 import org.apache.log4j.Layout;
4 import org.apache.log4j.RollingFileAppender;
5 import com.teamkonzept.web.TKEvent;
6 import com.teamkonzept.web.servlet.ServletInterface;
7 import java.io.File JavaDoc;
8 import java.io.IOException JavaDoc;
9
10 /**
11   Adds functionality to locate the log directory
12     * @author $Author: alex $
13     * @version $Revision: 1.1 $
14 */

15 public class WebmanRollingFileAppender extends RollingFileAppender
16 {
17     private static String JavaDoc documentRoot;
18     private boolean appended = false;
19     
20     /**
21         The default constructor does nothing.
22     */

23     public WebmanRollingFileAppender()
24     {
25         super();
26     }
27
28     /**
29         Instantiate a <code>DailyRollingFileAppender</code> and open the
30         file designated by <code>filename</code>. The opened filename will
31         become the ouput destination for this appender.
32     */

33     public WebmanRollingFileAppender (Layout layout, String JavaDoc filename) throws java.io.IOException JavaDoc
34     {
35         super();
36         // Root bekommen
37
String JavaDoc root = getDocRoot();
38         if (root != null)
39             setFile(root + File.pathSeparator + filename);
40         else
41             setFile(filename);
42         
43         setLayout(layout);
44     }
45     
46     /**
47         ueberschrieben, um das LogVerzeichnis davor zu packen
48     */

49     public void setFile(String JavaDoc name)
50     {
51         String JavaDoc root = getDocRoot();
52         // System.out.println("Docroot : " + root + " Name " + name);
53
if (root != null)
54             super.setFile(root + File.separator+ name);
55         else
56             super.setFile(name);
57         
58     }
59     
60     /**
61         ueberschrieben, um das LogVerzeichnis davor zu packen
62     */

63     public void setFile(String JavaDoc name, boolean append) throws java.io.IOException JavaDoc
64     {
65         String JavaDoc root = getDocRoot();
66         //System.out.println("Docroot append: " + root + " NAme:" + name);
67
if (root != null)
68             super.setFile(root + File.separator + name, append);
69         else
70             super.setFile(name, append);
71         
72     }
73     
74     public static void setDocRoot(String JavaDoc root)
75     {
76         documentRoot = root;
77         // appended = false;
78
}
79     
80     /**
81         setzt die Document Root der Webapplication vor den File Namen,
82         damit das log Verzeichnis nicht absolut im Konf-file angegeben werden muss
83     */

84     private String JavaDoc getDocRoot()
85     {
86         if (appended) // nur einmal davorpacken !
87
return null;
88         String JavaDoc contextPath = null;
89         TKEvent event = TKEvent.getEventForThread();
90         if(event != null){
91             contextPath = ((ServletInterface)event.getHttpInterface()).getContextPath();
92         }
93         else if(documentRoot != null){
94             contextPath = documentRoot;
95         }
96         contextPath += "log" + File.separator;
97         appended = true;
98         // kurz testen, ob es das Verzeichnis schon gibt
99
File JavaDoc f = new File JavaDoc(contextPath);
100         if (!f.exists())
101             f.mkdir();
102         return contextPath;
103     }
104     
105 }
106
Popular Tags