Showing posts with label fileLog. Show all posts
Showing posts with label fileLog. Show all posts

Monday, November 10, 2014

Custom Logging from stand alone Java

This post shows how to log manually in a file system from Java without involving cordys.

---------------------------------------------------------------------------------------------------------------

package better.faster.smarter.development;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class CustomLogger {
private static Logger logger = Logger.getLogger("IsureTransUtil");
private static FileHandler fh=null;
protected static void logEntry(String message)
{
try{
DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd");
       Calendar cal = Calendar.getInstance();          
    String logFile="IsureTransUtil_"+dateFormat.format(cal.getTime())+".log"; 
//FileHandler takes two parameter 1st one being the fileName and the second one being append mode. If set to true then it will append to the existing log file else will create new one.
    fh = new FileHandler(logFile,true);        
    logger.addHandler(fh);
    SimpleFormatter formatter = new SimpleFormatter();  
       fh.setFormatter(formatter); 
       logger.log(Level.SEVERE, message);        
}
catch(Exception e)
{

}
finally{
//Until you close the file handler the bytes which were written will not get reflected.
fh.close();
}
}
}

-----------------------------------------------------------------------------------------------
Usage of the above class
----------------------------------------------------------

package better.faster.smarter.development;

public class HowToLog{

public static void main(String[] args) {
try{
CustomLogger.logEntry("Started custom Logging");
}
catch(Exception e)
{

}
}
}


Monday, December 16, 2013

Logging in File system from Java

For places where you cannot debug, you can use CordysLogger according to your convenience  and debug your piece of code.

Logging into file system from Java
the Jar which needs to be used is managementlib.jar

private static final CordysLogger fileLog = CordysLogger.getCordysLogger(JE_UTILITY.class);

JE_UTILITY : Class Name where you want to Log.
This variable is a class variable.

the variable used is like 
fileLog.log(Severity.ERROR, "Exception in getting Pending tasks for the user "+e.getMessage());
fileLog.log(Severity.ERROR, "Exception in getting Pending tasks for the user",e);