Kako koristiti Log4J, primer

Započeo maxogm, 26.12.2010, 23:14

prethodna tema - sledeća tema

maxogm

Napraviti log4j.properties fajl.

log4j.properties

###################### ROOT LOGGER DEFINITION ########################
log4j.rootLogger = TRACE, CONSOLE, LOGFILE

###################### LOGFILE APPENDER DEFINITION ###################
log4j.appender.LOGFILE = org.apache.log4j.RollingFileAppender
log4j.appender.LOGFILE.Threshold = DEBUG
log4j.appender.LOGFILE.Append = true
log4j.appender.LOGFILE.MaxFileSize = 3000KB
log4j.appender.LOGFILE.MaxBackupIndex = 100
log4j.appender.LOGFILE.layout = org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern = %d %-5p [%c] - %m%n
log4j.appender.LOGFILE.File = ${user.home}/.jmax/log/jmax.log

###################### CONSOLE APPENDER DEFINITION ###################
log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold = INFO
log4j.appender.CONSOLE.Target = System.out
log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern = %d{ABSOLUTE} %x %-5p [%c{1}] %m%n



Test class:

package rs.in.jmax.log4j;

import org.apache.log4j.Logger;

public class Test {

private static final Logger LOGGER = Logger.getLogger(Test.class);

public static void main(String[] args) {
//nece biti na konzoli ni u fajlu
LOGGER.trace("TRACE level");
//bice logovano u fajl
LOGGER.debug("DEBUG level");

//logovace se i u fajl i na konzolu
LOGGER.info("INFO level");
LOGGER.warn("WARN level");
LOGGER.error("ERROR level");
LOGGER.fatal("FATAL level");
}
}



U krajnjem produktu potrebno je samo da u log4j.properties fajlu povisite nivo logovanja... i nece biti vise vasih log-ova.

Podrzani nivoi logovanja su:

trace
debug
info
warn
error
fatal