我正在尝试为JavaEE WebApp编写一些Logger方法。 在与Annotations发生冲突后,我将其作为我的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();
}
}
这是我宣布限定符的地方:
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 {
}
}
这就是我发布事件的地方:
@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
}
现在的问题是FileHandler正确地创建了Log.txt但它没有在那里写任何消息。 我也尝试将其写入.xml,但它所做的只是创建一个带有xml正文和空白“log”的文件,但没有消息。 已经检查过String message参数是否正确传递了它。
解决这个问题的任何想法?