Bu uygulama da BasicConfigurator.configure(); metodunu kullandık ve böylece log4j.properties dosyasına gereksinimiz kalmadı.
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
/**
*
* @author K_Goksu --> http://www.kasimgoksu.com
*
*/
public class MySampleApp {
static Logger logger = Logger.getLogger(MySampleApp.class);
@SuppressWarnings("unchecked")
public static void main(String[] args) {
// load default configure, don't need log4j.properties file!
BasicConfigurator.configure();
// # Set root logger level to DEBUG and its only appender to A1.
// log4j.rootLogger=DEBUG, A1
//
// # A1 is set to be a ConsoleAppender.
// log4j.appender.A1=org.apache.log4j.ConsoleAppender
//
// # A1 uses PatternLayout.
// log4j.appender.A1.layout=org.apache.log4j.PatternLayout
// log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
logger.info("### Log4j sample :))) ###\n");
Priority[] allPossiblePriorities = Level.getAllPossiblePriorities();
for (int i = 0; i < allPossiblePriorities.length; i++) {
//TRACE < DEBUG < INFO < WARN < ERROR < FATAL
logger.setLevel((Level) allPossiblePriorities[i]);
System.out.println("### Level priorty is now " + allPossiblePriorities[i]);
logger.debug("DEBUG");
logger.info("INFO");
logger.warn("WARN");
logger.error("ERROR");
logger.fatal("FATAL");
System.out.println("\n\n");
}
}
}
Kaynak koda buradan ulaşabilirsiniz.