Showing posts with label log. Show all posts
Showing posts with label log. 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, May 26, 2014

Log from Java method in WSO2 Framework

Pre-Requisites :

  Ø 
Application server should be up and running.
   Ø  Should be able to write a simple java method using eclipse.
   Ø  One should have knowledge on how to deploy the service from java methods.
   Ø  Should be able to test the service after deployment.

Steps :

    Ø Write a simple Custom Java method by following syntax.


  import org.apache.commons.logging.Log;
      import org.apache.commons.logging.LogFactory;

 private static Log eventLogger = LogFactory.getLog(HelloDH.class);

   Ø  Create a jar and deploy it in the application server (Or Any other way of deploying the java methods onto application server).

   Ø  Test and check for the logs.

o   Test the service by searching for the web service and once tested,You can check for the log as shown in the below screenshot.


You can even find the logs in file system. For ex., "
E:\Kiran\Softwares DH\WSO2\WSO2\wso2as-5.2.1\wso2as-5.2.1\repository\logs"

Tuesday, March 11, 2014

My Gateway is unable to log in filesystem

Problem statement :

I have a custom gateway "sch.oauthint.OAuthHandler.wcp" and I can only debug via logging into filesystem (I know only this way of debugging :P unlike webservices which I can do it via eclipse). So I have encountered a strange problem as I saw there is an error in my application and I want to log. Whenever there is any error it logs in a file which starts lik "Gateway...." in my cordys logs folder but in this scenario I was not able to see the file at all. Hence a blocker for me to understand what is the issue. I have not observed this behavior when I do it in my local system. 

One Approach :

What our(me along with my colleague) understanding was that the user context with which it logs into file system was not having rights to write into file system and our understanding was right. Hence we need to give the rights to the folder "Logs" of the instance of cordys.

Below are the images which would help you in solving the problem :
First immediate task is to find out the user for your website. You can do it by going into IIS settings,


Default Web Site : Website where cordys is installed

You will be seeing "Authentication" as one of the artifact under IIS (highlighted)
Double click on that, You will be viewing a screen which is shown below








Now click on the Name which shows "Enabled" under status, in my case it was "Anonymous Authentication" now on the right side there are "Actions", click on the Edit to view the below screen.

 You will now be able to see what is the User with which it is trying to log.

"IUSR" in my case.Now the next immediate thing you have to do is to give the access permissions on folder "Logs" for this user. This step is shown below
Right click on the folder "Logs" under instance and click on sharing property. Give the access to the user you have found out above "IUSR" in my case.

You can view that in the following screen. 
When I have seen this option the access to the user "IUSR" was "Contribute"(I don't know what is the meaning of this option :P) but I have changed this setting to "Read/Write" which is when my problem got solved.


Note : However this was my approach of solving the problem. There might be different ways also to solve this problem (Windows user can be changed but I did not do that as this approach given me solution).