How run Log4j program without using Properties file?
We will write programs in two ways.
Type 1:
package com.log.appender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import java.util.Properties;
public class Log4jPropr{
static Logger log = Logger.getLogger(Log4jPropr.class);
public static void main(String[] args) {
Properties log4jProperties = new Properties();
log4jProperties.setProperty("log4j.rootLogger", "INFO, myConsoleAppender");
log4jProperties.setProperty("log4j.appender.myConsoleAppender", "org.apache.log4j.ConsoleAppender");
log4jProperties.setProperty("log4j.appender.myConsoleAppender.layout", "org.apache.log4j.PatternLayout");
log4jProperties.setProperty("log4j.appender.myConsoleAppender.layout.ConversionPattern", "%-5p %c %x - %m%n");
PropertyConfigurator.configure(log4jProperties);
log.setLevel(Level.DEBUG);
log.debug("This is a debug message");
log.info("This is an info message");
}
}
Output:
DEBUG com.log.appender.Log4jPropr - This is a debug message
INFO com.log.appender.Log4jPropr - This is an info message
Type-2:
package com.log.appender;
import java.io.IOException;
import org.apache.log4j.DailyRollingFileAppender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
public class Log
{
public static Logger as_looger;
public static void main(String[] args) {
as_looger = Logger.getLogger("articles logging");
as_looger = Logger.getLogger(Log.class.getName());
FileAppender as_appender = null;
try {
as_appender = new DailyRollingFileAppender(new PatternLayout("%d{dd-MM-yyyy HH:mm:ss} %C %L %-5p: %m%n"), "path of file where to write logs","'.'dd-MM-yyyy");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
as_looger.setLevel(Level.DEBUG);
as_looger.addAppender(as_appender);
}
}
We will write programs in two ways.
Type 1:
package com.log.appender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import java.util.Properties;
public class Log4jPropr{
static Logger log = Logger.getLogger(Log4jPropr.class);
public static void main(String[] args) {
Properties log4jProperties = new Properties();
log4jProperties.setProperty("log4j.rootLogger", "INFO, myConsoleAppender");
log4jProperties.setProperty("log4j.appender.myConsoleAppender", "org.apache.log4j.ConsoleAppender");
log4jProperties.setProperty("log4j.appender.myConsoleAppender.layout", "org.apache.log4j.PatternLayout");
log4jProperties.setProperty("log4j.appender.myConsoleAppender.layout.ConversionPattern", "%-5p %c %x - %m%n");
PropertyConfigurator.configure(log4jProperties);
log.setLevel(Level.DEBUG);
log.debug("This is a debug message");
log.info("This is an info message");
}
}
Output:
DEBUG com.log.appender.Log4jPropr - This is a debug message
INFO com.log.appender.Log4jPropr - This is an info message
Type-2:
package com.log.appender;
import java.io.IOException;
import org.apache.log4j.DailyRollingFileAppender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
public class Log
{
public static Logger as_looger;
public static void main(String[] args) {
as_looger = Logger.getLogger("articles logging");
as_looger = Logger.getLogger(Log.class.getName());
FileAppender as_appender = null;
try {
as_appender = new DailyRollingFileAppender(new PatternLayout("%d{dd-MM-yyyy HH:mm:ss} %C %L %-5p: %m%n"), "path of file where to write logs","'.'dd-MM-yyyy");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
as_looger.setLevel(Level.DEBUG);
as_looger.addAppender(as_appender);
}
}
No comments:
Post a Comment