-1

I made a logging system, but I need to write the log messages in a seperate log/txt file.

I came up with this:

public class UIRestService {

    private static final Logger LOGGER = Logger.getLogger(UIRestService.class.toString());

    FileHandler fh;

    public UIRestService() {
        try {
            fh = new FileHandler("E:/MyLog.log");
            LOGGER.addHandler(fh);
            SimpleFormatter formatter = new SimpleFormatter();
            fh.setFormatter(formatter);
        } catch (IOException e){
            e.printStackTrace();
        }
    }
    public MainDTO registerEORI(@RequestBody EoriRegistration json){
        long startTime = System.currentTimeMillis();
        try {
            LOGGER.info("--- Start EORI registration ---");
            EoriData entity = Converter.convertDTO2Entity(json);
            LOGGER.info("Converted DTO to Entity");
            MainDTO dto = dataManager.registration(entity);
            if (dto instanceof EoriRegistration) {
                msgManager.writeMsg(json.getState(), json.getC8_number() == null ? "test" : json.getC8_number());//TODO remove after IAM json.getC8_number()
            }
            return dto;
        } catch (Exception e){
            LOGGER.info("Exception in registerEORI");
            e.printStackTrace();
            Error error = new Error(Constants.TYPE_DTO2);
            error.setErrorDescription(e.getMessage());
            LOGGER.info("Exception in registerEORI" + error);
            return error;
        } finally {
            long endTime = System.currentTimeMillis();
            LOGGER.info("Registering EORI id: " + json.getId() + " , eori number:" + json.getEoriNumber() + " , elapsed:" + (endTime - startTime));
        }
    }

}

I manually created MyLog.log file in E:/ , but there is nothing. I tried with no manual created file - again nothing. It says that constructor is never used. Can you tell me what I am doing wrong? I used this example - Example

Ashot Karakhanyan
  • 2,804
  • 3
  • 23
  • 28
Decrux
  • 177
  • 1
  • 6
  • 17
  • What if you manually replaced `public UIRestService()` with `static`. – Joop Eggen Aug 01 '18 at 09:04
  • Wait, what do you mean? – Decrux Aug 01 '18 at 09:07
  • Sorry I just saw a static LOGGER and an iniitialisation in a service constructor. Without much thinking I proposed a static iniitalizer `static { ... }`. I would also first call setFomatter and then addHandler. But that all seems unlikely to cause problems. – Joop Eggen Aug 01 '18 at 09:32
  • BTW The most often error when not seeing log messages is a wrong log level. Above it should be INFO or less. – Joop Eggen Aug 01 '18 at 09:34
  • Maybe I didn't explain good enough. I am seeing the log messages in console, but I need to write them in a log/txt file in the same time - so the writing itself in the file doesn't happen. The file is empty. – Decrux Aug 01 '18 at 10:00

1 Answers1

0

i think you have to set the loglevel.

LOGGER.setLevel(Level.INFO)

I always initialize my Loggers following a Tutorial by vogella.com (http://www.vogella.com/tutorials/Logging/article.html#logexample_createLogger) and they work fine:

    Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);

    // suppress the logging output to the console
    Logger rootLogger = Logger.*getLogger*("");
    Handler[] handlers = rootLogger.getHandlers();
    if (handlers[0] instanceof ConsoleHandler) {
        rootLogger.removeHandler(handlers[0]);
    }

    logger.setLevel(Level.INFO);
    fileTxt = new FileHandler("Logging.txt");
    fileHTML = new FileHandler("Logging.html");

    // create a TXT formatter
    formatterTxt = new SimpleFormatter();
    fileTxt.setFormatter(formatterTxt);
    logger.addHandler(fileTxt);

(Source: http://www.vogella.com/tutorials/Logging/article.html#logexample_createLogger)

Leni
  • 81
  • 6
  • I'm not sure what you mean. I did not suggest using a constructor. did you try to modify your code according to the link above? – Leni Aug 01 '18 at 13:19
  • I changed from default logging in java to log4j and everything is fine now. – Decrux Aug 02 '18 at 08:18