I'm trying to write some Logger Methods for a JavaEE WebApp. After having some conflicts with Annotations i got this as my Logging Class:
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.Produces;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import abc.crm.util.LogEvents.*;
@Named
@RequestScoped
public class Resources {
@Produces @PersistenceContext
private EntityManager entityManager;
@Produces
public FacesContext produceFacesContext() {
return FacesContext.getCurrentInstance();
}
private final static Logger LOG = Logger.getLogger(abc.crm.util.Resources.class.getName());
//This is another method doing the same thing.
public void doLogInfo(@Observes @LogInfo String message) {
FileHandler logWriter;
try {
logWriter = new FileHandler("CRMLog.txt", true);
logWriter.setFormatter(new SimpleFormatter());
LOG.addHandler(logWriter);
LOG.log(Level.INFO, message);
logWriter.close();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
This is where I declared the Qualifiers:
public class LogEvents {
@Qualifier
@Target({ FIELD, PARAMETER })
@Retention(RUNTIME)
public @interface LogInfo {
}
@Qualifier
@Target({ FIELD, PARAMETER })
@Retention(RUNTIME)
public @interface LogWarning {
}
@Qualifier
@Target({ FIELD, PARAMETER })
@Retention(RUNTIME)
public @interface LogError {
}
@Qualifier
@Target({ FIELD, PARAMETER })
@Retention(RUNTIME)
public @interface LogDebug {
}
}
And this is where I fire the Events:
@Inject
@LogDebug Event<String> doLogDebug;
@Inject
@Deleted
private Event<Customer> customerDeleteEventSrc;
public String doAddCustomer() {
doLogDebug.fire("Kunde kann jetzt angelegt werden");
return customerEditController.setCustomerToEdit(Mode.ADD);//übergibt dem Controller den Kunden
}
The problem now is that the FileHandler correctly creates the Log.txt but it doesn't write any messages in there. I also tried writing it to .xml but all it does is creating the file with an xml body and an empty space "log", but no message. Already checked if the String message parameter delivers correctly and it does.
Any Ideas on solving this?