Log4j API – Sample Programs


Log4j Examples:

The basic example to implement the Log4j:

import org.apache.log4j.Logger;

public class LogExample {

public LogExample() {
}
static Logger log = Logger.getLogger(LogExample.class);

public static void main(String argsp[]) {

log.debug(“projectname-modulename-Class-method-Here is some DEBUG”);
log.info(“projectname-modulename-Class-method-Here is some INFO”);
log.warn(“projectname-modulename-Class-method-Here is some WARN”);
log.error(“projectname-modulename-Class-method-Here is some ERROR”);
log.fatal(“projectname-modulename-Class-method-Here is some FATAL”);
log.warn(“projectname-modulename-Class-method-Here is some WARN”);

}
}

 

The sample example to read the log4j configurations from properties file:

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class Example2  {

public static void main(String a[])
{
Logger l=Logger.getLogger(Example2.class);

PropertyConfigurator.configure(“src/prop.properties”);

l.setLevel(Level.DEBUG);
l.debug(“Myprojectname-modulename-Class-method-ok executedddddddddddddd”);
l.info(“projectname-modulename-Class-method-some infoooooooooo”);
l.warn(“projectname-modulename-Class-method-warn msg”);
l.error(“projectname-modulename-Class-method-error msg……”);
l.fatal(“projectname-modulename-Class-method-this is some fatal errrrrrrr”);
}
}

Log4j configurations in properties file: prop.properties

log4j.rootLogger=debug, stdout
log4j.appender.stdout=org.apache.log4j.DailyRollingFileAppender
log4j.appender.stdout.File=logs/dailyrollfile3333.txt
log4j.appender.stdout.DatePattern=’.’yyyy-MM-dd-HH-mm
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%r  [%t]  %p  %c  %m  %d  %n


Implementing log4j with Console appender :

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;

//ConsoleAppender
public class Example1 {

public static void main(String a[])
{
Logger l=Logger.getLogger(Example1.class);
Layout lay=new SimpleLayout();
ConsoleAppender ap=new ConsoleAppender(lay);

l.addAppender(ap);
l.setLevel(Level.DEBUG);

try{
Class.forName(“oracle.jdbc.driver.OracleDriver”);
l.debug(“driver loaded”);
Connection con=DriverManager.getConnection(“jdbc:oracle:thin:@192.168.1.214:1521:XE”,”kiran”,”kiran”);
l.info(“connection established successfully”);

}catch(ClassNotFoundException e)
{
l.fatal(” OOPS………DriverClass Problem(plz check Driverclass)”);
e.printStackTrace();
}catch(SQLException se)
{
l.fatal(“OOPS…………Db Connection Problem”);
se.printStackTrace();
}
catch(Exception se)
{
l.error(“OOPS…………Db Problem”);
se.printStackTrace();
}

}
}


File Appender example in Log4j :

import java.sql.Connection;
import java.sql.DriverManager;

import org.apache.log4j.*;

//FileAppender

public class Example2  {

public static void main(String a[])
{
Logger l=Logger.getLogger(Example2.class);
try{
Layout lay=new SimpleLayout();
FileAppender ap=new FileAppender(lay,”sample.log”,true);
l.addAppender(ap);
l.setLevel(Level.DEBUG);
}catch(Exception d)
{
l.debug(“log4j stmts problem”);
}
try{
Class.forName(“oracle.jdbc.driver.OracleDriver”);
l.debug(“driver loaded……”);
Connection con=DriverManager.getConnection(“jdbc:oracle:thin:@192.168.1.214:1521:XE”,”kiran”,”kiran”);
l.info(“connection established successfully……….”);

}catch(Exception e)
{
l.fatal(“Db Problem”);
e.printStackTrace();
}
}
}

Advertisements
About

I born in Good cultural place called India. My family is traditional family. We fallow all Indian traditions.

Posted in Log4j
3 comments on “Log4j API – Sample Programs
  1. Joan says:

    Having read this I thought it was rather informative. I appreciate you finding the time and energy to put this short
    article together. I once again find myself spending a significant amount of time
    both reading and leaving comments. But so what, it was still worthwhile!

  2. zbigz premium account says:

    Excellent post. I was checking continuously this blog and I am impressed!
    Very helpful ifo specially the last prt 🙂 I care for such information much.

    I was looking for this particular info for a very long time.
    Thank youu and good luck.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 676 other followers

Calendar
January 2011
M T W T F S S
« Nov   Jul »
 12
3456789
10111213141516
17181920212223
24252627282930
31  
Mallikarjun Gunda

This is Mallikarjun Gunda, I am working as a software professional. Blogging is my Passion. This blog contains the posts on Java related technologies and programming languages. I am happy to share my knowledge through the blogs.

Blog Stats
  • 228,949 hits
IndiBlogger
malliktalksjava.wordpress.c...
70/100
All tags
AngularJS Apache Cassandra Arrays in Java Cassandra Cassandra Database Cassandra examples client stubs creating a java web service creating a web service using java difference between interface and abstract class Differences between SOAP and RESTfull webservices Display div as Overlay using JQuery download the spring basic application File directory Generating webservice client stubs Hibernate hibernate exceptions How to display over using JQuery how to generated web service client stubs in STS How to implement Spring basic application how to write singleton design pattern in java interface in java Java Java Arrays Java Example Programs Java examples Java interview questions java programs java project Java sample Programs Java Script JavaScript JavaServer Faces Java Sorting alogorithms Java Tutorials javatutorials Java Web services JDBC open shift openshift Overlay implementation using JQuery Overlay in JQuery POI API POI Examples public class public static void main Redhat OpenShift representational state transfer Restfull webservices sample programs in java Service-oriented architecture Singleton design pattern in java SOAP SOAP Webservices software Spring Framework Spring Interview Questions Spring MVC Spring MVC Example program Spring MVC sample Program Spring Sample Programs Spring Tutorials Struts technology Top Java Interview questions w3c dom Web service web service client Web Service Description Language webservices Web services writing singleton design pattern in Java WSDL Xml xml parsers