Gostaria de receber umas dicas para melhorar este codigo. A classe tem o objetivo de criar Logs para o meu programa.
public class Log {
private static Log log;
private final static String PATH = FileSystemView.getFileSystemView().getHomeDirectory().getPath() + "/M4Cloud/Logs/";
static {
log = new Log();
}
private Log() {
verificarExiste();
}
public void logar(String texto) {
System.out.println(texto);
escreverLinha(texto);
}
private void escreverLinha(String texto) throws RuntimeException {
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
Date hoje = new Date();
try (BufferedWriter bw = new BufferedWriter(new FileWriter(PATH + formatter.format(hoje) + ".txt"))) {
bw.write("[" + new SimpleDateFormat("HH:mm:ss").format(hoje) + "] " + texto);
bw.newLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void verificarExiste() {
criarDiretorioLog();
criarArquivoLog();
}
private void criarArquivoLog() {
Date hoje = new Date();
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
if (!new File(PATH + formatter.format(hoje) + ".txt").exists()) {
escreverLinha("##CRIADO EM " + formatter.format(hoje) + " POR M4CLOUD##");
}
}
private void criarDiretorioLog() {
if (!new File(PATH).isDirectory()) {
new File(PATH).mkdirs();
}
}
public static Log getInstance() {
return log;
}
}