0

我正在尝试为 JavaEE WebApp 编写一些记录器方法。在与注释发生一些冲突后,我将其作为我的日志记录类:

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 正文和空白空间“日志”的文件,但没有消息。已经检查了字符串消息参数是否正确传递并且确实如此。

关于解决这个问题的任何想法?

4

0 回答 0