KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > webdocwf > util > loader > logging > Log4jLogger


1 /**
2     Copyright (C) 2002-2003 Together
3     This library is free software; you can redistribute it and/or
4     modify it under the terms of the GNU Lesser General Public
5     License as published by the Free Software Foundation; either
6     version 2.1 of the License, or (at your option) any later version.
7     This library is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10     Lesser General Public License for more details.
11     You should have received a copy of the GNU Lesser General Public
12     License along with this library; if not, write to the Free Software
13     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111--1307 USA
14  */

15 package org.webdocwf.util.loader.logging;
16
17 import java.util.*;
18 import java.io.*;
19 import org.webdocwf.util.loader.BufferOctopusClass;
20
21 import java.util.Hashtable JavaDoc;
22 import java.io.*;
23 import org.apache.log4j.BasicConfigurator;
24 import org.apache.log4j.ConsoleAppender;
25 import org.apache.log4j.Level;
26 import org.apache.log4j.Logger;
27 import org.apache.log4j.PatternLayout;
28 import org.apache.log4j.RollingFileAppender;
29 import org.apache.log4j.spi.LoggerRepository;
30 import org.apache.log4j.xml.DOMConfigurator;
31
32 public class Log4jLogger
33     extends org.webdocwf.util.loader.logging.Logger{
34
35   /**
36    * Log file name.
37    */

38   File activeLogFile;
39
40   /**
41    * Log file writter.
42    */

43   PrintWriter logFileStream;
44
45   private boolean[] enabledLogLevels = new boolean[3];
46   private Hashtable JavaDoc messages = new Hashtable JavaDoc();
47   private String JavaDoc logMode;
48   private org.apache.log4j.Logger logger;
49
50   /**
51    * Construct a new logger. Configuration is not done now, to allow
52    * the logger to be created very early.
53    */

54   public Log4jLogger() {
55     centralLogger = this;
56   }
57
58   /**
59    * Configure Logger with given config file, interpreting of config file is
60    * logger implementation specific.
61    *
62    * @param log4jConfFile is Path to configuration file.
63    * @throws Exception
64    */

65   public void configure(String JavaDoc log4jConfFile) throws Exception JavaDoc {
66
67     try {
68       DOMConfigurator.configure(log4jConfFile);
69       this.logger=Logger.getLogger("OctopusLogger");
70     }
71     catch (javax.xml.parsers.FactoryConfigurationError JavaDoc fce) {
72       throw new Exception JavaDoc("Cannot configure Log4jLogger! FactoryConfigurationError occured!");
73
74     }
75   }
76
77   public int getLevel(String JavaDoc level) {
78     if (level.equalsIgnoreCase(org.webdocwf.util.loader.logging.Logger.strLOGMODE_NONE))
79       return 0;
80     else if (level.equalsIgnoreCase(org.webdocwf.util.loader.logging.Logger.strLOGMODE_NORMAL))
81       return 1;
82     else
83       return 2;
84   }
85
86   public boolean isEnabled(int level) {
87     return true;
88   }
89
90   public boolean isEnabled(String JavaDoc level) {
91 // return isEnabled(this.getLevel(level));
92
return true;
93   }
94
95   public void write(int level, String JavaDoc msg) {
96     Level lev;
97     if (this.logger == null) {
98       this.logger = Logger.getLogger("OctopusLogger");
99     }
100     if(level==1)
101       lev=Level.INFO;
102     else if( level == 2 )
103       lev=Level.DEBUG;
104     else
105       lev = Level.INFO;
106
107     logger.log(lev, msg);
108   }
109
110   public synchronized void write(String JavaDoc level, String JavaDoc msg) {
111     write(getLevel(level), msg);
112   }
113
114   public synchronized void write(int level, String JavaDoc msg, Throwable JavaDoc throwable) {
115     Date date = new Date();
116     StringWriter stackBuf = new StringWriter();
117     throwable.printStackTrace(new PrintWriter(stackBuf));
118     stackBuf.flush();
119
120     String JavaDoc errMsg = msg + ":" + " " + throwable.getMessage() + '\n' + stackBuf;
121     this.write(level, errMsg);
122
123   }
124
125   public synchronized void write(String JavaDoc level, String JavaDoc msg, Throwable JavaDoc throwable) {
126     write(getLevel(level), msg, throwable);
127   }
128
129   public void setEnabledLogLevels(String JavaDoc logMode) {
130   }
131
132   public boolean[] getEnabledLogLevels() {
133     return enabledLogLevels;
134   }
135
136   public String JavaDoc getMessage(String JavaDoc key) {
137     if (key != null) {
138       return (String JavaDoc)this.messages.get(key);
139     } else
140       return null;
141   }
142
143   public boolean setMessage(String JavaDoc key, String JavaDoc value) {
144     if (value != null && key != null) {
145       this.messages.put(key, value);
146       return true;
147     } else
148       return false;
149   }
150
151   public boolean writeEcho(String JavaDoc strLogTxt) {
152     if(!this.logMode.equalsIgnoreCase(org.webdocwf.util.loader.logging.Logger.strLOGMODE_NONE)) {
153       this.write(org.webdocwf.util.loader.logging.Logger.strLOGMODE_NORMAL,strLogTxt);
154       BufferOctopusClass.getInstance().writeToBuffer(strLogTxt);
155       return true;
156     }
157     else
158     return false;
159   }
160
161   public void close(){
162   //
163
}
164 }
165
Popular Tags