<div dir="ltr">
<p class="" style><span class="">Hi,</span></p><p class="" style><span class="">I am building a simple web application that can be used to preview the pattern layout of log files. The application aims to give you an idea of what your log files will look like if you use a pattern layout.</span></p>
<p class="" style><span class="">Here you can see it working for the Log4j PatternLayout (still in progress):</span></p><p class="" style><span class=""><a href="http://log-preview.appspot.com/">http://log-preview.appspot.com/</a><br>
</span></p><p class="" style>Feel free to play around with, it is self explanatory.<br></p><p class="" style>I want to add support for Logback as well. </p><p class="" style>The idea is that I have a simple API, that is a servlet that receives a pattern as a parameter. I then create an appender with that pattern layout and attach it to the loggers of some classes. The appender actually appends to the response stream, so the logs are actually the response of the API.</p>
<p class="" style>So, as I used a WriterAppender for log4j, I suppose an OutputStreamAppender is the equivalent for Logback.</p><p class="" style>Unfortunately, I have not managed to append anything to the result stream. Could you see what is wring with my code? It was dead simple with Log4j, but I am still getting trouble here.</p>
<p class="" style>Thanks!<br></p><p class="" style><br></p><p class="" style>P.S. This is stripped down version that I think should be printing a log in the response</p><p class="" style><span class=""><br></span></p><p class="">
<span class=""><br></span></p><p class=""><span class="">package</span> com.markos.logpreview;</p>
<p class=""><span class="">import</span> java.io.IOException;<br></p>
<p class=""><span class="">import</span> javax.servlet.http.HttpServlet;<br></p>
<p class=""><span class="">import</span> javax.servlet.http.HttpServletRequest;</p>
<p class=""><span class="">import</span> javax.servlet.http.HttpServletResponse;</p>
<p class=""><span class="">import</span> org.slf4j.LoggerFactory;<br></p>
<p class=""><span class="">import</span> ch.qos.logback.classic.Logger;<br></p>
<p class=""><span class="">import</span> ch.qos.logback.classic.LoggerContext;</p>
<p class=""><span class="">import</span> ch.qos.logback.classic.PatternLayout;</p>
<p class=""><span class="">import</span> ch.qos.logback.classic.spi.ILoggingEvent;</p>
<p class=""><span class="">import</span> ch.qos.logback.core.OutputStreamAppender;</p>
<p class=""><br></p>
<p class="">@SuppressWarnings<span class="">(</span><span class="">"serial"</span><span class="">)</span></p>
<p class=""><span class="">public</span> <span class="">class</span> Log_previewServlet <span class="">extends</span> HttpServlet {</p>
<p class=""><br></p>
<p class=""><span class=""><span class=""> </span></span>@Override</p>
<p class=""><span class=""> </span><span class="">public</span> <span class="">void</span> doGet(HttpServletRequest req, HttpServletResponse resp) <span class="">throws</span> IOException {</p>
<p class=""><br></p>
<p class=""><span class=""> </span><span class=""> </span>resp.setContentType(<span class="">"text/plain"</span>);</p>
<p class=""><br></p>
<p class=""><span class=""> </span><span class=""> </span>LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();</p>
<p class=""><span class=""> </span><span class=""> </span>loggerContext.reset();</p>
<p class=""><span class=""> </span><span class=""> </span>OutputStreamAppender<ILoggingEvent> outputStreamAppender = <span class="">new</span> OutputStreamAppender<ILoggingEvent>();</p>
<p class=""><span class=""> </span><span class=""> </span>outputStreamAppender.setContext(loggerContext);</p>
<p class=""><span class=""> </span><span class=""> </span>outputStreamAppender.setOutputStream(resp.getOutputStream());</p>
<p class=""><br></p>
<p class=""><span class=""> </span><span class=""> </span>PatternLayout pl = <span class="">new</span> PatternLayout();</p>
<p class=""><span class=""><span class=""> </span><span class=""> </span>pl.setPattern(</span>"%d %5p %t [%c:%L] %m%n)"<span class="">);</span></p>
<p class=""><span class=""> </span><span class=""> </span>pl.setContext(loggerContext);</p>
<p class=""><span class=""> </span><span class=""> </span>pl.start();</p>
<p class=""><span class=""> </span><span class=""> </span>outputStreamAppender.setLayout(pl);</p>
<p class=""><span class=""> </span><span class=""> </span>outputStreamAppender.start();</p>
<p class=""><br></p>
<p class=""><span class=""> </span><span class=""> </span>Logger logbackLogger = loggerContext.getLogger(Logger.<span class="">ROOT_LOGGER_NAME</span>);</p>
<p class=""><span class=""> </span><span class=""> </span>logbackLogger.addAppender(outputStreamAppender);</p>
<p class=""><b><span class=""><span class=""> </span><span class=""> </span>logbackLogger.error(</span>"I should show up in the response!"<span class="">);</span></b></p>
<p class=""><br></p>
<p class=""><span class=""> </span>}</p>
<p class="">}</p><div><br></div>-- <br>Sent from my iPhone
</div>