|
MDi JLib 1.7.4 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.microdeveloper.util.io.log.ErrorLog
public class ErrorLog
ErrorLog provides a consolidated class for error handling and output.
To use ErrorLog, create a log instance:
ErrorLog log = new ErrorLog();and then when the error occurs:
log.add( "Some error message" );The time and date will be added to your message and written to System.err.
Example:
ErrorLog log = new ErrorLog(); log.setLogFile( "errors.txt" ); // turn on text file logging ... log.add( "Some error message" ); // this error message is sent to the console and copied to the file "errors.txt" ... // end of the program log.setLogFileOff();Note that a "file flush" is performed after each error message is processed. This is in case your program is prematurely terminated: the file will still contain all of the messages you sent (instead of having them in a buffer in memory waiting for the file to be closed).
Note also that setLogFileOff() does not have to be called when your program finishes, although it is good programming practice. In other words, you will not lose any data from your file if you do not call setLogFileOff().
INTERNAL LOGGING
Example:
ErrorLog log = new ErrorLog(); log.setInternalLog( true ); // turn on internal logging ... log.add( "file " + fileName + " corrupted at byte " + pos ); // this error message is stored in Errors and copied to the console ... //When you are ready to extract the log: String[] errors = log.getList();If you have different levels of severity you wish to keep track of, you can create different ErrorLog objects:
ErrorLog errors = new ErrorLog(); ErrorLog warnings = new ErrorLog(); ErrorLog suggestions = new ErrorLog(); ... errors.add( "Some error message" ); ... warnings.add( "Some warning message" ); ... if ( foo.length() < 5 ) {
Constructor Summary | |
---|---|
ErrorLog()
Create an ErrorLog object where errors are sent to System.err |
|
ErrorLog(String fileName)
Create an ErrorLog object where errors are sent to a text file. |
Method Summary | |
---|---|
void |
add(String message)
Add an error message to the ErrorLog object. |
void |
appendLogFile(String fileName)
Tell the ErrorLog object that all future error messages are to be copied to a text file. |
String[] |
getList()
Get a list of all error messages that have been passed in to the ErrorLog object. |
boolean |
isConsoleLogOn()
Test to see if this ErrorLog object currently copies error messages to the console. |
boolean |
isFileLogOn()
Test to see if this ErrorLog object currently copies error messages to a text file. |
boolean |
isInternalLogOn()
Test to see if this ErrorLog object currently copies error messages to an internal buffer. |
int |
numErrors()
Get the number of errors that this object has encountered. |
void |
setConsole(boolean on)
Tell the ErrorLog object to start/stop copying error messages to the console. |
void |
setInternalLog(boolean on)
Tell the ErrorLog object to start/stop copying error messages to an internal buffer. |
void |
setLogFile(String fileName)
Tell the ErrorLog object that all future error messages are to be copied to a text file. |
void |
setLogFileOff()
Tell the ErrorLog object that no further errors are to be copied to a text file. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public ErrorLog()
public ErrorLog(String fileName) throws IOException
If the text file already exists, it will first be deleted. If the text file does not already exist, it will be created.
This constructor defaults to console logging off. If you want both console logging and file logging, call this constructor and then call setConsole( true );
fileName
- The name of the file that will be created and store future error messages.
IOException
- thrown for any IO errors encountered while creating the file. Method Detail |
---|
public void setLogFile(String fileName) throws IOException
If the text file already exists, it will first be deleted. If the text file does not already exist, it will be created.
fileName
- The name of the file that will be created and store future error messages.
IOException
- thrown for any IO errors encountered while creating the file. public void appendLogFile(String fileName) throws IOException
If the text file already exists, new messages will be appended to the end. If the text file does not already exist, it will be created.
fileName
- The name of the file that will store future error messages.
IOException
- thrown for any IO errors encountered while creating/opening the file. public void setLogFileOff()
This method also properly closes the log file.
public boolean isFileLogOn()
public void setConsole(boolean on)
on
- true: start sending messages to the console. false: stop sending messages to the console. public boolean isConsoleLogOn()
public void add(String message)
"message" has a time/date stamp added to it.
If internal logging is on, the message is appended to the internal collection of error messages.
If console logging is on, the message is copied to the console.
If file logging is on, the message is copied to the log file.
message
- this can be any text you want. public int numErrors()
public String[] getList()
public void setInternalLog(boolean on)
on
- true start copying messages to the internal buffer. false stop copying messages to the internal buffer.public boolean isInternalLogOn()
|
MDi JLib 1.7.4 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |