[logback-user] Cant able to appy <pattern> and <Layout> in <encoder> of logback.xml

goto meet127 gotomeet127 at gmail.com
Sun Dec 28 07:43:10 CET 2014


I am trying to create custom layout in Logback, using the link,

http://stackoverflow.com/questions/14168684/creating-a-custom-layout-in-logback

and i am successful in creating log file.

The problem is when I apply "pattern" tag in encoder tag of logback.xml
like this,

Logback.xml

==========

<appender name="appLogFileAppender"
class="ch.qos.logback.core.rolling.RollingFileAppender"><rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>C:/tmp.log</fileNamePattern>
        </rollingPolicy>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
             <pattern> %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36}
- %msg%n </pattern>
             <layout class="com.dces.util.LoggingConsoleLayout" />
        </encoder>
    </appender>

it is not applied on log file, which is created, and I am getting content
without date&time AS BELOW.

*--** [ERROR]main.java.com.srccodes.log.LogbackHello - Welcome to the
HelloWorld example of Logback. *

*-- **[ERROR]main.java.com.srccodes.log.LogbackHello - Dummy error message.*

Actually if I remove
class="ch.qos.logback.core.encoder.LayoutWrappingEncoder" in encoder tag
,pattern is working, but then my custom layout is not working.

I want to apply *pattern* tag as well as custom *layout* tag on encoder
tag.Is it is possible?


LoggingConsoleLayout.xml - My Cuatom Layout

=====================================

public class LoggingConsoleLayout extends LayoutBase<ILoggingEvent>{

@Override

public String doLayout(ILoggingEvent event) {

StringBuffer sbuf = null;

try

{

sbuf = new StringBuffer(128);

sbuf.append("-- ");

sbuf.append("[");

sbuf.append(event.getLevel());

sbuf.append("]");

sbuf.append(event.getLoggerName());

sbuf.append(" - ");

*sbuf.append(encodeData(event.getFormattedMessage()));*

*//here i am encoding my log messages and which is important for me*

sbuf.append(CoreConstants.LINE_SEPARATOR);

}

catch(Exception e)

{

e.printStackTrace();

}

return sbuf.toString();

}

}

when I tried this,


<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">

 <layout class="com.dces.util.LoggingConsoleLayout" >

     <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} -
%msg%n</Pattern>

   </layout>

</encoder>


I got *ERROR in ch.qos.logback.core.joran.spi.Interpreter at 17:17 - no
applicable action for [Pattern], current ElementPath  is
[[configuration][appender][encoder][layout][Pattern]]*

And my pattern is not applied to my layout...

Is there is any possibility to have both layout and pattern tags in encoder
tag?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qos.ch/pipermail/logback-user/attachments/20141228/0c73806f/attachment-0001.html>


More information about the Logback-user mailing list