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: MonologLogChannel.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.objectweb.util.monolog.api.BasicLevel;
028: import org.objectweb.util.monolog.api.Level;
029:
030: //import org.objectweb.util.monolog.file.monolog.PropertiesConfAccess;
031:
032: /**
033: * Monolog implementation of a channel associated with a logging
034: * facility. All messages for the facility are written using a channel.
035: * Care is take to avoid synchronization when possible for performance
036: * reasons.
037: *
038: * @author Sinisa Milosevic
039: * @see com.lutris.logging.LogChannel
040: * @see com.lutris.logging.Log4jLogger
041: */
042: public class MonologLogChannel implements LogChannel {
043:
044: /**
045: * Our symbolic name.
046: */
047: String separatorLine = "";
048: // private String facility;
049:
050: private static final int INT_ERROR = -1;
051:
052: /**
053: * Format for the date.
054: */
055: private static final SimpleDateFormat dateFormatter = new SimpleDateFormat(
056: "yyyy.MM.dd HH:mm:ss");
057:
058: /**
059: * Logger object with which we are associated.
060: */
061: private org.objectweb.util.monolog.api.Logger logger;
062:
063: /**
064: * Construct a new log channel.
065: *
066: * @param chanFacility The facility that the channel is associate with.
067: * @param loggerObj The logging object that this channel will be associated
068: * with.
069: */
070: protected MonologLogChannel(String chanFacility,
071: org.objectweb.util.monolog.api.Logger loggerObj,
072: String separator) {
073: // facility = chanFacility;
074: logger = loggerObj;
075: separatorLine = separator;
076: }
077:
078: public int getLevel(String level) {
079: if (logger != null) {
080: return logger.getCurrentIntLevel();
081: }
082: return INT_ERROR;
083: }
084:
085: /**
086: * Determine if logging is enabled for the specified level.
087: */
088: public boolean isEnabled(int level) {
089: return true;
090: // return logger.isEnabledFor(intToLevel(level));
091: }
092:
093: /**
094: * Determine if logging is enabled for the specified level.
095: */
096: public boolean isEnabled(String level) {
097: return true;
098: }
099:
100: /**
101: * Write a string to the log file.
102: */
103: public void write(int level, String msg) {
104: Level lev;
105:
106: lev = intToLevel(level);
107: if (lev == null) {
108: lev = BasicLevel.LEVEL_INFO;
109: }
110: logger.log(lev, msg + separatorLine);
111: }
112:
113: /**
114: * Write a string and exception to the log file.
115: */
116: public void write(int level, String msg, Throwable throwable) {
117: Level lev;
118:
119: lev = intToLevel(level);
120: if (lev == null) {
121: lev = BasicLevel.LEVEL_INFO;
122: }
123: logger.log(lev, msg + separatorLine, throwable);
124: }
125:
126: /**
127: * Write a string to the log file.
128: */
129: public void write(String level, String msg) {
130: Level lev;
131:
132: lev = stringToLevel(level);
133: if (lev == null) {
134: lev = BasicLevel.LEVEL_INFO;
135: }
136: logger.log(lev, msg + separatorLine);
137: }
138:
139: /**
140: * Write a string and exception to the log file.
141: */
142: public void write(String level, String msg, Throwable throwable) {
143: Level lev;
144: lev = stringToLevel(level);
145: if (lev == null) {
146: lev = BasicLevel.LEVEL_INFO;
147: }
148: logger.log(lev, msg + separatorLine, throwable);
149: }
150:
151: private Level intToLevel(int level) {
152: Level lev;
153:
154: switch (level) {
155: case com.lutris.logging.Logger.EMERGENCY:
156: case com.lutris.logging.Logger.ALERT:
157: lev = BasicLevel.LEVEL_FATAL;
158: break;
159:
160: case com.lutris.logging.Logger.CRITICAL:
161: case com.lutris.logging.Logger.ERROR:
162: lev = BasicLevel.LEVEL_ERROR;
163: break;
164:
165: case com.lutris.logging.Logger.WARNING:
166: lev = BasicLevel.LEVEL_WARN;
167: break;
168:
169: case com.lutris.logging.Logger.NOTICE:
170: case com.lutris.logging.Logger.INFO:
171: lev = BasicLevel.LEVEL_INFO;
172: break;
173:
174: case com.lutris.logging.Logger.DEBUG:
175: case com.lutris.logging.Logger.DEBUG1:
176: case com.lutris.logging.Logger.DEBUG2:
177: case com.lutris.logging.Logger.DEBUG3:
178: case com.lutris.logging.Logger.DEBUG4:
179: case com.lutris.logging.Logger.DEBUG5:
180: case com.lutris.logging.Logger.DEBUG6:
181: case com.lutris.logging.Logger.DEBUG7:
182: case com.lutris.logging.Logger.DEBUG8:
183: case com.lutris.logging.Logger.DEBUG9:
184: case com.lutris.logging.Logger.DEBUGTMP:
185: lev = BasicLevel.LEVEL_DEBUG;
186: break;
187:
188: default:
189: lev = BasicLevel.LEVEL_DEBUG;
190: } // end switch (level)
191: return lev;
192: }
193:
194: private Level stringToLevel(String level) {
195: Level lev;
196:
197: if (level.equalsIgnoreCase("EMERGENCY")
198: || level.equalsIgnoreCase("ALERT")) {
199: lev = BasicLevel.LEVEL_FATAL;
200: } else if (level.equalsIgnoreCase("CRITICAL")
201: || level.equalsIgnoreCase("ERROR")) {
202: lev = BasicLevel.LEVEL_ERROR;
203: } else if (level.equalsIgnoreCase("WARNING")) {
204: lev = BasicLevel.LEVEL_WARN;
205: } else if (level.equalsIgnoreCase("NOTICE")
206: || level.equalsIgnoreCase("INFO")) {
207: lev = BasicLevel.LEVEL_INFO;
208: } else if (level.equalsIgnoreCase("DEBUG")
209: || level.equalsIgnoreCase("DEBUG1")
210: || level.equalsIgnoreCase("DEBUG2")
211: || level.equalsIgnoreCase("DEBUG3")
212: || level.equalsIgnoreCase("DEBUG4")
213: || level.equalsIgnoreCase("DEBUG5")
214: || level.equalsIgnoreCase("DEBUG6")
215: || level.equalsIgnoreCase("DEBUG7")
216: || level.equalsIgnoreCase("DEBUG8")
217: || level.equalsIgnoreCase("DEBUG9")
218: || level.equalsIgnoreCase("DEBUGTMP")) {
219: lev = BasicLevel.LEVEL_DEBUG;
220: } else {// default
221: lev = BasicLevel.LEVEL_DEBUG;
222: }
223: return lev;
224: }
225:
226: /**
227: * <b>NOT SUPPORTED</b>
228: *
229: * @param level Symbolic level that is to be checked.
230: * @return A log writer object.
231: */
232: public LogWriter getLogWriter(String level) {
233: return new MonologWriter(this , level);
234:
235: }
236:
237: /**
238: * <b>NOT SUPPORTED</b>
239: *
240: * @param level Numeric level that is to be checked.
241: * @return A log writer object.
242: */
243: public LogWriter getLogWriter(int level) {
244: return new MonologWriter(this, level);
245: }
246: }
|