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