UI Logger 를 제작하였습니다.
ㅁ 사용법
- 사용법은 기존의 자바 로깅과 유사하게 구현하였습니다.
(내부 구현은 GWT-Ext 의 ShowcasePanel 참조...)
01 | protected static LoggerWindow logger = LoggerWindow.get(); |
05 | logger.info( "start initPanel" ); |
09 | if (logger.isDebugEnabled()) { |
10 | logger.debug( "before createFormRenderer" ); |
위의 코드같이 로그를 작성합니다.
ㅁ 페이지 실행
- 실행시 주소 뒤에 파라미터로 로그레벨을 정의합니다.
(ex) log_level=debug
[로그 레벨]
- debug
- info
- warn
- error (default)
ㅁ LoggerWindow
- 로그 레벨은 색으로 구분하였으며 이전 로그와의 시간차인
Interval 과 해당 로그 발생시간인
Time 필드가 있습니다.
메시지는 아래쪽에 표시됩니다.
[#M_LoggerWindow 소스코드 보기|접기|
001 | import java.util.Date; |
003 | import com.gwtext.client.core.Function; |
004 | import com.gwtext.client.data.DateFieldDef; |
005 | import com.gwtext.client.data.FieldDef; |
006 | import com.gwtext.client.data.IntegerFieldDef; |
007 | import com.gwtext.client.data.Record; |
008 | import com.gwtext.client.data.RecordDef; |
009 | import com.gwtext.client.data.Store; |
010 | import com.gwtext.client.data.StringFieldDef; |
011 | import com.gwtext.client.util.DateUtil; |
012 | import com.gwtext.client.util.Format; |
013 | import com.gwtext.client.widgets.Tool; |
014 | import com.gwtext.client.widgets.Window; |
015 | import com.gwtext.client.widgets.grid.CellMetadata; |
016 | import com.gwtext.client.widgets.grid.ColumnConfig; |
017 | import com.gwtext.client.widgets.grid.ColumnModel; |
018 | import com.gwtext.client.widgets.grid.GridPanel; |
019 | import com.gwtext.client.widgets.grid.GridView; |
020 | import com.gwtext.client.widgets.grid.Renderer; |
021 | import com.gwtext.client.widgets.grid.RowParams; |
022 | import com.gwtext.client.widgets.layout.FitLayout; |
024 | public class LoggerWindow extends Window { |
026 | private static LoggerWindow instance; |
028 | public static LoggerWindow get() { |
029 | if (instance == null ) { |
030 | instance = new LoggerWindow(); |
031 | instance.initLogger(); |
039 | protected static final String DEBUG = "debug" ; |
041 | protected static final int LEVEL_DEBUG = 0 ; |
046 | protected static final String INFO = "info" ; |
048 | protected static final int LEVEL_INFO = 1 ; |
053 | protected static final String WARN = "warning" ; |
055 | protected static final int LEVEL_WARN = 2 ; |
060 | protected static final String ERROR = "error" ; |
062 | protected static final int LEVEL_ERROR = 3 ; |
067 | protected static final String[] LOG_LEVELS = { |
068 | DEBUG, INFO, WARN, ERROR |
074 | protected static final String[] LOG_COLORS = { |
075 | "4096EE" , "40EE54" , "EEB340" , "EE5040" |
078 | private static final String LOG_COLOR_FORMAT = "style=\"background:#{0};padding:5px\"" ; |
080 | private RecordDef recordDef; |
084 | private boolean showState; |
086 | private int logLevel; |
088 | private Date prevDate; |
090 | private LoggerWindow() { |
096 | setLayout( new FitLayout()); |
104 | private void initGridPanel() { |
105 | recordDef = new RecordDef( new FieldDef[]{ |
106 | new DateFieldDef( "date" ), |
107 | new StringFieldDef( "type" ), |
108 | new IntegerFieldDef( "level" ), |
109 | new StringFieldDef( "message" ) |
112 | store = new Store(recordDef); |
114 | ColumnModel columnModel = new ColumnModel( new ColumnConfig[]{ |
115 | new ColumnConfig( " " , "type" , 3 , true , new Renderer() { |
116 | public String render(Object value, CellMetadata cellMetadata, Record record, int rowIndex, int colNum, Store store) { |
117 | int level = record.getAsInteger( "level" ); |
118 | cellMetadata.setHtmlAttribute(Format.format(LOG_COLOR_FORMAT, LOG_COLORS[level])); |
122 | new ColumnConfig( "Interval" , "date" , 50 , true , new Renderer() { |
123 | public String render(Object value, CellMetadata cellMetadata, Record record, int rowIndex, int colNum, Store store) { |
124 | if (prevDate == null ) { |
125 | prevDate = (Date) value; |
128 | Date currDate = (Date) value; |
130 | Date date = new Date(currDate.getTime() - prevDate.getTime()); |
131 | prevDate = (Date) value; |
132 | return DateUtil.format(date, "i:s.u" ); |
135 | new ColumnConfig( "Time" , "date" , 60 , true , new Renderer() { |
136 | public String render(Object value, CellMetadata cellMetadata, Record record, int rowIndex, int colNum, Store store) { |
137 | return DateUtil.format((Date) value, "H:i:s.u" ); |
142 | GridPanel gridPanel = new GridPanel(store, columnModel); |
143 | gridPanel.setAutoExpandColumn( "date" ); |
144 | addTool( new Tool(Tool.MINUS, new Function() { |
145 | public void execute() { |
147 | store.commitChanges(); |
152 | GridView view = new GridView() { |
153 | public String getRowClass(Record record, int index, RowParams rowParams, Store store) { |
154 | rowParams.setBody(Format.format( "<p>{0}</p>" , record.getAsString( "message" ))); |
159 | view.setEnableRowBody( true ); |
160 | view.setForceFit( true ); |
161 | gridPanel.setView(view); |
169 | protected void initLogger() { |
170 | String paramLevel = com.google.gwt.user.client.Window.Location.getParameter( "log_level" ); |
171 | logLevel = LEVEL_ERROR; |
172 | for ( int i= 0 ; i<LOG_LEVELS.length; i++) { |
173 | String level = LOG_LEVELS[i]; |
174 | if (level.equals(paramLevel)) { |
179 | setPosition( 700 , 100 ); |
186 | public void debug(String message) { |
187 | log(DEBUG, LEVEL_DEBUG, message); |
194 | public void info(String message) { |
195 | log(INFO, LEVEL_INFO, message); |
202 | public void warn(String message) { |
203 | log(WARN, LEVEL_WARN, message); |
209 | public void error(String message) { |
210 | log(ERROR, LEVEL_ERROR, message); |
214 | * 현재 로깅이 가능한 상태를 확인합니다. |
218 | protected boolean isLoggerEnabled( int nLevel) { |
219 | return logLevel <= nLevel; |
223 | * 디버그 로그가 가능한 상태인지를 확인합니다. |
226 | public boolean isDebugEnabled() { |
227 | return isLoggerEnabled(LEVEL_DEBUG); |
231 | * 정보 로그가 가능한 상태인지를 확인합니다. |
234 | public boolean isInfoEnabled() { |
235 | return isLoggerEnabled(LEVEL_INFO); |
239 | * 경고 로그가 가능한 상태인지를 확인합니다. |
242 | public boolean isWarnEnabled() { |
243 | return isLoggerEnabled(LEVEL_WARN); |
249 | * @param type DEBUG, INFO, ... 와 같은 로그 타입 |
250 | * @param level LEVEL_DEBUG, LEVEL_INFO, ... 와 같은 로그 레벨 |
251 | * @param message 로그 메세지 |
253 | private void log(String type, int level, String message) { |
254 | if (isLoggerEnabled(level)) { |
258 | Record record = recordDef.createRecord( new Object[]{ |
264 | store.insert( 0 , record); |