Thursday, 8 December 2011

Configure Log4j on Java Console Applications


Configure Log4j on Java Console Applications

This example demonstrated how to configure Log4j setup using the Proerties file. This example program uses simple satndalone java program for running the example. But, in most of the project scenarios it will be used in the web application. However the configuration file will be the same.

Steps to follow:

Step1: Download log4j jar from log4j main site (or http://www.java2s.com/Code/Jar/JKL/Downloadlog4jjar.htm) and add to your project lib.
Step2: Create a property file log4j.properties
and make sure that it is available to java file(Or modify the path in java file)
Step3: Create a Log4jPropertyTest.java file with below mentioned java code(Log4jPropertyTest.java)
Step4: Compile and run the Log4jPropertyTest.java file
Step5: log4j.appender.Appender1.File=c:\\sample.log
is the actual log file created after running Log4jPropertyTest java file.

log4j.properties

# Set root logger level to DEBUG and its only appender to Appender1.
log4j.rootLogger=DEBUG, Appender1
# Appender1 is set to be a ConsoleAppender.
log4j.appender.Appender1=org.apache.log4j.RollingFileAppender
log4j.appender.Appender1.File=c:\\sample.log
# DEBUG uses PatternLayout.
log4j.appender.DEBUG.layout=org.apache.log4j.PatternLayout
log4j.appender.DEBUG.layout.ConversionPattern= %d{dd MMM yyyy HH:mm:ss,SSS} %-4r [%t] %-5p %c %x - %m%n
log4j.appender.Appender1.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender1.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss,SSS} %-4r [%t] %-5p %c %x - %m%n


Log4jPropertyTest.java

package com.chandra.log4j;

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

/** * source : http://javacodehelp.blogspot.com/ */
public class Log4jPropertyTest {
 private static Logger logger = Logger.getLogger(Log4jPropertyTest.class
   .getName());
 
 public static void main(String args[]) {
  PropertyConfigurator.configure("c:\\log4j.properties");
  logger.info("Test Log");
  Log4jPropertyTest.read();
  Category root = Category.getRoot();
  root.debug("Message 1");

 }

 public static void read() {

  logger.debug("Great!!!");
  System.out.println("You are in read method");

 }
}


Logging Methods:

Once we obtain an instance of a named logger, we can use several methods of the logger to log messages. The Logger class has the following methods for printing the logging information.

Monday, 5 December 2011

Java PGP sample Program

Easy way to Decrypt the "PGP encrypted file" using JAVA program.

Step1: Download below jars
1) bcpg-jdk14-145.jar
2) bcprov-ext-jdk14-145.jar
3) pgplib-2.5.jar
from http://www.softpedia.com/get/Programming/Components-Libraries/PGPLib.shtml and add them to class path (OR) Make sure the jars available to our java program.
Step 2: Use the below program for Decryption.

Java Program:

package pgplib;
import com.didisoft.pgp.PGPLib;
public class PGPSample {
 public static void main(String[] args) throws Exception {
  // initialize the library instance
  PGPLib pgp = new PGPLib();
  String privateKeyFile = "your .skr file path here";
  String privateKeyPass = "your pass key here";
  String originalFileName = pgp.decryptFile("your .pgp encrypted file path",
    privateKeyFile, privateKeyPass, " your output file path");
 }
}

Sample program:

package pgplib;
import com.didisoft.pgp.PGPLib;
public class PGPSample {
 public static void main(String[] args) throws Exception {
  // initialize the library instance
  PGPLib pgp = new PGPLib();
  String privateKeyFile = "c:\\secring.skr";
  String privateKeyPass = "javacodehelp";
  String originalFileName = pgp.decryptFile("c:\\abc.pgp",
    privateKeyFile, privateKeyPass, "c:\\OUTPUT.txt");
 }
}