0001: /*
0002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
0003: *
0004: * // Copyright (c) 1998, 2007, Oracle. All rights reserved.
0005: *
0006: *
0007: * The contents of this file are subject to the terms of either the GNU
0008: * General Public License Version 2 only ("GPL") or the Common Development
0009: * and Distribution License("CDDL") (collectively, the "License"). You
0010: * may not use this file except in compliance with the License. You can obtain
0011: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
0012: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
0013: * language governing permissions and limitations under the License.
0014: *
0015: * When distributing the software, include this License Header Notice in each
0016: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
0017: * Sun designates this particular file as subject to the "Classpath" exception
0018: * as provided by Sun in the GPL Version 2 section of the License file that
0019: * accompanied this code. If applicable, add the following below the License
0020: * Header, with the fields enclosed by brackets [] replaced by your own
0021: * identifying information: "Portions Copyrighted [year]
0022: * [name of copyright owner]"
0023: *
0024: * Contributor(s):
0025: *
0026: * If you wish your version of this file to be governed by only the CDDL or
0027: * only the GPL Version 2, indicate your decision by adding "[Contributor]
0028: * elects to include this software in this distribution under the [CDDL or GPL
0029: * Version 2] license." If you don't indicate a single choice of license, a
0030: * recipient has the option to distribute your version of this file under
0031: * either the CDDL, the GPL Version 2 or to extend the choice of license to
0032: * its licensees as provided above. However, if you add GPL Version 2 code
0033: * and therefore, elected the GPL Version 2 license, then the option applies
0034: * only if the new code is made subject to such option by the copyright
0035: * holder.
0036: */
0037: package oracle.toplink.essentials.logging;
0038:
0039: import java.util.*;
0040: import java.text.*;
0041: import java.io.*;
0042: import oracle.toplink.essentials.sessions.Session;
0043: import oracle.toplink.essentials.internal.databaseaccess.Accessor;
0044: import oracle.toplink.essentials.internal.localization.*;
0045: import oracle.toplink.essentials.exceptions.*;
0046: import oracle.toplink.essentials.internal.sessions.AbstractSession;
0047:
0048: /**
0049: * Represents the abstract log that implements all the generic logging functions.
0050: * It contains a singleton SessionLog that logs messages from outside any TopLink session.
0051: * The singleton SessionLog can also be passed to a TopLink session when messages
0052: * are logged through that session. When JDK1.4 is used, a singleton JavaLog is created.
0053: * Otherwise a singleton DefaultSessionLog is created.
0054: *
0055: * @see SessionLog
0056: * @see SessionLogEntry
0057: * @see DefaultSessionLog
0058: * @see JavaLog
0059: */
0060: public abstract class AbstractSessionLog implements SessionLog,
0061: java.lang.Cloneable {
0062:
0063: /**
0064: * Represents the log level
0065: */
0066: protected int level;
0067:
0068: /**
0069: * Represents the singleton SessionLog
0070: */
0071: protected static SessionLog defaultLog;
0072:
0073: /**
0074: * Represents the session that owns this SessionLog
0075: */
0076: protected Session session;
0077:
0078: /**
0079: * Represents the session type of the session that owns this SessionLog
0080: */
0081: protected String sessionType;
0082:
0083: /**
0084: * Caches the session hash code string of the session that owns this SessionLog
0085: */
0086: protected String sessionHashCode;
0087:
0088: /**
0089: * Represents prefix to logged severe
0090: */
0091: protected static String SEVERE_PREFIX = null;
0092:
0093: /**
0094: * Represents prefix to logged warning
0095: */
0096: protected static String WARNING_PREFIX = null;
0097:
0098: /**
0099: * Represents prefix to logged info
0100: */
0101: protected static String INFO_PREFIX = null;
0102:
0103: /**
0104: * Represents prefix to logged config
0105: */
0106: protected static String CONFIG_PREFIX = null;
0107:
0108: /**
0109: * Represents prefix to logged fine
0110: */
0111: protected static String FINE_PREFIX = null;
0112:
0113: /**
0114: * Represents prefix to logged finer
0115: */
0116: protected static String FINER_PREFIX = null;
0117:
0118: /**
0119: * Represents prefix to logged finest
0120: */
0121: protected static String FINEST_PREFIX = null;
0122:
0123: /**
0124: * Cached TopLink prefix string.
0125: */
0126: protected static String TOPLINK_PREFIX = null;
0127:
0128: /**
0129: * Connection string
0130: */
0131: protected static final String CONNECTION_STRING = "Connection";
0132:
0133: /**
0134: * Thread string
0135: */
0136: protected static final String THREAD_STRING = "Thread";
0137:
0138: /**
0139: * Represents the writer that will receive the formatted log entries
0140: */
0141: protected Writer writer;
0142:
0143: /**
0144: * Format use to print the current date/time.
0145: */
0146: protected DateFormat dateFormat;
0147:
0148: /**
0149: * Allows the printing of the stack to be explictitly disabled/enabled.
0150: * CR #3870467.
0151: * null value is default behavoir of determining from log level.
0152: */
0153: protected Boolean shouldLogExceptionStackTrace;
0154:
0155: /**
0156: * Allows the printing of the date to be explictitly disabled/enabled.
0157: * CR #3870467.
0158: * null value is default behavoir of determining from log level.
0159: */
0160: protected Boolean shouldPrintDate;
0161:
0162: /**
0163: * Allows the printing of the thread to be explictitly disabled/enabled.
0164: * CR #3870467.
0165: * null value is default behavoir of determining from log level.
0166: */
0167: protected Boolean shouldPrintThread;
0168:
0169: /**
0170: * Allows the printing of the session to be explictitly disabled/enabled.
0171: * CR #3870467.
0172: * null value is default behavoir of determining from log level.
0173: */
0174: protected Boolean shouldPrintSession;
0175:
0176: /**
0177: * Allows the printing of the connection to be explictitly disabled/enabled.
0178: * CR #4157545.
0179: * null value is default behavoir of determining from log level.
0180: */
0181: protected Boolean shouldPrintConnection;
0182:
0183: /**
0184: * PUBLIC:
0185: * Create a new AbstractSessionLog
0186: */
0187: public AbstractSessionLog() {
0188: this .writer = new PrintWriter(System.out);
0189: }
0190:
0191: /**
0192: * PUBLIC:
0193: * <p>
0194: * Return the log level. It is used when session is not available.
0195: * </p><p>
0196: *
0197: * @return the log level
0198: * </p>
0199: */
0200: public int getLevel() {
0201: return getLevel(null);
0202: }
0203:
0204: /**
0205: * PUBLIC:
0206: * <p>
0207: * Return the log level for the category name space.
0208: * </p><p>
0209: *
0210: * @return the log level
0211: * </p><p>
0212: * @param category the string representation of a TopLink category, e.g. "sql", "transaction" ...
0213: * </p>
0214: */
0215: public int getLevel(String category) {
0216: return level;
0217: }
0218:
0219: /**
0220: * PUBLIC:
0221: * <p>
0222: * Set the log level. It is used when session is not available.
0223: * </p><p>
0224: *
0225: * @param level the new log level
0226: * </p>
0227: */
0228: public void setLevel(int level) {
0229: setLevel(level, null);
0230: }
0231:
0232: /**
0233: * PUBLIC:
0234: * <p>
0235: * Set the log level for the category name space.
0236: * </p><p>
0237: *
0238: * @param level the new log level
0239: * @param category the string representation of a TopLink category, e.g. "sql", "transaction" ...
0240: * </p>
0241: */
0242: public void setLevel(int level, String category) {
0243: this .level = level;
0244: }
0245:
0246: /**
0247: * PUBLIC:
0248: * <p>
0249: * Check if a message of the given level would actually be logged.
0250: * It is used when session is not available.
0251: * </p><p>
0252: *
0253: * @return true if the given message level will be logged
0254: * </p><p>
0255: * @param level the log request level
0256: * </p>
0257: */
0258: public boolean shouldLog(int level) {
0259: return shouldLog(level, null);
0260: }
0261:
0262: /**
0263: * PUBLIC:
0264: * <p>
0265: * Check if a message of the given level would actually be logged for the category name space.
0266: * !isOff() is checked to screen out the possibility when both
0267: * log level and log request level are set to OFF.
0268: * </p><p>
0269: *
0270: * @return true if the given message level will be logged
0271: * </p><p>
0272: * @param level the log request level
0273: * @param category the string representation of a TopLink category, e.g. "sql", "transaction" ...* </p>
0274: * </p>
0275: */
0276: public boolean shouldLog(int level, String category) {
0277: return (this .level <= level) && !isOff();
0278: }
0279:
0280: /**
0281: * PUBLIC:
0282: * <p>
0283: * Return the singleton SessionLog. If the singleton SessionLog does not exist,
0284: * a new one is created based on the version of JDK being used from the Version class.
0285: * </p><p>
0286: *
0287: * @return the singleton SessionLog
0288: * </p>
0289: */
0290: public static SessionLog getLog() {
0291: if (defaultLog == null) {
0292: defaultLog = new DefaultSessionLog();
0293: }
0294: return defaultLog;
0295: }
0296:
0297: /**
0298: * PUBLIC:
0299: * <p>
0300: * Set the singleton SessionLog.
0301: * </p>
0302: *
0303: * @param sessionLog a SessionLog
0304: * </p>
0305: */
0306: public static void setLog(SessionLog sessionLog) {
0307: defaultLog = sessionLog;
0308: defaultLog.setSession(null);
0309: }
0310:
0311: /**
0312: * PUBLIC:
0313: * <p>
0314: * Get the session.
0315: * </p>
0316: *
0317: * @return session
0318: * </p>
0319: */
0320: public Session getSession() {
0321: return this .session;
0322: }
0323:
0324: /**
0325: * PUBLIC:
0326: * <p>
0327: * Set the session.
0328: * </p>
0329: *
0330: * @param session a Session
0331: * </p>
0332: */
0333: public void setSession(Session session) {
0334: if (this .session == null) {
0335: this .session = session;
0336: buildSessionType();
0337: buildSessionHashCode();
0338: }
0339: }
0340:
0341: /**
0342: * PUBLIC:
0343: * <p>
0344: * Log a message that does not need to be translated. This method is intended for
0345: * external use when logging messages are wanted within the TopLink output.
0346: * </p><p>
0347: *
0348: * @param level the log request level value
0349: * </p><p>
0350: * @param message the string message
0351: * </p>
0352: */
0353: public void log(int level, String message) {
0354: if (!shouldLog(level)) {
0355: return;
0356: }
0357: //Bug#4566524 Pass in false for external use
0358: log(level, message, (Object[]) null, false);
0359: }
0360:
0361: /**
0362: * INTERNAL:
0363: * <p>
0364: * Log a message with one parameter that needs to be translated.
0365: * </p><p>
0366: *
0367: * @param level the log request level value
0368: * </p><p>
0369: * @param message the string message
0370: * </p><p>
0371: * @param param a parameter of the message
0372: * </p>
0373: */
0374: public void log(int level, String message, Object param) {
0375: if (!shouldLog(level)) {
0376: return;
0377: }
0378: log(level, message, new Object[] { param });
0379: }
0380:
0381: /**
0382: * INTERNAL:
0383: * <p>
0384: * Log a message with two parameters that needs to be translated.
0385: * </p><p>
0386: *
0387: * @param level the log request level value
0388: * </p><p>
0389: * @param message the string message
0390: * </p><p>
0391: * @param param1 a parameter of the message
0392: * </p><p>
0393: * @param param2 second parameter of the message
0394: * </p>
0395: */
0396: public void log(int level, String message, Object param1,
0397: Object param2) {
0398: if (!shouldLog(level)) {
0399: return;
0400: }
0401: log(level, message, new Object[] { param1, param2 });
0402: }
0403:
0404: /**
0405: * INTERNAL:
0406: * <p>
0407: * Log a message with three parameters that needs to be translated.
0408: * </p><p>
0409: *
0410: * @param level the log request level value
0411: * </p><p>
0412: * @param message the string message
0413: * </p><p>
0414: * @param param1 a parameter of the message
0415: * </p><p>
0416: * @param param2 second parameter of the message
0417: * </p><p>
0418: * @param param3 third parameter of the message
0419: * </p>
0420: */
0421: public void log(int level, String message, Object param1,
0422: Object param2, Object param3) {
0423: if (!shouldLog(level)) {
0424: return;
0425: }
0426: log(level, message, new Object[] { param1, param2, param3 });
0427: }
0428:
0429: /**
0430: * INTERNAL:
0431: * <p>
0432: * Log a message with an array of parameters that needs to be translated.
0433: * </p><p>
0434: *
0435: * @param level the log request level value
0436: * </p><p>
0437: * @param message the string message
0438: * </p><p>
0439: * @param params array of parameters to the message
0440: * </p>
0441: */
0442: public void log(int level, String message, Object[] params) {
0443: log(level, message, params, true);
0444: }
0445:
0446: /**
0447: * INTERNAL:
0448: * <p>
0449: * Log a message. shouldTranslate determines if the message needs to be translated.
0450: * </p><p>
0451: *
0452: * @param level the log request level
0453: * </p><p>
0454: * @param message the string message
0455: * </p><p>
0456: * @param params array of parameters to the message
0457: * </p><p>
0458: * @param shouldTranslate true if the message needs to be translated
0459: * </p>
0460: */
0461: public void log(int level, String message, Object[] params,
0462: boolean shouldTranslate) {
0463: if (!shouldLog(level)) {
0464: return;
0465: }
0466: log(new SessionLogEntry(level, null, message, params, null,
0467: shouldTranslate));
0468: }
0469:
0470: /**
0471: * PUBLIC:
0472: * <p>
0473: * Log a SessionLogEntry
0474: * </p><p>
0475: *
0476: * @param entry SessionLogEntry that holds all the information for a TopLink logging event
0477: * </p>
0478: */
0479: public abstract void log(SessionLogEntry sessionLogEntry);
0480:
0481: /**
0482: * By default the session (and its connection is available) are printed,
0483: * this can be turned off.
0484: */
0485: public boolean shouldPrintSession() {
0486: return (shouldPrintSession == null)
0487: || shouldPrintSession.booleanValue();
0488: }
0489:
0490: /**
0491: * By default the session (and its connection is available) are printed,
0492: * this can be turned off.
0493: */
0494: public void setShouldPrintSession(boolean shouldPrintSession) {
0495: if (shouldPrintSession) {
0496: this .shouldPrintSession = Boolean.TRUE;
0497: } else {
0498: this .shouldPrintSession = Boolean.FALSE;
0499: }
0500: }
0501:
0502: /**
0503: * By default the connection is printed, this can be turned off.
0504: */
0505: public boolean shouldPrintConnection() {
0506: return (shouldPrintConnection == null)
0507: || shouldPrintConnection.booleanValue();
0508: }
0509:
0510: /**
0511: * By default the connection is printed, this can be turned off.
0512: */
0513: public void setShouldPrintConnection(boolean shouldPrintConnection) {
0514: if (shouldPrintConnection) {
0515: this .shouldPrintConnection = Boolean.TRUE;
0516: } else {
0517: this .shouldPrintConnection = Boolean.FALSE;
0518: }
0519: }
0520:
0521: /**
0522: * By default the stack is logged for FINER or less (finest).
0523: * The logging of the stack can also be explicitly turned on or off.
0524: */
0525: public boolean shouldLogExceptionStackTrace() {
0526: if (shouldLogExceptionStackTrace == null) {
0527: return getLevel() <= FINER;
0528: } else {
0529: return shouldLogExceptionStackTrace.booleanValue();
0530: }
0531: }
0532:
0533: /**
0534: * By default the stack is logged for FINER or less (finest).
0535: * The logging of the stack can also be explicitly turned on or off.
0536: */
0537: public void setShouldLogExceptionStackTrace(
0538: boolean shouldLogExceptionStackTrace) {
0539: if (shouldLogExceptionStackTrace) {
0540: this .shouldLogExceptionStackTrace = Boolean.TRUE;
0541: } else {
0542: this .shouldLogExceptionStackTrace = Boolean.FALSE;
0543: }
0544: }
0545:
0546: /**
0547: * By default the date is always printed, but can be turned off.
0548: */
0549: public boolean shouldPrintDate() {
0550: return (shouldPrintDate == null)
0551: || (shouldPrintDate.booleanValue());
0552: }
0553:
0554: /**
0555: * By default the date is always printed, but can be turned off.
0556: */
0557: public void setShouldPrintDate(boolean shouldPrintDate) {
0558: if (shouldPrintDate) {
0559: this .shouldPrintDate = Boolean.TRUE;
0560: } else {
0561: this .shouldPrintDate = Boolean.FALSE;
0562: }
0563: }
0564:
0565: /**
0566: * By default the thread is logged for FINE or less (finer,etc.).
0567: * The logging of the thread can also be explicitly turned on or off.
0568: */
0569: public boolean shouldPrintThread() {
0570: if (shouldPrintThread == null) {
0571: return getLevel() <= FINE;
0572: } else {
0573: return shouldPrintThread.booleanValue();
0574: }
0575: }
0576:
0577: /**
0578: * By default the thread is logged for FINE or less (finer,etc.).
0579: * The logging of the thread can also be explicitly turned on or off.
0580: */
0581: public void setShouldPrintThread(boolean shouldPrintThread) {
0582: if (shouldPrintThread) {
0583: this .shouldPrintThread = Boolean.TRUE;
0584: } else {
0585: this .shouldPrintThread = Boolean.FALSE;
0586: }
0587: }
0588:
0589: /**
0590: * PUBLIC:
0591: * <p>
0592: * Return the writer that will receive the formatted log entries.
0593: * </p><p>
0594: *
0595: * @return the log writer
0596: * </p>
0597: */
0598: public Writer getWriter() {
0599: return writer;
0600: }
0601:
0602: /**
0603: * PUBLIC:
0604: * <p>
0605: * Set the writer that will receive the formatted log entries.
0606: * </p><p>
0607: *
0608: * @param writer the log writer
0609: * </p>
0610: */
0611: public void setWriter(Writer writer) {
0612: this .writer = writer;
0613: }
0614:
0615: /**
0616: * Build and return a date format.
0617: * The default is a format that is sortable and easily parsed.
0618: * @return the date format
0619: */
0620: protected DateFormat buildDefaultDateFormat() {
0621: return new SimpleDateFormat("yyyy.MM.dd hh:mm:ss.SSS");
0622: }
0623:
0624: /**
0625: * PUBLIC:
0626: * Return the date format to be used when printing a log entry date.
0627: * @return the date format
0628: */
0629: public DateFormat getDateFormat() {
0630: if (dateFormat == null) {
0631: dateFormat = this .buildDefaultDateFormat();
0632: }
0633: return dateFormat;
0634: }
0635:
0636: /**
0637: * Return the specified date and/or time information in string.
0638: * The format will be determined by the date format settings.
0639: */
0640: protected String getDateString(Date date) {
0641: return this .getDateFormat().format(date);
0642: }
0643:
0644: /**
0645: * Return the supplement detail information including date, session, thread and connection.
0646: */
0647: protected String getSupplementDetailString(SessionLogEntry entry) {
0648: StringWriter writer = new StringWriter();
0649:
0650: if (shouldPrintDate()) {
0651: writer.write(getDateString(entry.getDate()));
0652: writer.write("--");
0653: }
0654: if (shouldPrintSession() && (entry.getSession() != null)) {
0655: writer.write(this .getSessionString(entry.getSession()));
0656: writer.write("--");
0657: }
0658: if (shouldPrintConnection() && (entry.getConnection() != null)) {
0659: writer.write(this
0660: .getConnectionString(entry.getConnection()));
0661: writer.write("--");
0662: }
0663: if (shouldPrintThread()) {
0664: writer.write(this .getThreadString(entry.getThread()));
0665: writer.write("--");
0666: }
0667: return writer.toString();
0668: }
0669:
0670: /**
0671: * Return the current session including the type and id.
0672: */
0673: protected String getSessionString(Session session) {
0674: // For bug 3422759 the session to log against should be the one in the
0675: // event, not the static one in the SessionLog, for there are many
0676: // sessions but only one SessionLog.
0677: if (session != null) {
0678: return ((AbstractSession) session).getLogSessionString();
0679: } else {
0680: return getSessionString();
0681: }
0682: }
0683:
0684: /**
0685: * Return the current session type.
0686: */
0687: protected void buildSessionType() {
0688: if (session != null) {
0689: sessionType = ((AbstractSession) session)
0690: .getSessionTypeString();
0691: } else {
0692: sessionType = null;
0693: }
0694: }
0695:
0696: /**
0697: * Return the current session hash code.
0698: */
0699: protected void buildSessionHashCode() {
0700: if (session != null) {
0701: sessionHashCode = String.valueOf(System
0702: .identityHashCode(session));
0703: } else {
0704: sessionHashCode = null;
0705: }
0706: }
0707:
0708: /**
0709: * Return the current session including the type and id.
0710: */
0711: protected String getSessionString() {
0712: return sessionType + "(" + sessionHashCode + ")";
0713: }
0714:
0715: /**
0716: * Return the specified connection information.
0717: */
0718: protected String getConnectionString(Accessor connection) {
0719: // Bug 3630182 - if possible, print the actual connection's hashcode instead of just the accessor
0720: if (connection.getDatasourceConnection() == null) {
0721: return CONNECTION_STRING
0722: + "("
0723: + String.valueOf(System
0724: .identityHashCode(connection)) + ")";
0725: } else {
0726: return CONNECTION_STRING
0727: + "("
0728: + String.valueOf(System.identityHashCode(connection
0729: .getDatasourceConnection())) + ")";
0730: }
0731: }
0732:
0733: /**
0734: * Return the specified thread information.
0735: */
0736: protected String getThreadString(Thread thread) {
0737: return THREAD_STRING + "(" + String.valueOf(thread) + ")";
0738: }
0739:
0740: /**
0741: * Print the prefix string representing TopLink logging
0742: */
0743:
0744: //Bug3135111 Prefix strings are not translated until the first time they are used.
0745: protected void printPrefixString(int level) {
0746: try {
0747: switch (level) {
0748: case SEVERE:
0749: if (SEVERE_PREFIX == null) {
0750: SEVERE_PREFIX = LoggingLocalization
0751: .buildMessage("toplink_severe");
0752: }
0753: this .getWriter().write(SEVERE_PREFIX);
0754: break;
0755: case WARNING:
0756: if (WARNING_PREFIX == null) {
0757: WARNING_PREFIX = LoggingLocalization
0758: .buildMessage("toplink_warning");
0759: }
0760: this .getWriter().write(WARNING_PREFIX);
0761: break;
0762: case INFO:
0763: if (INFO_PREFIX == null) {
0764: INFO_PREFIX = LoggingLocalization
0765: .buildMessage("toplink_info");
0766: }
0767: this .getWriter().write(INFO_PREFIX);
0768: break;
0769: case CONFIG:
0770: if (CONFIG_PREFIX == null) {
0771: CONFIG_PREFIX = LoggingLocalization
0772: .buildMessage("toplink_config");
0773: }
0774: this .getWriter().write(CONFIG_PREFIX);
0775: break;
0776: case FINE:
0777: if (FINE_PREFIX == null) {
0778: FINE_PREFIX = LoggingLocalization
0779: .buildMessage("toplink_fine");
0780: }
0781: this .getWriter().write(FINE_PREFIX);
0782: break;
0783: case FINER:
0784: if (FINER_PREFIX == null) {
0785: FINER_PREFIX = LoggingLocalization
0786: .buildMessage("toplink_finer");
0787: }
0788: this .getWriter().write(FINER_PREFIX);
0789: break;
0790: case FINEST:
0791: if (FINEST_PREFIX == null) {
0792: FINEST_PREFIX = LoggingLocalization
0793: .buildMessage("toplink_finest");
0794: }
0795: this .getWriter().write(FINEST_PREFIX);
0796: break;
0797: default:
0798: if (TOPLINK_PREFIX == null) {
0799: TOPLINK_PREFIX = LoggingLocalization
0800: .buildMessage("toplink");
0801: }
0802: this .getWriter().write(TOPLINK_PREFIX);
0803: }
0804: } catch (IOException exception) {
0805: throw ValidationException.logIOError(exception);
0806: }
0807: }
0808:
0809: /**
0810: * PUBLIC:
0811: * Set the date format to be used when
0812: * printing a log entry date.
0813: * @param dateFormat java.text.DateFormat
0814: */
0815: public void setDateFormat(DateFormat dateFormat) {
0816: this .dateFormat = dateFormat;
0817: }
0818:
0819: /**
0820: * Return the formatted message based on the information from the given SessionLogEntry.
0821: * The message will either be translated and formatted or formatted only depending
0822: * on if the shouldTranslate flag is set to true of false.
0823: */
0824: protected String formatMessage(SessionLogEntry entry) {
0825: String message = entry.getMessage();
0826: if (entry.shouldTranslate()) {
0827: if (entry.getLevel() > FINE) {
0828: message = LoggingLocalization.buildMessage(message,
0829: entry.getParameters());
0830: } else {
0831: message = TraceLocalization.buildMessage(message, entry
0832: .getParameters());
0833: }
0834: } else {
0835: //bug#2988,if there are entry parameters and the string "{0" contained in the message
0836: //body, we assume it needs to be formatted.
0837: if (entry.getParameters() != null
0838: && entry.getParameters().length > 0
0839: && message.indexOf("{0") >= 0) {
0840: message = java.text.MessageFormat.format(message, entry
0841: .getParameters());
0842: }
0843: }
0844: return message;
0845: }
0846:
0847: /**
0848: * PUBLIC:
0849: * <p>
0850: * Log a throwable at FINER level.
0851: * </p><p>
0852: *
0853: * @param throwable a Throwable
0854: * </p>
0855: */
0856: public void throwing(Throwable throwable) {
0857: if (shouldLog(FINER)) {
0858: SessionLogEntry entry = new SessionLogEntry(null, throwable);
0859: entry.setLevel(FINER);
0860: log(entry);
0861: }
0862: }
0863:
0864: /**
0865: * INTERNAL:
0866: * Translate the string value of the log level to the constant value.
0867: * If value is null or invalid use the default.
0868: */
0869: public static int translateStringToLoggingLevel(String loggingLevel) {
0870: if (loggingLevel == null) {
0871: return INFO;
0872: } else if (loggingLevel.equals("OFF")) {
0873: return OFF;
0874: } else if (loggingLevel.equals("SEVERE")) {
0875: return SEVERE;
0876: } else if (loggingLevel.equals("WARNING")) {
0877: return WARNING;
0878: } else if (loggingLevel.equals("INFO")) {
0879: return INFO;
0880: } else if (loggingLevel.equals("CONFIG")) {
0881: return CONFIG;
0882: } else if (loggingLevel.equals("FINE")) {
0883: return FINE;
0884: } else if (loggingLevel.equals("FINER")) {
0885: return FINER;
0886: } else if (loggingLevel.equals("FINEST")) {
0887: return FINEST;
0888: } else if (loggingLevel.equals("ALL")) {
0889: return ALL;
0890: }
0891: return INFO;
0892: }
0893:
0894: /**
0895: * INTERNAL:
0896: * Translate the string value of the log level to the constant value.
0897: * If value is null or invalid use the default.
0898: */
0899: public static String translateLoggingLevelToString(int loggingLevel) {
0900: if (loggingLevel == OFF) {
0901: return "OFF";
0902: } else if (loggingLevel == SEVERE) {
0903: return "SEVERE";
0904: } else if (loggingLevel == WARNING) {
0905: return "WARNING";
0906: } else if (loggingLevel == INFO) {
0907: return "INFO";
0908: } else if (loggingLevel == CONFIG) {
0909: return "CONFIG";
0910: } else if (loggingLevel == FINE) {
0911: return "FINE";
0912: } else if (loggingLevel == FINER) {
0913: return "FINER";
0914: } else if (loggingLevel == FINEST) {
0915: return "FINEST";
0916: } else if (loggingLevel == ALL) {
0917: return "ALL";
0918: }
0919: return "INFO";
0920: }
0921:
0922: /**
0923: * PUBLIC:
0924: * <p>
0925: * This method is called when a severe level message needs to be logged.
0926: * The message will be translated
0927: * </p><p>
0928: *
0929: * @param message the message key
0930: * </p>
0931: */
0932: public void severe(String message) {
0933: log(SEVERE, message, (Object[]) null);
0934: }
0935:
0936: /**
0937: * PUBLIC:
0938: * <p>
0939: * This method is called when a warning level message needs to be logged.
0940: * The message will be translated
0941: * </p><p>
0942: *
0943: * @param message the message key
0944: * </p>
0945: */
0946: public void warning(String message) {
0947: log(WARNING, message, (Object[]) null);
0948: }
0949:
0950: /**
0951: * PUBLIC:
0952: * <p>
0953: * This method is called when a info level message needs to be logged.
0954: * The message will be translated
0955: * </p><p>
0956: *
0957: * @param message the message key
0958: * </p>
0959: */
0960: public void info(String message) {
0961: log(INFO, message, (Object[]) null);
0962: }
0963:
0964: /**
0965: * PUBLIC:
0966: * <p>
0967: * This method is called when a config level message needs to be logged.
0968: * The message will be translated
0969: * </p><p>
0970: *
0971: * @param message the message key
0972: * </p>
0973: */
0974: public void config(String message) {
0975: log(CONFIG, message, (Object[]) null);
0976: }
0977:
0978: /**
0979: * PUBLIC:
0980: * <p>
0981: * This method is called when a fine level message needs to be logged.
0982: * The message will be translated
0983: * </p><p>
0984: *
0985: * @param message the message key
0986: * </p>
0987: */
0988: public void fine(String message) {
0989: log(FINE, message, (Object[]) null);
0990: }
0991:
0992: /**
0993: * PUBLIC:
0994: * <p>
0995: * This method is called when a finer level message needs to be logged.
0996: * The message will be translated
0997: * </p><p>
0998: *
0999: * @param message the message key
1000: * </p>
1001: */
1002: public void finer(String message) {
1003: log(FINER, message, (Object[]) null);
1004: }
1005:
1006: /**
1007: * PUBLIC:
1008: * <p>
1009: * This method is called when a finest level message needs to be logged.
1010: * The message will be translated
1011: * </p><p>
1012: *
1013: * @param message the message key
1014: * </p>
1015: */
1016: public void finest(String message) {
1017: log(FINEST, message, (Object[]) null);
1018: }
1019:
1020: /**
1021: * PUBLIC:
1022: * <p>
1023: * Log a throwable with level.
1024: * </p><p>
1025: *
1026: * @param level the log request level value
1027: * </p><p>
1028: * @param throwable a Throwable
1029: * </p>
1030: */
1031: public void logThrowable(int level, Throwable throwable) {
1032: // Must not create the log if not logging as is a performance issue.
1033: if (shouldLog(level)) {
1034: log(new SessionLogEntry(null, level, null, throwable));
1035: }
1036: }
1037:
1038: /**
1039: * PUBLIC:
1040: * Check if the log level is set to off.
1041: */
1042: public boolean isOff() {
1043: return this .level == OFF;
1044: }
1045:
1046: /**
1047: * INTERNAL:
1048: * Each session owns its own session log because session is stored in the session log
1049: */
1050: public Object clone() {
1051: try {
1052: return super .clone();
1053: } catch (Exception exception) {
1054: return null;
1055: }
1056: }
1057: }
|