My favorites | Sign in
Project Logo
                
Changes to /trunk/api/src/org/jogre/common/util/JogreLogger.java
r2 vs. r10   Edit
  Compare: vs.   Format:
Revision r10
Go to: 
Project members, sign in to write a code review
/trunk/api/src/org/jogre/common/util/JogreLogger.java   r2 /trunk/api/src/org/jogre/common/util/JogreLogger.java   r10
1 /* 1 /*
2 * JOGRE (Java Online Gaming Real-time Engine) - API 2 * JOGRE (Java Online Gaming Real-time Engine) - API
3 * Copyright (C) 2004 Bob Marks (marksie531@yahoo.com) 3 * Copyright (C) 2004 Bob Marks (marksie531@yahoo.com)
4 * http://jogre.sourceforge.org 4 * http://jogre.sourceforge.org
5 * 5 *
6 * This program is free software; you can redistribute it and/or 6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License 7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2 8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version. 9 * of the License, or (at your option) any later version.
10 * 10 *
11 * This program is distributed in the hope that it will be useful, 11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details. 14 * GNU General Public License for more details.
15 * 15 *
16 * You should have received a copy of the GNU General Public License 16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software 17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */ 19 */
20 package org.jogre.common.util; 20 package org.jogre.common.util;
21 21
22 import java.io.*; 22 import java.io.*;
23 import java.text.SimpleDateFormat; 23 import java.text.SimpleDateFormat;
24 import java.util.Calendar; 24 import java.util.Calendar;
25 import java.util.Date; 25 import java.util.Date;
26 26
27 /** 27 /**
28 * <p>Custom logging was created for JOGRE so that is as flexible and as 28 * <p>Custom logging was created for JOGRE so that is as flexible and as
29 * lightweight as possible (e.g. log4j is too large at 345kb). This logging 29 * lightweight as possible (e.g. log4j is too large at 345kb). This logging
30 * stores a timestamp, followed by the class and then the message itself.</p> 30 * stores a timestamp, followed by the class and then the message itself.</p>
31 * 31 *
32 * <p>To use this class you simply create a new instance at the top of a class 32 * <p>To use this class you simply create a new instance at the top of a class
33 * such as:</p> 33 * such as:</p>
34 * 34 *
35 * <p><code>JogreLogger logger = new JogreLog (this.getClass());</code></p> 35 * <p><code>JogreLogger logger = new JogreLog (this.getClass());</code></p>
36 * 36 *
37 * <p>To use this class in source code use the log (String message), error 37 * <p>To use this class in source code use the log (String message), error
38 * (String method, String message), info (String method, String message) and 38 * (String method, String message), info (String method, String message) and
39 * debug (String method, String message) methods. The error method should be 39 * debug (String method, String message) methods. The error method should be
40 * used when there is simply an error (used for speed), logInfo for logging 40 * used when there is simply an error (used for speed), logInfo for logging
41 * information (on a stable system) and logDebug (slower) for debugging an 41 * information (on a stable system) and logDebug (slower) for debugging an
42 * application (logs everything).</p> 42 * application (logs everything).</p>
43 * 43 *
44 * <p>To see the priority of debug output at run time by setting the following 44 * <p>To see the priority of debug output at run time by setting the following
45 * key/value properties in a "game.properties" file. All of these properties 45 * key/value properties in a "game.properties" file. All of these properties
46 * are optional and if they don't exist then their default values are assumed 46 * are optional and if they don't exist then their default values are assumed
47 * (which are in bold).</p> 47 * (which are in bold).</p>
48 * <p> 48 * <p>
49 * <ul> 49 * <ul>
50 * <li><code>log.priority.console</code> - must be equal to <i>one</i> of the following:</li> 50 * <li><code>log.priority.console</code> - must be equal to <i>one</i> of the following:</li>
51 * <ul> 51 * <ul>
52 * <li><code>1</code> - error (fast, but only logs errors.</li> 52 * <li><code>1</code> - error (fast, but only logs errors.</li>
53 * <li><b><code>2</code> - info</b> - (medium speend, logs information strings).</li> 53 * <li><b><code>2</code> - info</b> - (medium speend, logs information strings).</li>
54 * <li><code>3</code> - debug - (slow, logs everything.</li> 54 * <li><code>3</code> - debug - (slow, logs everything.</li>
55 * </ul> 55 * </ul>
56 * <li><code>log.priority.file</code> - must be equal to <i>one</i> of the following:</li> 56 * <li><code>log.priority.file</code> - must be equal to <i>one</i> of the following:</li>
57 * <ul> 57 * <ul>
58 * <li><code>1</code> - error (fast, but only logs errors.</li> 58 * <li><code>1</code> - error (fast, but only logs errors.</li>
59 * <li><code>2</code> - info - (medium speend, logs information strings).</li> 59 * <li><code>2</code> - info - (medium speend, logs information strings).</li>
60 * <li><b><code>3</code> - debug</b> - (slow, logs everything.</li> 60 * <li><b><code>3</code> - debug</b> - (slow, logs everything.</li>
61 * </ul> 61 * </ul>
62 * <li><code>log.show.priority</code> - <code>true</code> or <b><code>false</code></b> - show priority in log outputs.</li> 62 * <li><code>log.show.priority</code> - <code>true</code> or <b><code>false</code></b> - show priority in log outputs.</li>
63 * </ul> 63 * </ul>
64 * </p> 64 * </p>
65 * 65 *
66 * <p>Logs which are saved by file are saved in the following location - 66 * <p>Logs which are saved by file are saved in the following location -
67 * "logs/yyyy_MM/log_dd_MMM_yyyy.txt" where y = year, d = day and M = month e.g. 67 * "logs/yyyy_MM/log_dd_MMM_yyyy.txt" where y = year, d = day and M = month e.g.
68 * "logs/2004_04/log_29_April_2004.txt". This logger is not anywhere near as 68 * "logs/2004_04/log_29_April_2004.txt". This logger is not anywhere near as
69 * efficient as others but this will be investigated soon.</p> 69 * efficient as others but this will be investigated soon.</p>
70 * 70 *
71 * @author Bob Marks 71 * @author Bob Marks
72 * @version Alpha 0.2.3 72 * @version Alpha 0.2.3
73 */ 73 */
74 public class JogreLogger implements IJogreLog { 74 public class JogreLogger implements IJogreLog {
75 75
76 // declare keys are which are stored in the resources strings 76 // declare keys are which are stored in the resources strings
77 private static final String KEY_LOG_CONSOLE_PRIORITY = "log.priority.console"; 77 private static final String KEY_LOG_CONSOLE_PRIORITY = "log.priority.console";
78 private static final String KEY_LOG_FILE_PRIORITY = "log.priority.file"; 78 private static final String KEY_LOG_FILE_PRIORITY = "log.priority.file";
79 private static final String KEY_LOG_SHOW_PRIORITY = "log.show.priority"; 79 private static final String KEY_LOG_SHOW_PRIORITY = "log.show.priority";
80 80
81 // name of the log directory 81 // name of the log directory
82 private static final String LOG_DIRECTORY = "log"; 82 private static final String LOG_DIRECTORY = "log";
83 83
84 // dont log constant. 84 // dont log constant.
85 private static final String DONT_LOG = "-1"; 85 private static final String DONT_LOG = "-1";
86 86
87 /** Name of this class. */ 87 /** Name of this class. */
88 protected String className; 88 protected String className;
89 89
90 /** Priority of console logging. */ 90 /** Priority of console logging. */
91 protected int consolePriority; 91 protected int consolePriority;
92 92
93 /** Priority of file logging. */ 93 /** Priority of file logging. */
94 protected int filePriority; 94 protected int filePriority;
95 95
96 /** If this is true then show the priority of the log as a String. */ 96 /** If this is true then show the priority of the log as a String. */
97 protected boolean showPriority; 97 protected boolean showPriority;
98 98
99 // Date formatters (declare once for performance) 99 // Date formatters (declare once for performance)
100 private static final String DATE_FORMAT_DIR = "yyyy_MM"; 100 private static final String DATE_FORMAT_DIR = "yyyy_MM";
101 private static final String DATE_FORMAT_FILE = "dd_MMM_yyyy"; 101 private static final String DATE_FORMAT_FILE = "dd_MMM_yyyy";
102 private static final String DATE_FORMAT_LOG = "yyyy/MM/dd hh:mm:ss:SSS"; 102 private static final String DATE_FORMAT_LOG = "yyyy/MM/dd hh:mm:ss:SSS";
103 private SimpleDateFormat dateFormatDir = null; 103 private SimpleDateFormat dateFormatDir = null;
104 private SimpleDateFormat dateFormatFile = null; 104 private SimpleDateFormat dateFormatFile = null;
105 private SimpleDateFormat dateFormatLog = null; 105 private SimpleDateFormat dateFormatLog = null;
106 106
107 /** 107 /**
108 * Constructor which takes the class of the logged Class. 108 * Constructor which takes the class of the logged Class.
109 * @param loggedClass 109 * @param loggedClass
110 */ 110 */
111 public JogreLogger (Class loggedClass) { 111 public JogreLogger (Class loggedClass) {
112 // Populate the class Name 112 // Populate the class Name
113 String fullClassName = loggedClass.getName(); 113 String fullClassName = loggedClass.getName();
114 this.className = fullClassName.substring(fullClassName.lastIndexOf('.') + 1); 114 this.className = fullClassName.substring(fullClassName.lastIndexOf('.') + 1);
115 115
116 // Initilise simple date formatters 116 // Initilise simple date formatters
117 this.dateFormatDir = new SimpleDateFormat (DATE_FORMAT_DIR); 117 this.dateFormatDir = new SimpleDateFormat (DATE_FORMAT_DIR);
118 this.dateFormatFile = new SimpleDateFormat (DATE_FORMAT_FILE); 118 this.dateFormatFile = new SimpleDateFormat (DATE_FORMAT_FILE);
119 this.dateFormatLog = new SimpleDateFormat (DATE_FORMAT_LOG); 119 this.dateFormatLog = new SimpleDateFormat (DATE_FORMAT_LOG);
120 120
121 // Set up booleans 121 // Set up booleans
122 initilise (); 122 initilise ();
123 } 123 }
124 124
125 /** 125 /**
126 * Log an error message. 126 * Log an error message.
127 * 127 *
128 * @param method Method in a class. 128 * @param method Method in a class.
129 * @param message Message to log. 129 * @param message Message to log.
130 */ 130 */
131 public void error (String method, String message) { 131 public void error (String method, String message) {
132 log (ERROR, method, message); 132 log (ERROR, method, message);
133 } 133 }
134 134
135 /** 135 /**
136 * Log an information message. 136 * Log an information message.
137 * 137 *
138 * @param method Method in a class. 138 * @param method Method in a class.
139 * @param message Message to log. 139 * @param message Message to log.
140 * 140 *
141 */ 141 */
142 public void info (String method, String message) { 142 public void info (String method, String message) {
143 log (INFO, method, message); 143 log (INFO, method, message);
144 } 144 }
145 145
146 /** 146 /**
147 * Log a debug message. 147 * Log a debug message.
148 * 148 *
149 * @param method Method in a class. 149 * @param method Method in a class.
150 * @param message Message to log. 150 * @param message Message to log.
151 */ 151 */
152 public void debug (String method, String message) { 152 public void debug (String method, String message) {
153 log (DEBUG, method, message); 153 log (DEBUG, method, message);
154 } 154 }
155 155
156 /** 156 /**
157 * Simple log which will go in at INFO level. 157 * Simple log which will go in at INFO level.
158 * 158 *
159 * @param message Message to log 159 * @param message Message to log
160 */ 160 */
161 public void log (String message) { 161 public void log (String message) {
162 log (INFO, DONT_LOG, message); 162 log (INFO, DONT_LOG, message);
163 } 163 }
164 164
165 /** 165 /**
166 * Logs a stacktrace. 166 * Logs a stacktrace.
167 * 167 *
168 * @param e Exception object. 168 * @param e Exception object.
169 */ 169 */
170 public void stacktrace (Exception e) { 170 public void stacktrace (Throwable e) {
171 error ("Exception", e.getMessage()); 171 error ("Exception", e.getMessage());
172 } 172 }
173 173
174 /** 174 /**
175 * Intilise the priority level and booleans of where to output the logs. 175 * Intilise the priority level and booleans of where to output the logs.
176 */ 176 */
177 private void initilise () { 177 private void initilise () {
178 178
179 // Set defaults 179 // Set defaults
180 consolePriority = DEFAULT_CONSOLE_PRIORITY; 180 consolePriority = DEFAULT_CONSOLE_PRIORITY;
181 filePriority = NONE; // DEFAULT_FILE_PRIORITY; shutting this off for applets until better solution 181 filePriority = NONE; // DEFAULT_FILE_PRIORITY; shutting this off for applets until better solution
182 showPriority = DEFAULT_SHOW_PRIORITY; 182 showPriority = DEFAULT_SHOW_PRIORITY;
183 183
184 // read in priorities for console and file logging 184 // read in priorities for console and file logging
185 /* TODO - Change this as there is a seperate server and API now. 185 /* TODO - Change this as there is a seperate server and API now.
186 GameProperties properties = GameProperties.getInstance(); 186 GameProperties properties = GameProperties.getInstance();
187 187
188 if (properties != null) { 188 if (properties != null) {
189 consolePriority = properties.getInt (KEY_LOG_CONSOLE_PRIORITY, DEFAULT_CONSOLE_PRIORITY); 189 consolePriority = properties.getInt (KEY_LOG_CONSOLE_PRIORITY, DEFAULT_CONSOLE_PRIORITY);
190 filePriority = properties.getInt (KEY_LOG_FILE_PRIORITY, DEFAULT_FILE_PRIORITY); 190 filePriority = properties.getInt (KEY_LOG_FILE_PRIORITY, DEFAULT_FILE_PRIORITY);
191 showPriority = properties.getBoolean (KEY_LOG_SHOW_PRIORITY, DEFAULT_SHOW_PRIORITY); 191 showPriority = properties.getBoolean (KEY_LOG_SHOW_PRIORITY, DEFAULT_SHOW_PRIORITY);
192 }*/ 192 }*/
193 193
194 // Create to see if top level "log" directory is created. 194 // Create to see if top level "log" directory is created.
195 // FileUtils.createDirectory (LOG_DIRECTORY); 195 // FileUtils.createDirectory (LOG_DIRECTORY);
196 196
197 } 197 }
198 198
199 /** 199 /**
200 * Method which performs the debug. 200 * Method which performs the debug.
201 * 201 *
202 * @param logPriority Priority of the log (should be between 1 and 3) 202 * @param logPriority Priority of the log (should be between 1 and 3)
203 * @param method Used to show class.method () in log. If this is equal 203 * @param method Used to show class.method () in log. If this is equal
204 * to DONT_LOG then this isnt displayed. 204 * to DONT_LOG then this isnt displayed.
205 * @param message Message to log. 205 * @param message Message to log.
206 */ 206 */
207 private void log (int logPriority, String method, String message) { 207 private void log (int logPriority, String method, String message) {
208 208
209 // Check priority 209 // Check priority
210 // Create log message from priority, time stamp, classs & message 210 // Create log message from priority, time stamp, classs & message
211 Calendar calender = Calendar.getInstance(); 211 Calendar calender = Calendar.getInstance();
212 Date date = calender.getTime(); 212 Date date = calender.getTime();
213 213
214 // Create log Strings 214 // Create log Strings
215 String timeStamp = dateFormatLog.format (date); 215 String timeStamp = dateFormatLog.format (date);
216 String classMethodStr = ""; 216 String classMethodStr = "";
217 String priorityStr = ""; 217 String priorityStr = "";
218 218
219 if (!method.equals (DONT_LOG)) 219 if (!method.equals (DONT_LOG))
220 classMethodStr = className + "." + method + "(): "; 220 classMethodStr = className + "." + method + "(): ";
221 221
222 // Create the priority string if required 222 // Create the priority string if required
223 if (showPriority && logPriority > 0 && logPriority < 4 && !method.equals (DONT_LOG)) 223 if (showPriority && logPriority > 0 && logPriority < 4 && !method.equals (DONT_LOG))
224 priorityStr = PRIORITY_STRS [logPriority] + " "; 224 priorityStr = PRIORITY_STRS [logPriority] + " ";
225 225
226 String logMessage = 226 String logMessage =
227 "[" + timeStamp + "] " + // timestamp 227 "[" + timeStamp + "] " + // timestamp
228 priorityStr + // error e.g. ERROR INFO DEBUG 228 priorityStr + // error e.g. ERROR INFO DEBUG
229 classMethodStr + // class and method 229 classMethodStr + // class and method
230 message; // actual message 230 message; // actual message
231 231
232 if (logPriority <= consolePriority) 232 if (logPriority <= consolePriority)
233 writeToConsole (logMessage); 233 writeToConsole (logMessage);
234 if (logPriority <= filePriority) 234 if (logPriority <= filePriority)
235 writeToFile (logMessage, date); 235 writeToFile (logMessage, date);
236 } 236 }
237 237
238 /** 238 /**
239 * Log to the console. 239 * Log to the console.
240 * 240 *
241 * @param logMessage Log message which is going to the console 241 * @param logMessage Log message which is going to the console
242 */ 242 */
243 private void writeToConsole (String logMessage) { 243 private void writeToConsole (String logMessage) {
244 System.out.println (logMessage); 244 System.out.println (logMessage);
245 } 245 }
246 246
247 /** 247 /**
248 * Log to the console. 248 * Log to the console.
249 * 249 *
250 * @param logMessage Message to log. 250 * @param logMessage Message to log.
251 * @param date Date reference for working out directory names etc. 251 * @param date Date reference for working out directory names etc.
252 */ 252 */
253 private void writeToFile (String logMessage, Date date) { 253 private void writeToFile (String logMessage, Date date) {
254 // Create to see if top level "log" directory is created. 254 // Create to see if top level "log" directory is created.
255 FileUtils.createDirectory (LOG_DIRECTORY); 255 FileUtils.createDirectory (LOG_DIRECTORY);
256 256
257 // Create the name of the directory and file 257 // Create the name of the directory and file
258 char PS = File.separatorChar; // path seperater 258 char PS = File.separatorChar; // path seperater
259 String logDirectory = dateFormatDir.format (date); 259 String logDirectory = dateFormatDir.format (date);
260 String logFilename = "log_" + dateFormatFile.format (date) + ".txt"; 260 String logFilename = "log_" + dateFormatFile.format (date) + ".txt";
261 261
262 // Create actual log file 262 // Create actual log file
263 String fullFileName = LOG_DIRECTORY + PS + logDirectory + PS + logFilename; 263 String fullFileName = LOG_DIRECTORY + PS + logDirectory + PS + logFilename;
264 File logFile = new File (fullFileName); 264 File logFile = new File (fullFileName);
265 265
266 // Create the log directory if it doesn't exist 266 // Create the log directory if it doesn't exist
267 FileUtils.createDirectory (LOG_DIRECTORY + PS + logDirectory); 267 FileUtils.createDirectory (LOG_DIRECTORY + PS + logDirectory);
268 268
269 try { 269 try {
270 BufferedWriter fileout = null; // declare 270 BufferedWriter fileout = null; // declare
271 271
272 if (logFile.exists()) { 272 if (logFile.exists()) {
273 fileout = new BufferedWriter(new FileWriter(fullFileName, true)); // true = append 273 fileout = new BufferedWriter(new FileWriter(fullFileName, true)); // true = append
274 fileout.newLine(); 274 fileout.newLine();
275 } 275 }
276 else // else create the file 276 else // else create the file
277 fileout = new BufferedWriter(new FileWriter(fullFileName)); 277 fileout = new BufferedWriter(new FileWriter(fullFileName));
278 278
279 // write text to file. 279 // write text to file.
280 fileout.write (logMessage); 280 fileout.write (logMessage);
281 fileout.close (); 281 fileout.close ();
282 } 282 }
283 catch (IOException ioe) { 283 catch (IOException ioe) {
284 // if error don't worry (do nothing) 284 // if error don't worry (do nothing)
285 } 285 }
286 } 286 }
287 } 287 }
Hosted by Google Code