View Javadoc

1   // $Id: StyledLogMessage.java 126 2009-01-13 17:07:04Z agony $
2   /**
3    * 
4    */
5   package de.mindcrimeilab.swing.log4j;
6   
7   import java.awt.Color;
8   import java.util.HashMap;
9   import java.util.Map;
10  
11  import javax.swing.text.AttributeSet;
12  import javax.swing.text.MutableAttributeSet;
13  import javax.swing.text.SimpleAttributeSet;
14  import javax.swing.text.StyleConstants;
15  
16  import org.apache.log4j.Level;
17  
18  /**
19   * @author Michael Engelhardt<me@mindcrime-ilab.de>
20   * @author $Author: agony $
21   * @version $Revision: 126 $
22   * 
23   */
24  public final class StyledLogMessage {
25  
26      private final String formattedLogMessage;
27  
28      private final Level level;
29  
30      private final AttributeSet style;
31  
32      public static MutableAttributeSet STYLE_ERROR = new SimpleAttributeSet();
33  
34      public static MutableAttributeSet STYLE_WARNING = new SimpleAttributeSet();
35  
36      public static MutableAttributeSet STYLE_INFO = new SimpleAttributeSet();
37  
38      public static MutableAttributeSet STYLE_CONFIG = new SimpleAttributeSet();
39  
40      public static MutableAttributeSet STYLE_FINE = new SimpleAttributeSet();
41  
42      public static MutableAttributeSet STYLE_FINER = new SimpleAttributeSet();
43  
44      public static MutableAttributeSet STYLE_FINEST = new SimpleAttributeSet();
45  
46      public static MutableAttributeSet STYLE_DEFAULT = new SimpleAttributeSet();
47  
48      private static final Map<Level, AttributeSet> levelStyle = new HashMap<Level, AttributeSet>();
49  
50      static {
51          String FONTFAMILY = "Verdana";
52          int FONTSIZE = 12;
53  
54          // Log level Error
55          StyleConstants.setFontFamily(StyledLogMessage.STYLE_ERROR, FONTFAMILY);
56          StyleConstants.setFontSize(StyledLogMessage.STYLE_ERROR, FONTSIZE);
57          StyleConstants.setForeground(StyledLogMessage.STYLE_ERROR, Color.red.darker());
58          StyleConstants.setBold(StyledLogMessage.STYLE_ERROR, true);
59  
60          // Log level Info
61          StyleConstants.setFontFamily(StyledLogMessage.STYLE_INFO, FONTFAMILY);
62          StyleConstants.setFontSize(StyledLogMessage.STYLE_INFO, FONTSIZE);
63          StyleConstants.setForeground(StyledLogMessage.STYLE_INFO, Color.green.darker());
64  
65          // Log level fine
66          StyleConstants.setFontFamily(StyledLogMessage.STYLE_FINE, FONTFAMILY);
67          StyleConstants.setFontSize(StyledLogMessage.STYLE_FINE, FONTSIZE - 1);
68          StyleConstants.setForeground(StyledLogMessage.STYLE_FINE, Color.black);
69          StyleConstants.setBold(StyledLogMessage.STYLE_FINE, false);
70  
71          // Log level finer
72          StyleConstants.setFontFamily(StyledLogMessage.STYLE_FINER, FONTFAMILY);
73          StyleConstants.setFontSize(StyledLogMessage.STYLE_FINER, FONTSIZE - 2);
74          StyleConstants.setForeground(StyledLogMessage.STYLE_FINER, Color.black);
75          StyleConstants.setBold(StyledLogMessage.STYLE_FINER, false);
76  
77          // Log level finest
78          StyleConstants.setFontFamily(StyledLogMessage.STYLE_FINEST, FONTFAMILY);
79          StyleConstants.setFontSize(StyledLogMessage.STYLE_FINEST, FONTSIZE - 3);
80          StyleConstants.setForeground(StyledLogMessage.STYLE_FINEST, Color.black);
81          StyleConstants.setBold(StyledLogMessage.STYLE_FINEST, false);
82  
83          // default
84          StyleConstants.setFontFamily(StyledLogMessage.STYLE_DEFAULT, FONTFAMILY);
85          StyleConstants.setFontSize(StyledLogMessage.STYLE_DEFAULT, FONTSIZE);
86          StyleConstants.setForeground(StyledLogMessage.STYLE_DEFAULT, Color.black);
87          StyleConstants.setBold(StyledLogMessage.STYLE_DEFAULT, false);
88  
89          StyledLogMessage.levelStyle.put(Level.ALL, StyledLogMessage.STYLE_FINEST);
90          StyledLogMessage.levelStyle.put(Level.TRACE, StyledLogMessage.STYLE_FINER);
91          StyledLogMessage.levelStyle.put(Level.DEBUG, StyledLogMessage.STYLE_FINE);
92          StyledLogMessage.levelStyle.put(Level.INFO, StyledLogMessage.STYLE_INFO);
93          StyledLogMessage.levelStyle.put(Level.WARN, StyledLogMessage.STYLE_WARNING);
94          StyledLogMessage.levelStyle.put(Level.ERROR, StyledLogMessage.STYLE_ERROR);
95          StyledLogMessage.levelStyle.put(Level.FATAL, StyledLogMessage.STYLE_ERROR);
96      }
97  
98      private StyledLogMessage(Level level, String formattedLogMessage) {
99          this.level = level;
100         this.formattedLogMessage = formattedLogMessage;
101 
102         if (Level.ALL.equals(level)) {
103             style = StyledLogMessage.STYLE_FINEST;
104         }
105         else if (Level.TRACE.equals(level)) {
106             style = StyledLogMessage.STYLE_FINER;
107         }
108         else if (Level.DEBUG.equals(level)) {
109             style = StyledLogMessage.STYLE_FINE;
110         }
111         else if (Level.INFO.equals(level)) {
112             style = StyledLogMessage.STYLE_INFO;
113         }
114         else if (Level.WARN.equals(level)) {
115             style = StyledLogMessage.STYLE_WARNING;
116         }
117         else if (Level.ERROR.equals(level)) {
118             style = StyledLogMessage.STYLE_ERROR;
119         }
120         else if (Level.FATAL.equals(level)) {
121             style = StyledLogMessage.STYLE_ERROR;
122         }
123         else {
124             style = StyledLogMessage.STYLE_DEFAULT;
125         }
126     }
127 
128     public static final StyledLogMessage getLogMessage(Level level, String formattedLogMessage) {
129         return new StyledLogMessage(level, formattedLogMessage);
130     }
131 
132     /**
133      * @return the formattedLogMessage
134      */
135     public String getFormattedLogMessage() {
136         return formattedLogMessage;
137     }
138 
139     /**
140      * @return the level
141      */
142     public Level getLevel() {
143         return level;
144     }
145 
146     /**
147      * @return the style
148      */
149     public AttributeSet getStyle() {
150         return style;
151     }
152 
153 }