001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: Log4jLogChannel.java,v 1.3 2007-10-19 10:05:39 sinisa Exp $
022: */
023: package com.lutris.logging;
024:
025: import java.text.SimpleDateFormat;
026:
027: import org.apache.log4j.Level;
028: import org.apache.log4j.Logger;
029:
030: /**
031: * Standard implementation of a channel associated with a logging
032: * facility. All messages for the facility are written using a channel.
033: * Care is take to avoid synchronization when possible for performance
034: * reasons.
035: *
036: * @author Vladimir Puskas
037: * @author Predrag Djoic
038: * @author Sinisa Milosevic
039: * @see com.lutris.logging.LogChannel
040: * @see com.lutris.logging.Log4jLogger
041: */
042: public class Log4jLogChannel implements LogChannel {
043:
044: /**
045: * Our symbolic name.
046: */
047: String separatorLine = "";
048:
049: // private String facility;
050:
051: private static final int INT_ERROR = -1;
052:
053: /**
054: * Format for the date.
055: */
056: private static final SimpleDateFormat dateFormatter = new SimpleDateFormat(
057: "yyyy.MM.dd HH:mm:ss");
058:
059: /**
060: * Logger object with which we are associated.
061: */
062: private org.apache.log4j.Logger logger;
063:
064: /**
065: * Construct a new log channel.
066: *
067: * @param chanFacility The facility that the channel is associate with.
068: * @param loggerObj The logging object that this channel will be associated
069: * with.
070: */
071: protected Log4jLogChannel(String chanFacility,
072: org.apache.log4j.Logger loggerObj, String separator) {
073: // facility = chanFacility;
074: logger = loggerObj;
075: separatorLine = separator;
076:
077: }
078:
079: public int getLevel(String level) {
080: if (logger != null) {
081: if (logger.getLevel() != null) {
082: return logger.getLevel().toInt();
083: }
084: }
085: return INT_ERROR;
086: }
087:
088: /**
089: * Determine if logging is enabled for the specified level.
090: */
091: public boolean isEnabled(int level) {
092: return true;
093: // return logger.isEnabledFor(intToLevel(level));
094: }
095:
096: /**
097: * Determine if logging is enabled for the specified level.
098: */
099: public boolean isEnabled(String level) {
100: return true;
101: // return logger.isEnabledFor(stringToLevel(level));
102: }
103:
104: /**
105: * Write a string to the log file.
106: */
107: public void write(int level, String msg) {
108: Level lev;
109:
110: if (logger == null) {
111: logger = Logger.getLogger(LogAdapter.class.getName());
112: }
113: lev = intToLevel(level);
114: if (lev == null) {
115: lev = Level.INFO;
116: }
117:
118: logger.log(lev, msg + separatorLine);
119: }
120:
121: /**
122: * Write a string and exception to the log file.
123: */
124: public void write(int level, String msg, Throwable throwable) {
125: Level lev;
126:
127: if (logger == null) {
128: logger = Logger.getLogger(LogAdapter.class.getName());
129: }
130: lev = intToLevel(level);
131: if (lev == null) {
132: lev = Level.INFO;
133: }
134: logger.log(lev, msg + separatorLine, throwable);
135: }
136:
137: /**
138: * Write a string to the log file.
139: */
140: public void write(String level, String msg) {
141: Level lev;
142:
143: if (logger == null) {
144: logger = Logger.getLogger(LogAdapter.class.getName());
145: }
146: lev = stringToLevel(level);
147: if (lev == null) {
148: lev = Level.INFO;
149: }
150: logger.log(lev, msg + separatorLine);
151: }
152:
153: /**
154: * Write a string and exception to the log file.
155: */
156: public void write(String level, String msg, Throwable throwable) {
157: Level lev;
158:
159: if (logger == null) {
160: logger = Logger.getLogger(LogAdapter.class.getName());
161: }
162: lev = stringToLevel(level);
163: if (lev == null) {
164: lev = Level.INFO;
165: }
166: logger.log(lev, msg + separatorLine, throwable);
167: }
168:
169: private Level intToLevel(int level) {
170: Level lev;
171:
172: switch (level) {
173: case com.lutris.logging.Logger.EMERGENCY:
174: case com.lutris.logging.Logger.ALERT:
175: lev = Level.FATAL;
176: break;
177:
178: case com.lutris.logging.Logger.CRITICAL:
179: case com.lutris.logging.Logger.ERROR:
180: lev = Level.ERROR;
181: break;
182:
183: case com.lutris.logging.Logger.WARNING:
184: lev = Level.WARN;
185: break;
186:
187: case com.lutris.logging.Logger.NOTICE:
188: case com.lutris.logging.Logger.INFO:
189: lev = Level.INFO;
190: break;
191:
192: case com.lutris.logging.Logger.DEBUG:
193: case com.lutris.logging.Logger.DEBUG1:
194: case com.lutris.logging.Logger.DEBUG2:
195: case com.lutris.logging.Logger.DEBUG3:
196: case com.lutris.logging.Logger.DEBUG4:
197: case com.lutris.logging.Logger.DEBUG5:
198: case com.lutris.logging.Logger.DEBUG6:
199: case com.lutris.logging.Logger.DEBUG7:
200: case com.lutris.logging.Logger.DEBUG8:
201: case com.lutris.logging.Logger.DEBUG9:
202: case com.lutris.logging.Logger.DEBUGTMP:
203: lev = Level.DEBUG;
204: break;
205:
206: default:
207: lev = Level.DEBUG;
208: } // end switch (level)
209: return lev;
210: }
211:
212: private Level stringToLevel(String level) {
213: Level lev;
214:
215: if (level.equalsIgnoreCase("EMERGENCY")
216: || level.equalsIgnoreCase("ALERT")) {
217: lev = Level.FATAL;
218: } else if (level.equalsIgnoreCase("CRITICAL")
219: || level.equalsIgnoreCase("ERROR")) {
220: lev = Level.ERROR;
221: } else if (level.equalsIgnoreCase("WARNING")) {
222: lev = Level.WARN;
223: } else if (level.equalsIgnoreCase("NOTICE")
224: || level.equalsIgnoreCase("INFO")) {
225: lev = Level.INFO;
226: } else if (level.equalsIgnoreCase("DEBUG")
227: || level.equalsIgnoreCase("DEBUG1")
228: || level.equalsIgnoreCase("DEBUG2")
229: || level.equalsIgnoreCase("DEBUG3")
230: || level.equalsIgnoreCase("DEBUG4")
231: || level.equalsIgnoreCase("DEBUG5")
232: || level.equalsIgnoreCase("DEBUG6")
233: || level.equalsIgnoreCase("DEBUG7")
234: || level.equalsIgnoreCase("DEBUG8")
235: || level.equalsIgnoreCase("DEBUG9")
236: || level.equalsIgnoreCase("DEBUGTMP")) {
237: lev = Level.DEBUG;
238: } else {// default
239: lev = Level.DEBUG;
240: }
241: return lev;
242: }
243:
244: /**
245: * <b>NOT SUPPORTED</b>
246: *
247: * @param level Symbolic level that is to be checked.
248: * @return A log writer object.
249: */
250: public LogWriter getLogWriter(String level) {
251: // throw new UnsupportedOperationException("Log4jLogChannel doesn't support this operation.");
252: return new Log4jLogWriter(this , level);
253:
254: }
255:
256: /**
257: * <b>NOT SUPPORTED</b>
258: *
259: * @param level Numeric level that is to be checked.
260: * @return A log writer object.
261: */
262: public LogWriter getLogWriter(int level) {
263: // throw new UnsupportedOperationException("Log4jLogChannel doesn't support this operation.");
264: return new Log4jLogWriter(this, level);
265: }
266: }
|