001: /*
002: * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.ubt;
006:
007: import com.sun.portal.log.common.PortalLogger;
008:
009: import java.util.logging.*;
010: import java.util.Hashtable;
011: import java.util.Date;
012: import java.util.Vector;
013: import java.text.SimpleDateFormat;
014:
015: /**
016: * This is a simple ELF formatter meant for UBTLogRecord.
017: * This formats various different fields of UBTLogRecord into a tab seperated
018: * String. The fields appear in the order that is given by getHead(Handler)
019: * method. If a field does not have any value or is not part of UBTLogRecord
020: * it is formatted as '-'.
021: * <p>
022: * There is no message to localize. So formatMessage is not used.
023: * If needed can be delegated to the super class
024: */
025: public class UBTELFFormatter extends Formatter {
026: private static Logger logger = PortalLogger
027: .getLogger(UBTELFFormatter.class);
028: private SimpleDateFormat dateFormat = new SimpleDateFormat(
029: "yyyy.MM.dd G 'at' HH:mm:ss z");
030: private String fieldDelimiter = "\t";
031: private String fieldEnclosure = "";
032: private String fieldNull = "-";
033: private String lineSeparator = (String) java.security.AccessController
034: .doPrivileged(new sun.security.action.GetPropertyAction(
035: "line.separator"));
036: private Vector fields;
037:
038: /**
039: * Gets the representation of a null fields in this formatter.
040: */
041: public String getFieldNull() {
042: return fieldNull;
043: }
044:
045: /**
046: * Gets the date format that is used by this formatter.
047: */
048: public SimpleDateFormat getDateFormat() {
049: return dateFormat;
050: }
051:
052: /**
053: * Gets the string used to enclose the fields, if any.
054: * @return String field enclosure
055: */
056: public String getFieldEnclosure() {
057: return fieldEnclosure;
058: }
059:
060: /**
061: * Gets the field delimiter in the formatted message.
062: * @return String field delimiter
063: */
064: public String getFieldDelimiter() {
065: return fieldDelimiter;
066: }
067:
068: /**
069: * Gets the head message of this formatter.
070: * A handler uses this method to log the head message of its formatter
071: * whenever it opens handler to log messages.
072: * The head message of this formatter represents the order of appearence
073: * of the fields for a log level.
074: * Field identifiers are listed based on the handler's log level.
075: * @return String Head message
076: * @param handler Handler to which this formatter is added.
077: */
078: public String getHead(Handler handler) {
079: StringBuffer sbuffer = new StringBuffer();
080: if (handler instanceof java.util.logging.FileHandler) {
081: sbuffer.append("#Version: 1.0").append(lineSeparator);
082: sbuffer.append("#Date: ").append(
083: dateFormat.format(new Date()))
084: .append(lineSeparator);
085: sbuffer.append("#Fields: ").append(lineSeparator);
086: //this could have been based on the handler's level. Since, details are based on
087: // the log level, field details of all possible levels are mentioned in the log file
088: //Also it possible that the logger levels are modified dynamically
089: sbuffer.append("#Level INFO: ").append(
090: getFieldIdentifiers(Level.INFO)).append(
091: lineSeparator);
092: sbuffer.append("#Level FINE: ").append(
093: getFieldIdentifiers(Level.FINE)).append(
094: lineSeparator);
095: sbuffer.append("#Level FINER: ").append(
096: getFieldIdentifiers(Level.FINER)).append(
097: lineSeparator);
098: //'!' in the following line is used for deciding on the file schema dynamically. So don't change it
099: sbuffer.append("!Level FINEST: ").append(
100: getFieldIdentifiers(Level.FINEST)).append(
101: lineSeparator);
102: }
103: return sbuffer.toString();
104: }
105:
106: private void getLogFields(Level level) {
107: fields = UBTLogManager.getInstance().getLogFields(level);
108: }
109:
110: private String getFieldIdentifiers(Level level) {
111: StringBuffer fieldBuff = new StringBuffer();
112: getLogFields(level);
113: for (int i = 0; i < fields.size(); i++) {
114: fieldBuff.append(fields.get(i)).append(fieldDelimiter);
115: }
116: return fieldBuff.toString();
117: }
118:
119: /**
120: * Gets the tail message of the formatter.
121: * A handler calls this method before closing itself.
122: * In this formatter, this message provides the time of the call.
123: * @return String Tail message
124: * @param handler Handler to which this formatter is added.
125: */
126: public String getTail(Handler handler) {
127: return "#Closed...Date: " + dateFormat.format(new Date())
128: + lineSeparator;
129: }
130:
131: /**
132: * Method to format the message of record.
133: * In this formatter, record is expected to be UBTLogRecord and on that
134: * record, getLogFieldTable() is called to get the HashTable of field
135: * name value pair.
136: * @return String formatted message
137: * @param record LogRecord message of which need to be formatted.
138: */
139: public synchronized String format(LogRecord record) {
140: // this is done twice - once here and once in getHead(..)
141: // since format is called first and getHead() is called later
142: // with LogRecord and handler as parameters. Handler would not let
143: // any LogRecord go through to formatter if its level is lower than
144: // that of record's. So, getHead() should display Handler's level fields.
145: // and accordingly the record should be formatted
146: getLogFields(record.getLevel());
147: StringBuffer logbuff = new StringBuffer();
148: try {
149: Hashtable logInfoTable = null;
150: UBTLogRecord rec = ((UBTLogRecord) record);
151: logInfoTable = rec.getLogFieldTable();
152: for (int i = 0; i < fields.size(); i++) {
153: Object val = logInfoTable.get(fields.get(i));
154: logbuff.append(fieldEnclosure);
155: if (val != null)
156: logbuff.append(val.toString());
157: else
158: logbuff.append(fieldNull);
159: logbuff.append(fieldEnclosure).append(fieldDelimiter);
160: }
161: } catch (Exception e) {
162: //something went wrong.
163: logger.log(Level.INFO, "PSUB_CSPU0001", e);
164: }
165: if (logbuff.length() > 0)
166: //introduced check when a newline
167: //is displayed even if the event level is OFF / lower than logger's level
168: logbuff.append(lineSeparator);
169: return logbuff.toString();
170: }
171: }
|