0

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?

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
mammago
  • 247
  • 2
  • 13
  • What's your current log level? And which logging system are you using? – longhua Nov 20 '13 at 10:16
  • The log level is FINE like you can see in the code. I don't exactly know what you mean by logging system, im using the in-built Java Logging class – mammago Nov 20 '13 at 10:31
  • Could you please provide a simple complete example? Log system usually has a global configuration, log level. It defines if a log message should be printed or not. `FINE` in `LOG.log(Level.FINE, message)` only means this `message` should be print in log level FINE or higher. It is not the global configuration. – longhua Nov 20 '13 at 11:31
  • Where is the annotation `@LogDebug` defined? Could you please list the JAR's you are using? – longhua Nov 20 '13 at 11:32
  • added the related code parts – mammago Nov 20 '13 at 13:38
  • Are you sure `doLogInfo` has been called? Can you see the message printed on console? By the way, I think you should remove `logWriter` before close it. – longhua Nov 20 '13 at 14:20
  • The method gets called and the console output is correct. I just noticed logWarning and logError get handled correctly, only debug and Info don't write in the logfile. So it seems like the lower log levels don't execute. – mammago Nov 21 '13 at 08:41
  • The default log level is `Level.INFO`. Maybe you have specified the log level. Please check "logging.properties" in "jre/lib" or your logging config file specified via "-Djava.util.logging.config.file" property. – longhua Nov 21 '13 at 09:38

0 Answers0