java - log4j - no appenders could be found, custom config file -



java - log4j - no appenders could be found, custom config file -

i'm using log4j in project first time, , i'm trying utilize custom configuration log output. want logger configuration in custom file, not log4j.xml or log4j.properties. have right now:

constructor:

public manager(int managerid, string loggerconfigfile) { this.manager_id = managerid; logger = logger.getlogger("testlogger" + manager_id); propertyconfigurator.configure(loggerconfigfile); }

method calls logger first time:

public void getpacketsfromstream(inputstream inputstream) { logger.info("manager " + manager_id + " started.");

(there's more later, that's not important)

contents of test1.config (that's value of loggerconfigfile in constructor)

testlogger1=debug,a1 log4j.appender.a1=org.apache.log4j.consoleappender log4j.appender.a1.layout=org.apache.log4j.patternlayout log4j.appender.a1.layout.conversionpattern=%-4r %-5p [%t] %37c %3x - %m%n

i've checked, , config file is in classpath.

i'd expect result in logger writing 1 statement console. instead, (with -dlog4j.debug flag used) next output:

log4j: trying find [log4j.xml] using context classloader sun.misc.launcher$appclassloader@1f12c4e. log4j: trying find [log4j.xml] using sun.misc.launcher$appclassloader@1f12c4e class loader. log4j: trying find [log4j.xml] using classloader.getsystemresource(). log4j: trying find [log4j.properties] using context classloader sun.misc.launcher$appclassloader@1f12c4e. log4j: trying find [log4j.properties] using sun.misc.launcher$appclassloader@1f12c4e class loader. log4j: trying find [log4j.properties] using classloader.getsystemresource(). log4j: not find resource: [null]. log4j: not find root logger information. ok? log4j: finished configuring. log4j:warn no appenders found logger (testlogger1). log4j:warn please initialize log4j scheme properly. log4j:warn see http://logging.apache.org/log4j/1.2/faq.html#noconfig more info.

what doing wrong?

edit:

i did first reply suggests - moved log4j configuration before , added rootlogger configuration file log4j.rootlogger=debug,a1, , got expected output. tried modify config file bit more, , ended this:

log4j.rootlogger=debug,a0 testlogger1=debug,a1 log4j.appender.a0=org.apache.log4j.consoleappender log4j.appender.a1=org.apache.log4j.consoleappender log4j.appender.a0.layout=org.apache.log4j.patternlayout log4j.appender.a1.layout=org.apache.log4j.patternlayout log4j.appender.a0.layout.conversionpattern=%-4r %-5p [%t] %37c %3x --- %m%n log4j.appender.a1.layout.conversionpattern=%-4r %-5p [%t] %37c %3x - %m%n

note a0 , a1 have different output formats. got in output:

log4j: trying find [log4j.xml] using context classloader sun.misc.launcher$appclassloader@1f12c4e. log4j: trying find [log4j.xml] using sun.misc.launcher$appclassloader@1f12c4e class loader. log4j: trying find [log4j.xml] using classloader.getsystemresource(). log4j: trying find [log4j.properties] using context classloader sun.misc.launcher$appclassloader@1f12c4e. log4j: trying find [log4j.properties] using sun.misc.launcher$appclassloader@1f12c4e class loader. log4j: trying find [log4j.properties] using classloader.getsystemresource(). log4j: not find resource: [null]. log4j: parsing [root] value=[debug,a0]. log4j: level token [debug]. log4j: category root set debug log4j: parsing appender named "a0". log4j: parsing layout options "a0". log4j: setting property [conversionpattern] [%-4r %-5p [%t] %37c %3x --- %m%n]. log4j: end of parsing "a0". log4j: parsed "a0" options. log4j: finished configuring. 0 info [main] testlogger1 --- manager 1 started.

how can log4j utilize testlogger1 , appender a1, rather rootlogger , a0? would've expected getlogger("testlogger1") that.

you should configure log4j before instantiating logger:

public manager(int managerid, string loggerconfigfile) { propertyconfigurator.configure(loggerconfigfile); this.manager_id = managerid; logger = logger.getlogger("testlogger" + manager_id); }

you should define root logger in configuration avoid "could not find root logger information"; example:

log4j.rootlogger=debug, a1

see http://logging.apache.org/log4j/1.2/manual.html more details. illustration utilize java environment variable specify file name/path contains log4j configuration (instead of doing in java code).

java log4j

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -