View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.student.common.ui.client.logging;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.kuali.student.common.ui.client.application.KSAsyncCallback;
24  import org.kuali.student.common.ui.client.service.LogRpcService;
25  import org.kuali.student.common.ui.client.service.LogRpcServiceAsync;
26  
27  import com.google.gwt.core.client.GWT;
28  import com.google.gwt.user.client.Command;
29  import com.google.gwt.user.client.DeferredCommand;
30  
31  @Deprecated
32  public class Logger {
33  	/**
34  	 * Default log level is LogLevel.WARN
35  	 */
36  	static LogLevel logLevel = LogLevel.WARN;
37  	static LogBuffer buffer = new LogBuffer();
38  	static Map<String, String> clientContextInfo = new HashMap<String, String>();
39  	static int maxBufferSize = Integer.MAX_VALUE;
40  	/**
41  	 * Sets the maximum buffer size, resizing buffer if necessary.
42  	 * Can be an expensive operation, avoid frequent use.
43  	 * @param maxSize
44  	 */
45  	public static void setMaxBufferSize(int maxSize) {
46  		LogBuffer tmp = new LogBuffer(maxSize);
47  		for (LogMessage m : buffer.getLogMessages()) {
48  			tmp.add(m);
49  		}
50  		buffer = tmp;
51  	}
52  	
53  	public static void setLogLevel(LogLevel level) {
54  		logLevel = level;
55  	}
56  	public static LogLevel getLogLevel() {
57  		return logLevel;
58  	}
59  	public static void log(LogMessage message) {
60  		if (message.getLogLevel().getLevel() >= logLevel.getLevel()) {
61  			buffer.add(message);
62  		}
63  	}
64  	
65  	public static void log(LogLevel level, String message) {
66  		if (level.getLevel() >= logLevel.getLevel()) {		
67  			buffer.add(new LogMessage(level, message, (Throwable) null));
68  		}
69  	}
70  	
71  	public static void log(LogLevel level, String message, Throwable error) {
72  		if (level.getLevel() >= logLevel.getLevel()) {		
73  			buffer.add(new LogMessage(level, message, error));
74  		}
75  	}
76  	
77  	public static void debug(String message) {
78  		log(LogLevel.DEBUG, message);
79  	}
80  	public static void debug(String message, Throwable error) {
81  		log(LogLevel.DEBUG, message, error);
82  	}
83  	
84  	public static void info(String message) {
85  		log(LogLevel.INFO, message);
86  	}
87  	public static void info(String message, Throwable error) {
88  		log(LogLevel.INFO, message, error);
89  	}
90  	
91  	public static void warn(String message) {
92  		log(LogLevel.WARN, message);
93  	}
94  	public static void warn(String message, Throwable error) {
95  		log(LogLevel.WARN, message, error);
96  	}
97  	
98  	public static void error(String message) {
99  		log(LogLevel.ERROR, message);
100 	}
101 	public static void error(String message, Throwable error) {
102 		log(LogLevel.ERROR, message, error);
103 	}
104 	
105 	public static void fatal(String message) {
106 		log(LogLevel.FATAL, message);
107 	}
108 	public static void fatal(String message, Throwable error) {
109 		log(LogLevel.FATAL, message, error);
110 	}
111 	
112 	public static void reset() {
113 		buffer = new LogBuffer(maxBufferSize);
114 	}
115 	
116 	
117 	
118 	public static Map<String, String> getClientContextInfo() {
119         return clientContextInfo;
120     }
121 
122     public static void sendLogs() {
123 		final List<LogMessage> messages = new ArrayList<LogMessage>(buffer.getLogMessages());
124 		final Map<String, String> context = new HashMap<String, String>(clientContextInfo);
125 		reset();
126 		DeferredCommand.addCommand(new Command() {
127 			public void execute() {
128 			    String log = formatLog(messages);
129 			    LogRpcServiceAsync logService = (LogRpcServiceAsync) GWT .create(LogRpcService.class); 
130 			    
131 				logService.sendLog(context, log, new KSAsyncCallback<Boolean>() {
132 					public void handleFailure(Throwable caught) {
133 					    GWT.log("sendLog Failed", caught);					    
134 					    //throw new LogFailedException(caught);
135 					}
136 
137 					public void onSuccess(Boolean result) {
138 						// do nothing
139 					    GWT.log("sendLog OK", null);
140 					}
141 				});
142 			}
143 		});
144 	}
145 	
146     private static String formatLog(List<LogMessage> messages) {
147         StringBuilder s = new StringBuilder();
148         for (LogMessage lm : messages) {
149             s.append(lm.getLogLevel().toString());
150             s.append(":\t");
151             s.append(lm.getMessage());
152             Throwable t = lm.getError();
153             if (t != null) {
154                 appendStackTrace(t, s);
155             }
156         }
157         return s.toString();
158     }
159     
160     private static void appendStackTrace(Throwable t, StringBuilder s) {
161         s.append(t.toString());
162         s.append(": at\n");
163         StackTraceElement[] stack = t.getStackTrace();
164         for (StackTraceElement frame : stack) {
165             s.append(frame.toString());
166             s.append("\n");
167         }
168     }
169 }