Thursday, June 7, 2018

Add log file for console java program using log4j




UPLupdateSysProperties.log
2018-06-07 11:56:53 INFO  UPLupdateSysProperties:12 - Sample info message
2018-06-07 11:56:53 WARN  UPLupdateSysProperties:13 - Sample warn message
2018-06-07 11:56:53 ERROR UPLupdateSysProperties:14 - Sample error message
2018-06-07 11:56:53 FATAL UPLupdateSysProperties:15 - Sample fatal message
2018-06-07 12:10:49 INFO  UPLupdateSysProperties:12 - Sample info message
2018-06-07 12:10:49 WARN  UPLupdateSysProperties:13 - Sample warn message
2018-06-07 12:10:49 ERROR UPLupdateSysProperties:14 - Sample error message
2018-06-07 12:10:49 FATAL UPLupdateSysProperties:15 - Sample fatal message



Create java project and class. Put files log4j.properties and log4j.jar in "src" folder and add jar reference in build path.

modify log file location in file log4j.properties 
log4j.appender.file.File=D:\\Logs\\UPLupdateSysProperties.log

This will create folder/s (if not exists), log file at specified location. 
Whenever program get executed, new messages will get appended in log file.


UPLupdateSysProperties.java
package com.upl.utility;

import org.apache.log4j.Logger;

public class UPLupdateSysProperties {

       final static Logger logger = Logger.getLogger(UPLupdateSysProperties.class);
      
       public static void main(String[] args){
             
        logger.debug("Sample debug message");
        logger.info("Sample info message");
        logger.warn("Sample warn message");
        logger.error("Sample error message");
        logger.fatal("Sample fatal message");
       
              System.out.println("dbc1");
       }
}



log4j.properties
# Root logger option
log4j.rootLogger=INFO, stdout, file

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:\\Logs\\UPLupdateSysProperties.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n


Output
2018-06-07 12:27:32 INFO  UPLupdateSysProperties:12 - Sample info message
2018-06-07 12:27:32 WARN  UPLupdateSysProperties:13 - Sample warn message
2018-06-07 12:27:32 ERROR UPLupdateSysProperties:14 - Sample error message
2018-06-07 12:27:32 FATAL UPLupdateSysProperties:15 - Sample fatal message
dbc1



Wednesday, June 6, 2018

dbc blogger posts list

List of posts on blogger


a00 – General


1



2



3





a01 – C programs




a02 – Visual Basics 6




a03 – SQL server




a04a – Core Java




a04b – IBM CE-PE API


1







a04c – Advanced Java


a05 – ASP reports
a06 – Csharp ASPdotNET
a07a – Javascript
a07b – DOJO Javascript




Eclipse Basic Usage

Install Eclipse LUNA and define Workspace (D:\dScode\dCoreJavaPractice)




After completing java code, right click in code area & choose “Run As” & then “Java application”.
Code will get compiled & executed (if error free) and output can be seen in console window at bottom.

Right click Project name & select “export”, to create executable/JAR file (JavaARchive)

Select option “Runnable JAR file” from Java section and click “Next” button
Select proper Class name (having Main() method) in “Launch configuration”  and browse desired Export destination with JAR file name and Click “Finish” finally.





We can execute JAR file on command prompt (where JAR file located) using below command:
                D:\>java -jar <jar file name>
                D:\>java -jar HelloJavaProject.jar

D:\>jar xf HelloJavaProject.jar                            extract jar file
D:\>jar tf HelloJavaProject.jar                            view contents inside jar file









Keep all required jar files in a Workspace folder (D:\DBChavan\dJavaWorkspace\Jars)
and add those jar files externally in project.
Right click project name on select “Properties” i.e. Project Properties.
In project properties “Java Build Path” and in tab “Libraries” click button “Add external JARs...”
and browse jar files kept in workspace folder.
In project explorer external jar files will be seen as “Referenced Libraries”