1 16 package org.apache.log4j.gui.examples; 17 18 import javax.swing.*; 19 import java.awt.*; 20 import java.awt.event.*; 21 22 import org.apache.log4j.*; 23 import org.apache.log4j.gui.TextPanelAppender; 24 25 public class LogTextPanelExample { 26 boolean packFrame = false; 27 28 String catName = "dum.cat.name"; 29 30 public LogTextPanelExample() { 31 32 TextPanelAppender tpa = new TextPanelAppender(new PatternLayout("%-5p %d [%t]: %m%n"), "logTextPanel"); 34 tpa.setThreshold(Priority.DEBUG); 35 Category cat = Category.getInstance(catName); 36 cat.addAppender(tpa); 37 38 LogFrame frame = new LogFrame(tpa); 39 frame.validate(); 40 41 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 43 Dimension frameSize = frame.getSize(); 44 if (frameSize.height > screenSize.height) { 45 frameSize.height = screenSize.height; 46 } 47 if (frameSize.width > screenSize.width) { 48 frameSize.width = screenSize.width; 49 } 50 frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); 51 frame.setVisible(true); 52 } 53 54 55 public static void main(String [] args) { 56 try { 57 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 58 } 59 catch(Exception e) { 60 e.printStackTrace(); 61 } 62 LogTextPanelExample foo = new LogTextPanelExample(); 63 new LogTextPanelExampleGenThread(foo.catName); 64 } 65 } 66 67 class LogFrame extends JFrame { 68 69 public LogFrame(TextPanelAppender tpa) { 70 enableEvents(AWTEvent.WINDOW_EVENT_MASK); 71 JPanel contentPane = (JPanel) this.getContentPane(); 72 contentPane.setLayout(new BorderLayout()); 73 this.setSize(new Dimension(600, 400)); 74 this.setTitle("LogTextPanel Example"); 75 contentPane.add(tpa.getLogTextPanel(), BorderLayout.CENTER); 76 } 77 78 protected void processWindowEvent(WindowEvent e) { 80 super.processWindowEvent(e); 81 if (e.getID() == WindowEvent.WINDOW_CLOSING) { 82 System.exit(0); 83 } 84 } 85 } 86 87 class LogTextPanelExampleGenThread extends Thread { 88 89 String catName; 90 91 public LogTextPanelExampleGenThread(String catName) { 92 this.catName = catName; 93 this.setPriority(Thread.NORM_PRIORITY - 1); 94 this.start(); 95 } 96 97 public void run() { 98 Category cat = Category.getInstance(catName); 99 int cnt = 0; 100 while(true) { 101 cnt++; 102 int randEvt = (int)(Math.random() * 125); 103 if(randEvt < 3) 104 cat.fatal("{" + cnt + "} Something screwed up bad."); 105 else if(randEvt < 10) 106 cat.error("{" + cnt + "} An error occured while trying to delete all of your files."); 107 else if(randEvt < 25) 108 cat.warn("{" + cnt + "} It seems as if your hard disk is getting full."); 109 else if(randEvt < 55) 110 cat.info("{" + cnt + "} It is now time for tea."); 111 else if(randEvt < 65) 112 cat.debug("{" + cnt + "} Something bad is happening on line 565 of com.foo.Crap"); 113 else if(randEvt < 75) 114 cat.debug("{" + cnt + "} Input value for xe343dd is not equal to xe39dfd!"); 115 else if(randEvt < 85) 116 cat.debug("{" + cnt + "} Successfully reached line 2312 of com.foo.Goo"); 117 else if(randEvt < 105) 118 cat.debug("{" + cnt + "} Here is some extra handy debugging information for you."); 119 else if(randEvt < 115) 120 cat.debug("{" + cnt + "} The file you are about to write to is not open."); 121 else if(randEvt < 125) 122 cat.debug("{" + cnt + "} The input value to the method was <null>."); 123 124 try { 125 Thread.sleep(10); 126 } 127 catch(Exception e) {} 128 129 } 130 } 131 } | Popular Tags |