01: /*
02: * Created on Dec 9, 2003
03: *
04: * To change the template for this generated file go to
05: * Window>Preferences>Java>Code Generation>Code and Comments
06: */
07: package org.xdev.base.log;
08:
09: import java.io.IOException;
10: import java.net.InetAddress;
11: import java.text.SimpleDateFormat;
12: import java.util.StringTokenizer;
13:
14: import org.apache.log4j.Layout;
15: import org.apache.log4j.DailyRollingFileAppender;
16: import org.xdev.base.util.Tools;
17:
18: /**
19: * @author AYegorov
20: *
21: * To change the template for this generated type comment go to
22: * Window>Preferences>Java>Code Generation>Code and Comments
23: */
24: public class FormattedRollingFileAppender extends
25: DailyRollingFileAppender {
26: private SimpleDateFormat df = new SimpleDateFormat();
27:
28: /**
29: *
30: */
31: public FormattedRollingFileAppender() {
32: super ();
33: // TODO Auto-generated constructor stub
34: }
35:
36: /**
37: * @param arg0
38: * @param arg1
39: * @param arg2
40: * @throws java.io.IOException
41: */
42: public FormattedRollingFileAppender(Layout arg0, String arg1,
43: String arg2) throws IOException {
44: super (arg0, arg1, arg2);
45: // TODO Auto-generated constructor stub
46: }
47:
48: public void setFile(String file) {
49: super .setFile(this .buildFileName(file));
50: }
51:
52: public void setFile(String fileName, boolean append,
53: boolean bufferedIO, int bufferSize) throws IOException {
54: super .setFile(this .buildFileName(fileName), append, bufferedIO,
55: bufferSize);
56: }
57:
58: protected String buildFileName(String pattern) {
59: StringTokenizer tokens = new StringTokenizer(pattern, "+");
60: StringBuffer fileName = new StringBuffer();
61: String token = null;
62: while (tokens.hasMoreElements()) {
63: token = tokens.nextToken();
64: if (token.startsWith("$date")) {
65: fileName.append(this .buildTimestamp(token));
66: } else if (token.startsWith("$hostname")) {
67: fileName.append(this .buildHostname());
68: } else {
69: fileName.append(token);
70: }
71: }
72:
73: return fileName.toString();
74: }
75:
76: protected String buildTimestamp(String token) {
77: String pattern = token.substring(token.indexOf("(") + 1, token
78: .lastIndexOf(")"));
79: this .df.applyPattern(pattern);
80:
81: return this .df.format(new java.util.Date());
82: }
83:
84: protected String buildHostname() {
85: String host = "";
86:
87: try {
88: host = Tools.replaceString(InetAddress.getLocalHost()
89: .getHostAddress(), ".", "");
90: } catch (Exception ex) {
91: }
92:
93: return host;
94: }
95:
96: }
|