java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -
i wish write custom appender on basis of outputstreamappender
. wrote following class
package tests; import java.io.ioexception; import java.io.outputstream; import ch.qos.logback.core.outputstreamappender; public class myappender<e> extends outputstreamappender<e> { public myappender() { system.out.println("myappender created"); setoutputstream(new outputstream() { @override public void write(int b) throws ioexception { throw new unsupportedoperationexception(); } }); } }
then wrote following runner:
package tests; import org.slf4j.loggerfactory; import ch.qos.logback.classic.logger; public class runner { private static final logger log = (logger) loggerfactory.getlogger(runner.class); public static void main(string[] args) { //log.addappender(new myappender<iloggingevent>()); log.info("hello world"); } }
finally wrote following logback.xml
:
<configuration> <appender name="stdout" class="ch.qos.logback.core.consoleappender"> <encoder> <pattern>%d{hh:mm:ss.sss} [%thread] %-5level %logger{0} - %msg%n</pattern> </encoder> </appender> <appender name="myappender" class="tests.myappender"> <encoder> <pattern>%d{hh:mm:ss.sss} [%thread] %-5level %logger{0} - %msg%n</pattern> </encoder> </appender> <root level="debug"> <appender-ref ref="stdout" /> </root> </configuration>
and got following output:
myappender created logback: no context given tests.myappender[null] 20:45:29.957 [main] info runner - hello world
it indicates appender instantiated. absence of exceptions mean never called.
probably related error message don't understand: "logback: no context given tests.myappender[null]"
what mean , how fix?
how set filename want write there lots of other issues when extend outputstreamappender
rather extend fileappender(which intern extension of outputsteamappender) , override write method perfrom custom task.
public class myappender<e> extends fileappender<e> { @override protected void writeout(e event) throws ioexception { /**if (prudent) { safewrite(event); } else { super.writeout(event); } */ throw new unsupportedoperationexception(); } }
Comments
Post a Comment