Wed Apr 11, 2012 4:40 pm
Fri May 11, 2012 3:38 am
Sat May 12, 2012 9:57 pm
import java.awt.Color;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
/**
*
* @author Mohamed.Sami
*/
public class GUILogger {
private static GUILogger logger = null;
private static StyleContext context = new StyleContext();
private static StyledDocument document = new DefaultStyledDocument(context);
private static javax.swing.text.Style style = context.getStyle(StyleContext.DEFAULT_STYLE);
private static final JTextPane textArea = new JTextPane(document);
private static final JScrollPane TextAreaScroll = new JScrollPane(textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
private static DataOutputStream dataOutputStream;
private static final String filePath="./errorLog.txt";
private static final File file=new File(filePath);
private GUILogger() {
textArea.setSize(400, 400);
TextAreaScroll.setSize(400, 400);
StyleConstants.setAlignment(style, StyleConstants.ALIGN_LEFT);
StyleConstants.setFontSize(style, 10);
StyleConstants.setSpaceAbove(style, 1);
StyleConstants.setSpaceBelow(style, 1);
if(dataOutputStream==null){
FileOutputStream fileOutputStream=null;
try {
fileOutputStream = new FileOutputStream(file, true);
dataOutputStream = new DataOutputStream(fileOutputStream);
} catch (FileNotFoundException ex) {
Logger.getLogger(GUILogger.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public static GUILogger getInstance() {
if (logger == null) {
logger = new GUILogger();
}
return logger;
}
public void clearArea() {
try {
document.remove(0, document.getLength());
} catch (BadLocationException ex) {
Logger.getLogger(GUILogger.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void logInfo(String info) {
try {
document.insertString(document.getLength(), "\n" + new Date() + ": " + info, style);
} catch (BadLocationException ex) {
Logger.getLogger(GUILogger.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void logError(String error) {
try {
StyleConstants.setForeground(style, Color.red);
document.insertString(document.getLength(), "\n" + new Date() + ": " + error, style);
StyleConstants.setForeground(style, Color.black);
} catch (BadLocationException ex) {
Logger.getLogger(GUILogger.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void logGood(String good) {
try {
StyleConstants.setForeground(style, Color.GREEN.darker());
document.insertString(document.getLength(), "\n" + new Date() + ": " + good, style);
StyleConstants.setForeground(style, Color.black);
} catch (BadLocationException ex) {
Logger.getLogger(GUILogger.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void logToFile(String information){
try {
dataOutputStream.writeUTF("\n Log Date:" + new Date() + ": " + information);
dataOutputStream.flush();
} catch (IOException ex) {
ex.printStackTrace();
logError("Failed To Log To System File"+filePath+" :Error"+ex);
Logger.getLogger(GUILogger.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static GUILogger getLogger() {
return logger;
}
public static void setLogger(GUILogger logger) {
GUILogger.logger = logger;
}
public static JScrollPane getTextAreaScroll() {
return TextAreaScroll;
}
public static JTextPane getTextArea() {
return textArea;
}
}
Mon Aug 13, 2012 12:19 pm
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.