Learning The "J"

Home » JAVA Learning » JAVA Core » Log4J » Log4J

Log4J

Apache log4j is a Java-based logging utility. It was originally written by Ceki Gülcü and is now a project of the Apache Software Foundation. log4j is one of several Java logging frameworks.

package logtest;

public class LogTest {

    /**
     * @param args the command line arguments
     */
    public static ActivityLog log = new ActivityLog(LogTest.class);
    
    public static void main(String[] args) {           
       log.logToFile(" Problem   ",ActivityLog.LogLevel.WARN);
    }
}

package logtest;

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

import java.util.Calendar;
import java.util.Date;
import java.net.UnknownHostException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.swing.JOptionPane;

public class ActivityLog {

    private static Logger logger;

    public enum LogLevel {

        DEBUG, INFO, ERROR, WARN
    }
    public String terminal = "UNKNOWN";

    public ActivityLog(Class c) {
        try {
            terminal = java.net.InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException ex) {
            ex.printStackTrace();
            System.out.println("UnKnown Host"); //Log For

            logToFile("UnKnown Host", LogLevel.INFO);
        }

        logger = Logger.getLogger(c);

        System.setProperty("fName", "c:\\" + getDateTime() + "_server.log");
        PropertyConfigurator.configure("log4j.properties");
    }

    private static String getDateTime() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        return dateFormat.format(date);
    }

    public static void msgBox(String msg) {
        JOptionPane.showMessageDialog(null, msg);
    }

    public void logToFile(String msg, LogLevel logLevel) {

        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss");
        String date = sdf.format(cal.getTime()).toUpperCase();
        switch (logLevel) {

            case DEBUG:
                try {
                    logger.debug(msg + " @ " + terminal + "   " + date);
                    //msgType="DEBUG";
                } catch (Exception ex) {
                    System.out.println("Unable to perform logging Operations");
                }
                break;
            case INFO:
                try {
                    logger.info(msg + " @ " + terminal + "   " + date);
                    //msgType="INFO";
                } catch (Exception ex) {
                    System.out.println("Unable to perform logging Operations");
                }
                break;
            case ERROR:
                try {
                    logger.error(msg + " @ " + terminal + "   " + date);
                    //msgType="ERROR";
                } catch (Exception ex) {
                    System.out.println("Unable to perform logging Operations");
                }
                break;
            case WARN:
                try {
                    logger.warn(msg + " @ " + terminal + " /  " + System.getProperty("user.name") + date);
                    //msgType="WARN";
                } catch (Exception ex) {
                    System.out.println("Unable to perform logging Operations");
                }
                break;
        }
    }    
}

Place the log4j.properties file in the root folder of your project and put the following things in it


log4j.rootLogger=DEBUG, CA, FA

#Console Appender
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

#File Appender
log4j.appender.FA=org.apache.log4j.FileAppender
log4j.appender.FA.File=${fName}
log4j.appender.FA.layout=org.apache.log4j.PatternLayout
log4j.appender.FA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

# Set the logger level of File Appender to WARN
log4j.appender.FA.Threshold = INFO

Advertisements

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

Start here

%d bloggers like this: