View Javadoc

1   // Copyright 2006-2007, by the California Institute of Technology.
2   // ALL RIGHTS RESERVED. United States Government Sponsorship acknowledged.
3   // Any commercial use must be negotiated with the Office of Technology Transfer
4   // at the California Institute of Technology.
5   //
6   // This software is subject to U. S. export control laws and regulations
7   // (22 C.F.R. 120-130 and 15 C.F.R. 730-774). To the extent that the software
8   // is subject to U.S. export control laws and regulations, the recipient has
9   // the responsibility to obtain export licenses or other export authority as
10  // may be required before exporting such information to foreign countries or
11  // providing access to foreign nationals.
12  //
13  // $Id$ 
14  //
15  
16  package gov.nasa.pds.tools.logging;
17  
18  import java.util.logging.Formatter;
19  import java.util.logging.Handler;
20  import java.util.logging.LogRecord;
21  import java.util.logging.Level;
22  
23  /***
24   * @author pramirez
25   * @version $Revision$
26   * 
27   */
28  public class ToolsLogFormatter extends Formatter {
29  
30      /* (non-Javadoc)
31       * @see java.util.logging.Formatter#format(java.util.logging.LogRecord)
32       */
33      public String format(LogRecord record) {
34          StringBuffer buffer = new StringBuffer();
35          ToolsLogRecord toolsRecord = (ToolsLogRecord) record;
36          buffer.append(" <record>\n");
37          if (record.getLevel() == Level.SEVERE)
38              buffer.append("  <level>ERROR</level>\n");
39          else
40              buffer.append("  <level>" + record.getLevel() + "</level>\n");
41          if (toolsRecord.getFile() != null)
42              buffer.append("  <file>" + toolsRecord.getFile() + "</file>\n");
43          if (toolsRecord.getLine() != -1)
44              buffer.append("  <line>" + toolsRecord.getLine() + "</line>\n");
45          if (toolsRecord.getContext() != null)
46              buffer.append("  <context>" + toolsRecord.getContext() + "</context>\n");
47          buffer.append("  <message><![CDATA[" + record.getMessage() + "]]></message>\n");
48          buffer.append(" </record>\n");
49          return buffer.toString();
50      }
51      
52      public String getHead(Handler handler) {
53          StringBuffer buffer = new StringBuffer();
54          buffer.append("<?xml version=\"1.0\" ?>\n");
55          buffer.append("<log>\n");
56          return buffer.toString();
57      }
58  
59      public String getTail(Handler handler) {
60          return "</log>";
61      }
62  }