Showing posts with label other2. Show all posts
Showing posts with label other2. Show all posts

February 11, 2012

How to Remove Virus NewFloder.exe your PC?

I want to tell you a story, two days back i got affected by this virus very badly as it eat up all my empty hard disk space of around 700 MB .


I was surprised that my most reliable friend Avast, for the first time failed me in this war against viruses but then again avg and bitdiffender also failed against it. This virus is know popularly as regsvr.exe virus, or as new folder.exe virus and most people identify this one by seeing autorun.inf file on their pen drives, But trend micro identified it as WORM_DELF.FKZ. It is spreading mostly using pen drives as the medium.

Well, so here is the story of how i was able to kill the monster and reclaim my hard disk space.
Manual Process of removal

I prefer manual process simply because it gives me option to learn new things in the process.

So let’s start the process off reclaiming the turf that virus took over from us.

1.Cut The Supply Line

1.Search for autorun.inf file. It is a read only file so you will have to change it to normal by right clicking the file , selecting the properties and un-check the read only option

2.Open the file in notepad and delete everything and save the file.

3.Now change the file status back to read only mode

4.so that the virus could not get access again.


5.Click start->run and type msconfig and click ok

6.Go to startup tab look for regsvr and uncheck the option click OK.

7.Click on Exit without Restart, cause there are still few things we need to do before we can restart the PC.

8.Now go to control panel -> scheduled tasks, and delete the At1 task listed their.


2.Open The Gates Of Castle

1.Click on start ->

2.run and type gpedit.msc and click Ok.

3.If you are Windows XP Home Edition user you might not have gpedit.msc in that case download and install it from Windows XP Home Edition: gpedit.msc and then follow these steps.

4.Go to users configuration->Administrative templates->system

5.Find “prevent access to registry editing tools” and .

6.change the option to disable

7.Once you do this you have registry access back.

3.Launch The Attack At Heart Of Castle

1.Click on start->run and type regedit and click ok

2.Go to edit->find and ,

3.start the search for regsvr.exe

4.Delete all the occurrence of regsvr.exe; remember to take a backup before deleting. KEEP IN MIND regsvr32.exe is not to be deleted. Delete regsvr.exe occurrences only.

5.At one ore two places you will find it after explorer.exe in theses cases only delete the regsvr.exe part and not the whole part. E.g. Shell = “Explorer.exe regsvr.exe” the just delete the regsvr.exe and leave the explorer.exe

4.Seek And Destroy the enemy soldiers, no one should be left behind

1.Click on start->search->for files and folders.

2.Their click all files and folders

3.Type “*.exe” as filename to search for

4.Click on ‘when was it modified ‘ option and select the specify date option

5.Type from date as 1/31/2008 and also
6.type To date as 1/31/2008


7.Now hit search and wait for all the exe’s to show up.

8.Once search is over select all the exe files and shift+delete the files, caution must be taken so that you don’t delete the legitimate exe file that you have installed on 31st January.

9.Also selecting lot of files together might make your computer unresponsive so delete them in small bunches.

10.Also find and delete regsvr.exe, svchost .exe( notice an extra space between the svchost and .exe)

5.Time For Celebrations

1.Now do a cold reboot (ie press the reboot button instead) and you are done.

I hope this information helps you win your own battle against this virus. Soon all antivirus programs will be able to automatically detect and clean this virus. Also i hope Avast finds a way to solve this issues.

As a side note i have found a little back dog( winpatrol ) that used to work perfectly on my old system. It was not their in my new PC, I have installed it again , as I want to stay ahead by forever closing the supply line of these virus. You can download it form Winpatrol website.

UPDATE : Avast   

Check out How to stop regedit, task manager and msconfig from closing automatically if your regedit or msconfig closes automatically.

urs ramanujadasu
Read more ...

June 16, 2011

How to swap two variables, without using third variable ?

Swap two variables with out using third variable:
a=5,b=10
a=a+b;
b=a-b;
a=a-b;
-------
use xor to swap
a = a^b
b= a^b
a= a^b

-------
or
a=a*b;
b=a/b;
a=a/b;
Read more ...

May 26, 2011

Unit Testing, Automated testing, java, Junit

 Introduction

JUNIT—An Introduction


Ø  It is an open source Unit testing framework.
Ø  It provides a simple way to explicitly test specific areas of a Java program
Ø  It helps us in writing and running automated tests.

Why do you need Junits

Ø  They are simple.
Ø  They check results of their own and provide immediate results.
Ø  It’s inexpensive.
Ø  It increases the stability of software which is being tested.

Design of a Junit

Ø  The design of a junit is simple and it can be done in two ways :
        - Command pattern
      - Composite Pattern 

How to install a Junit in your PC

Ø  Installing a Junit is not a complex task.
Ø  The following are the steps involved while installing a junit in you PC:
§  Down Load it from web
§  Unzip the junit.zip distribution file to a directory
§  Add JUnit to the classpath
      Set CLASSPATH=%Directory-name%\junit.jar
§  Test the installation by using either the textual or graphical   test runner to run the sample tests which is present in the                           directory.

Writing a test Case Using Junit

Ø  Define a subclass of TestCase.
Ø  Override the setUp() and tearDown() (optional)method.
Ø  Define one or more public test () methods of the   object(s) under test and assert expected results.

How to write a Test Suite

Ø  A test suite is nothing but adding up of several test cases which help us to run all test cases in one go.
Ø  The Steps involved in writing a Suite are :
§  Write a Java class that defines a static suite () factory method that creates a TestSuite containing all the tests.

§   Define a main () method that runs the TestSuite in batch mode(which can be optional).

How to Run Junit

Ø  Junit provides a graphical as well as textual interface for testing. User can choose from either of them for testing.

     For graphical we have to import
        java junit.swingui.TestRunner

      For Textual we have to import
      Java junit.textui.TestRunner

Trends

Ø  Junit 4.4 Version is being released.
Ø  Junit Can be downloaded from SourceForge.
Ø  Official Website of Junit: http://www.junit.org/
Ø  Junits is used in other languages like C#,FORTRAN,PERL

Example of a JUNIT

A Sample Class to test with Junit
     public class Hello
              {
                        Public String getHelloWorld () { return “Hello World";
                        }
             

     Test Case For helloworld
     public class testHello extends TestCase
              {
              protected Hello hello;
              protected void setUp()
                        {
                          hello = newHello ();
                        }

                                                                                                    
public void testGetHelloWorld()
     {
               System.out.println ("HelloWorld example");
                String result = "Hello World";
                //Assert statement is given to check       whether the given result is equal to the returnvalueof function
               Assert.assertEquals (result, Hello.getHelloWorld ());
     }
//Main method is used for run tests for this class only
public static void main(String[] args)
      {
           junit.swingui.TestRunner.run (testHello.class);
      }
}
}


Unit Testing is a test development process by which individual module of the class will be tested. Junit is a simple test framework for writing unit test cases for java programs. Junit is very helpful in test driven development (TDD); it provides functionalities to the developer for writing and running unit test cases for a project in development.


Test Coverage: Test coverage helps in measuring the amount of code covered under testing.

Test Coverage criteria’s:
1.    Statement coverage
2.    Branch coverage
3.    Case coverage
4.    Path Coverage.

Junit plays a vital role in improving the test coverage for projects.
        Junit 4.x:
Junit 4.x is a completely different API from its earlier versions of Junit. It is based on new features added to java 5.0 and uses annotation for indentifying the test cases. Junit 4.x is simple and easier API for writing test cases and provides flexible ways to the user to introduce flexible initialization, test cleanups, timeout messages, and creating parameterized test cases.


Test class with Junit 4.x:
1.    Annotate a test method with @Test (Used by the framework for identifying the test case).
2.    Use any suitable asserts method provided by framework to test expected and actual results.

Some of the important features Added in Junit 4.x:

1.  Annotations:
Junit 4.x simplifies the testing by exploiting the java 5.0’s new feature called annotations. Annotations feature really helpful in identifying the test cases instead of relying on sub classing, reflection and naming features.

Available annotation in Junit 4.x:

 Annotation.
        Description.
@Test
Used to identify the test method.
@Before
Executes the method annotated with “@Before” before executing the test method.
Ex: Test method setup, providing input data.
@After
Executes the method annotated with “@After” after executing the test method.
Ex: Test data clean up operations.
@BeforeClass
Executes the method before start executing all test methods present in test class.
Ex: Providing DB connections.
@AfterClass
Executes the method after executing all test methods present in test class.
Ex: removing DB connections.
@ignore
Test method annotated with @ignore will be ignored by the framework.
@Test(timeout=200)
Fails the test case if execution time exceeds more than 200 ms.
@Test(expected=IllegalArgument
Exception.class)
Tests the method only if it throws mentioned exception.


           2.  Assert Statements:
Assert statements helps in testing the excepted results against the actual results.

      Some of the available Assert methods in Junit 4.x:

      Note: AssertArrayEquals() methods are available for all basic types in java.

      3.  Parameterized Test Cases:
This will allows us to run the same test case with different data. The prerequisite for parameterized test cases is to have an                      annotation @RunWith for the test class.

          @RunWith (value = Parameterized.class)

       4.Test Suits:
In Junit 3.* we need to add a suit() method for test classes to run all test cases as a test suit. But in Junit 4.x we can achieve this by using annotations. We use @RunWith and @Suite annotations.


package com.test;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({TestSampleCalculator.class})

publicclass RunAllTests {

}

Installation:
                  1. Download Junit-4.*.jar from http://www.junit.org/   (* - any sub version of junit 4).
                  2. To make Junit jar available to the project we have to add it to the java build path (Eclipse – using add libraries). 

Sample Example:  SampleCalculator.java
                 
   package com.code;

 publicclass SampleCalculator {

publicint add(int a, int b){
int result = a + b;   
return result;
     }

publicint sub(int a, int b){
int result = a - b;
return result;
     }

publicint div(int a, int b){
int result = a / b;
return result;
     }
publicint mul(int a, int b){
int result = a * b;
return result;
     }
    }

Junit Test case for the above java class:

package com.test;
importstatic org.junit.Assert.*;
import com.code.SampleCalculator;

import org.junit.Ignore;
import org.junit.Test;

publicclass TestSampleCalculator {

@Test
publicvoid add(){
int[] actuals = {new SampleCalculator().add(1, 1),  newSampleCalculator().add(2, 2)};
assertArrayEquals(expecteds, actuals);
    }

@Test
publicvoid sub(){
int[] actuals = {new SampleCalculator().sub(4, 2),  new SampleCalculator().sub(6, 2)};
assertArrayEquals(expecteds, actuals);
    }

@Test
publicvoid div(){
int[] actuals = {new SampleCalculator().div(4, 2), new SampleCalculator().div(8, 2)};
assertArrayEquals(expecteds, actuals);
    }
@Test
publicvoid mul(){
int[] actuals = {new SampleCalculator().mul(1, 2), new SampleCalculator().mul(2, 2)};
assertArrayEquals(expecteds, actuals);
    }

// This test case expects an ArithmeticException. IF the method throws it, then the test case will                               pass.
@Test(expected= ArithmeticException.class)
publicvoid divCheck(){
assertEquals(2, new SampleCalculator().div(4, 0)); 
    }


// This test case will be ignored by the Framework.
@Ignore
publicvoid nulCheck(){
assertEquals(10, new SampleCalculator().mul(2, 6));
      }

publicstaticint[] expecteds = {2,4};
}.
Read more ...

February 06, 2011

Log4J

An Idea of Log4J:
----------------

what is Log4J and it's advantages & disadvantages ...
Log4j API is an Apache project. It provides you the logging mechanism. which allows you to direct the debug & error (basically) statments onto a particular file/database/messaging system etc. Its provide a much cleaner way to store the output statment during the program execution. It can record information upto a class level. For ex: You have X Y Z ...N classes....lets say Z classe has thrown and exception/error which you logged using Log4j while finding the rootcause you would see those error/debug files...It will show you the absolute class name as well if configured so. Log4j is fully configurable using either. properties or .xml.
Disadvantage is it take 5 nanoseconds each time to log a statement or when it is invoked.

Introduction of LOG4J:
---------------------

Logging within the context of program development constitutes inserting statements into the program that provide some kind of output information that is useful to the developer. Examples of logging are trace statements, dumping of structures and the familiar System.out.println or printf debug statements. log4j offers a hierarchical way to insert logging statements within a Java program. Multiple output formats and multiple levels of logging information are available.

By using a dedicated logging package, the overhead of maintaining thousands of System.out.println statements is alleviated as the logging may be controlled at runtime from configuration scripts. log4j maintains the log statements in the shipped code. By formalising the process of logging, some feel that one is encouraged to use logging more and with higher degree of usefulness.

How to Instal:
-------------

In order to use the tools we are about to install it is necessary to setup the operating environment so that the tools know where to find stuff they need and the operating system knows where to find the tools. A understanding of how to do this is essential as you will be asked to change the operating environment. I have comprehensively covered this in documents entitled Configuring A Windows Working Environment and Configuring A Unix Working Environment.
Download the log4j distribution from http://jakarta.apache.org/log4j/docs/download.html.

Extract the archived files to some suitable directory.

Add the file dist/lib/log4j-1.2.6.jar to your CLASSPATH environment variable.

Download http://apache.rmplc.co.uk/dist/xml/xerces-j/Xerces-J-bin.2.6.0.zip and unzip it to a temporary directory. Copy the files xercesImpl.jar and xmlParserAPIs.jar to some permanent location and append their paths to the CLASSPATH environment variable.

log4j Basic Concepts:

The use of log4j revolves around 3 main things:

public class Logger

Logger is responsible for handling the majority of log operations.

public interface Appender

Appender is responsible for controlling the output of log operations.

public abstract class Layout

Layout is responsible for formatting the output for Appender.

Logger:
------

The logger is the core component of the logging process. In log4j, there are 5 normal levels Levels of logger available (not including custom Levels), the following is borrowed from the log4j API (http://jakarta.apache.org/log4j/docs/api/index.html):

static Level DEBUG

The DEBUG Level designates fine-grained informational events that are most useful to debug an application.

static Level INFO

The INFO level designates informational messages that highlight the progress of the application at coarse-grained level.

static Level WARN

The WARN level designates potentially harmful situations.

static Level ERROR

The ERROR level designates error events that might still allow the application to continue running.

static Level FATAL

The FATAL level designates very severe error events that will presumably lead the application to abort.

In addition, there are two special levels of logging available: (descriptions borrowed from the log4j API http://jakarta.apache.org/log4j/docs/api/index.html):

static Level ALL

The ALL Level has the lowest possible rank and is intended to turn on all logging.

static Level OFF

The OFF Level has the highest possible rank and is intended to turn off logging.

The behaviour of loggers is hierarchical. The following table illustrates this:

Figure?1.?Logger Output Hierarchy
Logger Output Hierarchy

A logger will only output messages that are of a level greater than or equal to it. If the level of a logger is not set it will inherit the level of the closest ancestor. So if a logger is created in the package com.foo.bar and no level is set for it, it will inherit the level of the logger created in com.foo. If no logger was created in com.foo, the logger created in com.foo.bar will inherit the level of the root logger, the root logger is always instantiated and available, the root logger is assigned the level DEBUG.

There are a number of ways to create a logger, one can retrieve the root logger:

Logger logger = Logger.getRootLogger();

One can create a new logger:

Logger logger = Logger.getLogger("MyLogger");

More usually, one instantiates a static logger globally, based on the name of the class:

static Logger logger = Logger.getLogger(test.class);

All these create a logger called "logger", one can set the level with:

logger.setLevel((Level)Level.WARN);

You can use any of 7 levels; Level.DEBUG, Level.INFO, Level.WARN, Level.ERROR, Level.FATAL, Level.ALL and Level.OFF.

Appender:
--------

The Appender controls how the logging is output. The Appenders available are (descriptions borrowed from the log4j API http://jakarta.apache.org/log4j/docs/api/index.html):

ConsoleAppender: appends log events to System.out or System.err using a layout specified by the user. The default target is System.out.

DailyRollingFileAppender extends FileAppender so that the underlying file is rolled over at a user chosen frequency.

FileAppender appends log events to a file.

RollingFileAppender extends FileAppender to backup the log files when they reach a certain size.

WriterAppender appends log events to a Writer or an OutputStream depending on the user's choice.

SMTPAppender sends an e-mail when a specific logging event occurs, typically on errors or fatal errors.

SocketAppender sends LoggingEvent objects to a remote a log server, usually a SocketNode.

SocketHubAppender sends LoggingEvent objects to a set of remote log servers, usually a SocketNodes

SyslogAppendersends messages to a remote syslog daemon.

TelnetAppender is a log4j appender that specializes in writing to a read-only socket.

One may also implement the Appender interface to create ones own ways of outputting log statements.
3.2.1.?Using A ConsoleAppender

A ConsoleAppender can be created like this:

ConsoleAppender appender = new ConsoleAppender(new PatternLayout());

Which creates a console appender, with a default PatternLayout. The default output of System.out is used.
3.2.2.?Using A FileAppender

A FileAppender can be created like this:

FileAppender appender = null;
try {
appender = new FileAppender(new PatternLayout(),"filename");
} catch(Exception e) {}


The constructor in use above is:

FileAppender(Layout layout, String filename)
Instantiate a FileAppender and open the file designated by filename.


Another useful constructor is:

FileAppender(Layout layout, String filename, boolean append)
Instantiate a FileAppender and open the file designated by filename.


So that one may choose whether or not to append the file specified or not. If this is not specified, the default is to append.
3.2.3.?Using A WriterAppender

A WriterAppender can be created like this:

WriterAppender appender = null;
try {
appender = new WriterAppender(new PatternLayout(),new FileOutputStream("filename"));
} catch(Exception e) {}

Layout:
------

The Appender must have have an associated Layout so it knows how to format the output. There are three types of Layout available:

HTMLLayout formats the output as a HTML table.

PatternLayout formats the output based on a conversion pattern specified, or if none is specified, the default conversion pattern.

SimpleLayout formats the output in a very simple manner, it prints the Level, then a dash '-' and then the log message.

3.4.?Basic Examples Illustrating this
3.4.1.?SimpleLayout and FileAppender

Here is a very simplistic example of a program implementing a SimpleLayout and FileAppender:

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;
import org.apache.log4j.FileAppender;
public class simpandfile {
static Logger logger = Logger.getLogger(simpandfile.class);
public static void main(String args[]) {
SimpleLayout layout = new SimpleLayout();

FileAppender appender = null;
try {
appender = new FileAppender(layout,"output1.txt",false);
} catch(Exception e) {}

logger.addAppender(appender);
logger.setLevel((Level) Level.DEBUG);

logger.debug("Here is some DEBUG");
logger.info("Here is some INFO");
logger.warn("Here is some WARN");
logger.error("Here is some ERROR");
logger.fatal("Here is some FATAL");
}
}


You can download it: simpandfile.java. And checkout the output produced: output1.txt.
3.4.2.?HTMLLayout and WriterAppender

Here is a very simplistic example of a program implementing a HTMLLayout and WriterAppender:

import java.io.*;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.HTMLLayout;
import org.apache.log4j.WriterAppender;
public class htmlandwrite {
static Logger logger = Logger.getLogger(htmlandwrite.class);
public static void main(String args[]) {
HTMLLayout layout = new HTMLLayout();

WriterAppender appender = null;
try {
FileOutputStream output = new FileOutputStream("output2.html");
appender = new WriterAppender(layout,output);
} catch(Exception e) {}

logger.addAppender(appender);
logger.setLevel((Level) Level.DEBUG);

logger.debug("Here is some DEBUG");
logger.info("Here is some INFO");
logger.warn("Here is some WARN");
logger.error("Here is some ERROR");
logger.fatal("Here is some FATAL");
}
}


You can download it: htmlandwrite.java. And checkout the output produced: output2.html.
3.4.3.?PatternLayout and ConsoleAppender

Here is a very simplistic example of a program implementing a PatternLayout and ConsoleAppender:

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.ConsoleAppender;
public class consandpatt {
static Logger logger = Logger.getLogger(consandpatt.class);
public static void main(String args[]) {

// Note, %n is newline
String pattern = "Milliseconds since program start: %r %n";
pattern += "Classname of caller: %C %n";
pattern += "Date in ISO8601 format: %d{ISO8601} %n";
pattern += "Location of log event: %l %n";
pattern += "Message: %m %n %n";

PatternLayout layout = new PatternLayout(pattern);
ConsoleAppender appender = new ConsoleAppender(layout);

logger.addAppender(appender);
logger.setLevel((Level) Level.DEBUG);

logger.debug("Here is some DEBUG");
logger.info("Here is some INFO");
logger.warn("Here is some WARN");
logger.error("Here is some ERROR");
logger.fatal("Here is some FATAL");
}
}

Using External Configuration Files:
----------------------------------

Log4j is usually used in conjunction with external configuration files so that options do not have to be hard-coded within the software. The advantage of using an external configuration file is that changes can be made to the options without having to recompile the software. A disadvantage could be, that due to the io instructions used, it is slightly slower.

There are two ways in which one can specify the external configuration file: a plain text file or an XML file. Since everything is written in XML these days, this tutorial will focus on the XML approach but will also include relevant plain text examples. To begin with, examine the sample XML config file shown below:

The file starts with a standard XML declaration followed by a DOCTYPE declaration which indicates the DTD(Document Type Definition), this defines the structure of the XML file, what elements may be nested within other elements etc. This file is provided in the log4j distribution under src/java/org/apache/log4j/xml. Next comes the all-encapsulating log4j:configuration element, which was specified as the root element in the DOCTYPE declaration. Nested within the root element are two structures:


Here an Appender is created and called "ConsoleAppender", note that any name could have been chosen, it is because of the contrivity of examples that this name was chosen. The class for the appender is then specified in full, when referring to classes, one always uses the fully qualified class name. An Appender must always have a name and a class specified. Nested within Appender is the layout element which defines the layout to be a SimpleLayout. Layout must always have the class attribute.


The root element always exists and cannot be sub-classed. The example shows the priority being set to "debug" and the appender setup by including an appender-ref element, of which, more that one may be specified. See the file src/java/org/apache/log4j/xml/log4j.dtd in your log4j distribution for more information about the structure of an XML configuration file. The configuration file is pulled into the Java program like this:

DOMConfigurator.configure("configurationfile.xml");

The DOMConfigurator is used to initialise the log4j environment using a DOM tree. Here is the example xml configuration file: plainlog4jconfig.xml. Here is a program which implements this configuration file: files/externalxmltest.java:

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
public class externalxmltest {
static Logger logger = Logger.getLogger(externalxmltest.class);
public static void main(String args[]) {
DOMConfigurator.configure("xmllog4jconfig.xml");
logger.debug("Here is some DEBUG");
logger.info("Here is some INFO");
logger.warn("Here is some WARN");
logger.error("Here is some ERROR");
logger.fatal("Here is some FATAL");
}
}

Here is an XML configuration file for a Logger implementing a FileAppender using a PatternLayout:


You can download this example from here: xmllog4jconfig2.xml. For more examples of using xml files to configure a log4j environment, see the src/java/org/apache/log4j/xml/examples/ directory in the log4j distribution.

Here is the configuration file discussed above, expressed in the form of a plain text file:

# initialise root logger with level DEBUG and call it BLAH
log4j.rootLogger=DEBUG, BLAH
# add a ConsoleAppender to the logger BLAH
log4j.appender.BLAH=org.apache.log4j.ConsoleAppender
# set set that layout to be SimpleLayout
log4j.appender.BLAH.layout=org.apache.log4j.SimpleLayout


You can download it here: plainlog4jconfig.txt. Here is a program implementing this:

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class externalplaintest {
static Logger logger = Logger.getLogger(externalplaintest.class);
public static void main(String args[]) {
PropertyConfigurator.configure("plainlog4jconfig.xml");
logger.debug("Here is some DEBUG");
logger.info("Here is some INFO");
logger.warn("Here is some WARN");
logger.error("Here is some ERROR");
logger.fatal("Here is some FATAL");
}
}

You can download an example program that uses this configuration file here: files/externalplaintest.java. For more examples of using plain text files to configure a log4j environment, see the examples directory in the log4j distribution.

The use of external example files has only been briefly discussed here, it is assumed that you have the capacity to learn more by yourself by studying the examples provided with the log4j distribution and experimenting.

-----------------------------------------------------------------------------
Apache Log4j:

Logging Framework is an abstraction that hide details related to how logging
to be done, from end user. As Log4j is configuration based logging framework,
one can define how logging has to be done, by defining appropriate configuration
for File/JDBC or both

What would be file name or JDBC related connection URL with credentials

Levels of root logger, such as DEBUG, WARN, INFO, ERROR, FATAL etc

Many more, those as covered in example and articles below:

http://www.interview-questions-tips-forum.net/index.php/Apache-Log4j

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

Real time :[Using Log4j Example : A case study discussed]

DISCLAIMER :
The content provided in this page is not warranted or guaranteed by Interview-Questions-Tips-Forum.net.
We are not liable for any negative consequences that may result/arise from implementing
directly/indirectly any information coveredin these pages/articles/tutorials and these content
provided in this web site are intented for educational purpose only.

Please be informed that NONE of the design/code/matter from this page is claiming to be some
sort of best practice and we DO NOT expect any of our visitor/reader of this page to assume
this as some sort of best practice for any context and should not be using this as it is
without appropriate evaluation.
This page intends only to provide bits and pieces of known ways for doing some sort of example
and may not be fit for any other purpose.

In spite of all precautions taken to provide accurate and avoid any typo in these pages,
there might be some issues like grammatical mistakes and typo being observed in these pages,
We extend our sincerest apologies for the same.


Log4j Example : Using various levels of logging using Apache Log4j

Suppose there is a log file to be analyzed for certain issues while
tracing activity is performed. This activity can be real difficult
if one has to find/look for a pin from a box full of sand, type of
situation. This may happen if number of tracing / logging lines of
statements ranges from few hundred to thousand lines for a real
big application with many classes being executed with an operation.

So what do you think can be possible way to simplify this particular
problem?? In my thinking if there could be some way to filter only
those log statements, those are related to one particular class
or component in hand.

Yes, You are right!!! Apache Log4j has a provision for defining
which class to be considered for logging and leaving rest of the
classes not eligible for logging purpose.

In this example I shall try to demonstrate this objective of
configuring log4j.properties file in such a way so as to provide
a filter kind of functionality and this should be done without
any code change as far as the Java class files are concerned.
So this is pretty configuration time setting up activity.

In order to show a method execution flow, I have defined three dummy
class files, such as ExampleController, ExampleService, ExampleDAO
with some dummy kind of methods too.

demo.example.ExampleController.java

package demo.example;

import org.apache.log4j.Logger;

public class ExampleController {
private static Logger logger = Logger.getLogger(ExampleController.class);
public void delegateCall() {
logger.info("Entering delegateMethod");
new ExampleService().doBusiness();
logger.info("Leaving delegateMethod");
}
}
demo.example.ExampleService.java

package demo.example;

import org.apache.log4j.Logger;

public class ExampleService {
private static Logger logger = Logger.getLogger(ExampleService.class);
public void doBusiness() {
logger.info("Entering doBusiness");
new ExampleDAO().doStore();
logger.info("Leaving doBusiness");
}
}

demo.example.ExampleDAO.java

package demo.example;

import org.apache.log4j.Logger;

public class ExampleDAO {
private static Logger logger = Logger.getLogger(ExampleDAO.class);
public void doStore() {
logger.info("Entering doStore");
logger.debug("TODO database call");
logger.info("Leaving doStore");
}
}

As these files shows that ExampleController.delegateCall method
is calling ExampleService.doBusiness and this method is again
calling ExampleDAO.doStore, so there is a chain of method
execution with some logging statements being coded.
Now a very common (I think so) log4j properties file for this example
as follows:
log4j.rootLogger=INFO, A
log4j.appender.A=org.apache.log4j.ConsoleAppender
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern=%d [%t] %-5p %C - %m%n
This log4j.properties file prints out all the logging statements
that Log4j encounters while these method execution, as shown below:
[main] INFO demo.example.ExampleController - Entering delegateMethod
[main] INFO demo.example.ExampleService - Entering doBusiness
[main] INFO demo.example.ExampleDAO - Entering doStore
[main] INFO demo.example.ExampleDAO - Leaving doStore
[main] INFO demo.example.ExampleService - Leaving doBusiness
[main] INFO demo.example.ExampleController - Leaving delegateMethod
Now I have modified log4j.properties file and added the line marked
as bold RED in color
log4j.rootLogger=FATAL
log4j.logger.demo.example=INFO, A
log4j.appender.A=org.apache.log4j.ConsoleAppender
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern=%d [%t] %-5p %C - %m%n
This makes no difference so far as the number of logging statements
on console, for this example is concerned :
But the real difference appeared when I added the class file name
within the second line of log4j properties file as follows:
log4j.rootLogger=FATAL
log4j.logger.demo.example.ExampleController=INFO, A
log4j.appender.A=org.apache.log4j.ConsoleAppender
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern=%d [%t] %-5p %C - %m%n
This makes the log statement being printed on console, restricted
only to the demo.example.ExampleController class file, as show below:
[main] INFO demo.example.ExampleController - Entering delegateMethod
[main] INFO demo.example.ExampleController - Leaving delegateMethod
By this way one can apply some filter/restrictions (Just of understanding)
and log only those statements those are real important to track.
Read more ...

My Favorite Site's List

#update below script more than 500 posts