Last active
August 29, 2015 14:04
-
-
Save kleberandrade/9844b2eb0a919a86e67c to your computer and use it in GitHub Desktop.
Sistema de log criado em Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package logproject; | |
import java.io.BufferedWriter; | |
import java.io.File; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
public final class LogSystem implements AutoCloseable { | |
private static LogSystem instance; | |
private final StringBuilder data; | |
private File file; | |
private FileWriter out; | |
private BufferedWriter buffer; | |
private static final String SEPARATOR = "\t"; | |
private static final String END_OF_LINE = "\r\n"; | |
private static final String DEFAULT_FILE_NAME = "log.txt"; | |
private static final int EMPTY = 0; | |
private LogSystem() { | |
setFileName(DEFAULT_FILE_NAME); | |
data = new StringBuilder(); | |
} | |
public static synchronized LogSystem getInstance() { | |
if (instance == null) { | |
instance = new LogSystem(); | |
} | |
return instance; | |
} | |
public LogSystem setFileName(String pathName) { | |
file = new File(pathName); | |
return this; | |
} | |
public LogSystem open() { | |
return open(false); | |
} | |
public LogSystem open(boolean appendMode) { | |
try { | |
out = new FileWriter(file, appendMode); | |
if (buffer != null) { | |
buffer.close(); | |
} | |
buffer = new BufferedWriter(out); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
return this; | |
} | |
public LogSystem clear() { | |
data.delete(0, data.length()); | |
return this; | |
} | |
public LogSystem saveChange() { | |
if (data.length() != 0) { | |
nextLine(); | |
} | |
try { | |
buffer.write(data.toString()); | |
buffer.flush(); | |
clear(); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
return this; | |
} | |
public LogSystem nextLine() { | |
data.append(END_OF_LINE); | |
return this; | |
} | |
public LogSystem append(String text) { | |
data.append(text).append(SEPARATOR); | |
return this; | |
} | |
public LogSystem append(int text) { | |
data.append(text).append(SEPARATOR); | |
return this; | |
} | |
public LogSystem append(float text) { | |
data.append(text).append(SEPARATOR); | |
return this; | |
} | |
public LogSystem append(double text) { | |
data.append(text).append(SEPARATOR); | |
return this; | |
} | |
public LogSystem append(char text) { | |
data.append(text).append(SEPARATOR); | |
return this; | |
} | |
public LogSystem append(boolean text) { | |
data.append(text).append(SEPARATOR); | |
return this; | |
} | |
public LogSystem append(long text) { | |
data.append(text).append(SEPARATOR); | |
return this; | |
} | |
public LogSystem append(Object text) { | |
data.append(text).append(SEPARATOR); | |
return this; | |
} | |
public LogSystem appendLine(String text) { | |
data.append(text).append(END_OF_LINE); | |
return this; | |
} | |
public LogSystem appendLine(int text) { | |
data.append(text).append(END_OF_LINE); | |
return this; | |
} | |
public LogSystem appendLine(float text) { | |
data.append(text).append(END_OF_LINE); | |
return this; | |
} | |
public LogSystem appendLine(double text) { | |
data.append(text).append(END_OF_LINE); | |
return this; | |
} | |
public LogSystem appendLine(char text) { | |
data.append(text).append(END_OF_LINE); | |
return this; | |
} | |
public LogSystem appendLine(boolean text) { | |
data.append(text).append(END_OF_LINE); | |
return this; | |
} | |
public LogSystem appendLine(long text) { | |
data.append(text).append(END_OF_LINE); | |
return this; | |
} | |
public LogSystem appendLine(Object text) { | |
data.append(text).append(END_OF_LINE); | |
return this; | |
} | |
@Override | |
public void close() throws Exception { | |
if (buffer != null) { | |
buffer.close(); | |
} | |
} | |
} |
Muito obrigado Kleber, é um ótimo script e muito útil
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Esse esquema de log foi criado para um amigo que precisava tabelar seus dados em um arquivo. Contudo, ainda irei colocar formas de gravar em 3 formatos (TXT, XML, JSON) e também uma forma de log pura com o dia e horário da informação.