0

I have implemented logging mechanism for my AWT project (its a .jar application). so i made logging configuration via java class file as a utility class.

I have created utility class because am having more java files to have log messages.

Please find my LogUtility class.

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class LogUtility {
    
    private static LogUtility instance = new LogUtility();
    public static LogUtility getInstance(){
        return instance;
    }
    static Logger logger=Logger.getLogger("LogUtility");
    static {
        //String userHomePath=System.getProperty("user.home");
        String userHomePath = System.getenv("USERPROFILE").concat("\\AppData\\LocalLow\\Sun\\Java\\Deployment\\log");
        LogUtility.getInstance().info("Log Path -->"+userHomePath);
        try {
            String dateTimeStamp=new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
            FileHandler fh=new FileHandler(userHomePath.concat("\\app").concat("_"+dateTimeStamp+".log"));
            logger.addHandler(fh);
            SimpleFormatter formatter=new SimpleFormatter();
            fh.setFormatter(formatter);
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    
    public void info(String msg){
        logger.info(msg);
    }
    
}

Please find my implementation class

public class Login {
    
    private static LogUtility logUtility=LogUtility.getInstance();
    
    public static void copyFile(File afile, File bfile) throws IOException {
        logUtility.info("copyFile called");
        }
    
}

Logs are created and writting into the file but .lck file also its creating. Not sure why its creating. Any suggestion about this logging implementation.

Update 1 Above my implementation is working as expected and its writting into the .log file. But if there is any info messages, its not giving the exact java file name and line number from where that info is coming. Instead its giving the LogUtility class references.

INFO: null
Sep 22, 2021 7:56:54 PM com.loggers.LogUtility info
INFO: timeout:12
Sep 22, 2021 7:56:54 PM com.loggers.LogUtility info
INFO: null
Sep 22, 2021 7:56:54 PM com.loggers.LogUtility info
INFO: timeout:12
Sep 22, 2021 7:56:54 PM com.loggers.LogUtility info
INFO: null
Karthikeyan
  • 1,927
  • 6
  • 44
  • 109
  • Does this answer your question? [Java Custom Logger - Not logging proper classname](https://stackoverflow.com/questions/68978164/java-custom-logger-not-logging-proper-classname) – jmehrens Oct 01 '21 at 13:27

1 Answers1

1

The .lck file exists while the FileHandler is open. If you see the file after your program has terminated then that is a different topic. Your utility class looks like it is pinning the logger and only attaching the FileHandler once so all of that looks correct.

But if there is any info messages, its not giving the exact java file name and line number from where that info is coming. Instead its giving the LogUtility class references.

This is answered in Java Custom Logger - Not logging proper classname and that answer includes a working example.

jmehrens
  • 10,580
  • 1
  • 38
  • 47