Olá, estou precisando fazer loggin de uma aplicação que estou utilizando, mas gostaria de uma opinião de a classe que estou utilziando para fazer o loggin está implementada corretamente. Pois necessito criar arquivos de log para cada classe instanciada, por exemplo, eu tenho uma classe chamada Trabalhador, e para cada instancia dessa classe preciso cirar um novo arquivos de log, trabalhador1.log, trabalhador2.log, …
package util;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import core.Config;
/**
* Classe responsavel por criar arquivos de log.
* construtor passa como parametro o nome do arquivo que sera criado e o
* workerId que criou esse arquivo.
*
* @author robertow
*
*/
public class FileLog {
private String workerId;
private String nameFile;
private FileWriter fileWriter;
private PrintWriter filePrint;
public FileLog(String nameFile, String workerId) {
this.setWorkerId(workerId);
this.setNameFile(nameFile);
try {
this.fileWriter = new FileWriter(new File(Config.LOG_DIR + this.getNameFile() + "-" + this.getWorkerId() + ".txt"));
this.filePrint = new PrintWriter(fileWriter);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.print("ID \t\t TAREFA \t\t RESULTADO \t\t TEMPOEXECUCAO");
}
public void print(String str) {
try {
this.fileWriter = new FileWriter(new File(Config.LOG_DIR + this.getNameFile() + "-" + this.getWorkerId() + ".txt"),true);
this.filePrint = new PrintWriter(fileWriter);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
this.filePrint.println(str);
this.filePrint.close();
try {
this.fileWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void setWorkerId(String workerId) {
this.workerId = workerId;
}
public String getWorkerId() {
return this.workerId;
}
public void setNameFile(String nameFile) {
this.nameFile = nameFile;
}
public String getNameFile() {
return this.nameFile;
}
}
Tentei utilziar o a classe Loggin mas não estou conseguindo criar um arquivo de log para cada instancia da classe trabalhador.
Alguem poderia me auxiliar? Grato.