Showing posts with label Core Java. Show all posts
Showing posts with label Core Java. Show all posts

Monday, June 28, 2010

You need to read an application server log file , but you do not know where it is !!!

Background:

One Friday at around 10.30 at night , a very close friend of mine called me on my mobile and told me that he is facing a problem.He is working on a maintenance project which runs on a server , of which the log file location is unknown.He does not have the documents or time to do RnD on searching the log files also.But he needs to have a look at the log immediately !! There is a severe bug he needs to fix within few hours , otherwise there will be some sort of escalations.

Initially I suggested him to read the server related docs (which I myself have read on rare occasions !! ) and do some Googling on this .In reply he told me that he has already done that and still he is stuck .He needs a quick simple working solution for the time being !!

Problem Description:

To get the log file of a server at a known directory , irrespective of its default location.

Solution (or patch ?? ):

What I suggested for a quick fix is to use the System.setErr() and System.setOut() method of java, to point the console output to a custom location.Both methods should have a PrintStream object as argument, which will point to a specific file provided by you .In that case all the prints or exceptions will be stored in that specific location.

Sample code:


package com.ayan.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;

/**
* This class is an utility class containing a method to change the console
* output location as per the choice of the user .
*
* To use this util file ,the User just needs to add one liner code like :
* ConsoleUtil.setSystemOutPutLocation(outFileLocation,isError);
*
*
* @author ayandutta
*
*/
public class ConsoleUtil
{
public static String NORMAL_OUTPUT_FILE_NAME = "SysoutFile.txt";
public static String ERROR_FILE_NAME = "SysError.txt";
public static String OUTPUT_DIRECTORY = "c:" ;

public static String NORMAL_OUTPUT_FILE_LOCATION = OUTPUT_DIRECTORY + File.separator + File.separator+NORMAL_OUTPUT_FILE_NAME;
public static String ERROR_FILE_LOCATION =OUTPUT_DIRECTORY + File.separator + File.separator+ERROR_FILE_NAME;

/**
* This method sets the console out put location file.
* It works for sysout or syserror
* @param outFileLocation
* @param isError
*/
public static void setSystemOutPutLocation(String outFileLocation,boolean isError)
{
try
{
FileOutputStream outFileStr = new FileOutputStream(outFileLocation, true);
PrintStream outFilePrintStream = new PrintStream(outFileStr);
if(isError)
{
System.setErr(outFilePrintStream);
//System.out.println(" Error file is changed to : " +outFileLocation);
}
else
{
System.setOut(outFilePrintStream);
//System.out.println(" Output file is changed to : " +outFileLocation);
}
}
catch (Exception e)
{
System.out.println("Exception inside method setSystemOutPutLocation(),printing stack trace...");
e.printStackTrace();
}
}

public static void main(String[] args)
{


//Normal Sysouts and Syserrors Before console location is changed
System.out.println("Text for normal out put before file configuration");
System.err.println("Text error out put before file configuration");

//Here is the code where Sysouts and Syserrors console location is changed
setSystemOutPutLocation(NORMAL_OUTPUT_FILE_LOCATION, false);
setSystemOutPutLocation(ERROR_FILE_LOCATION, true);

//Normal Sysouts and Syserrors after console location is changed
System.out.println("Text for normal out put after file configuration");
System.err.println("Text error out put after file configuration");


//By force generating an Exception
generateException();
}

/**
* Just a method to create an exception by force
*/
public static void generateException()
{
try
{
String nullStr = null;
System.out.println(nullStr.length());
}
catch (Exception e)
{
System.out.println("Exception inside method generateException(),printing stack trace...");
e.printStackTrace();
}
}

/**
* Just a method to create an exception by force
*/
public static void printExecutionLocation()
{
System.out.println(System.getProperty("user.dir"));
}

public static void printAllEnvironmentInfo()
{
System.out.println(System.getProperties());
}
}

How to use this program :

Now invoke setSystemOutPutLocation(NORMAL_OUTPUT_FILE_LOCATION, false) and setSystemOutPutLocation(ERROR_FILE_LOCATION, true) from any of the classes of your running application .The log file will be generated on the location provided.

Disclaimer:

The solution (I would rather mention it as a patch ) that I provided at that point of time is just for solving the log file related issue quickly .It is not at all a complete solution .