<div dir="ltr">Works as a charm. Thank you Ceki for detailed explanation and pointing the XML to Groovy translator out!</div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Mar 24, 2016 at 11:46 AM, Ceki Gulcu <span dir="ltr"><<a href="mailto:ceki@qos.ch" target="_blank">ceki@qos.ch</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
Have you tried the logback.xml to logback.groovy translator? It is available at<br>
<br>
  <a href="http://logback.qos.ch/translator/asGroovy.html" rel="noreferrer" target="_blank">http://logback.qos.ch/translator/asGroovy.html</a><br>
<br>
Anyway, the logback.xml you provided translates into the following logback.groovy file:<br>
<br>
import ch.qos.logback.core.ConsoleAppender<br>
import ch.qos.logback.core.spi.LifeCycle<br>
import net.logstash.logback.appender.LogstashTcpSocketAppender<br>
import net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder<br>
<br>
import static ch.qos.logback.classic.Level.ALL<br>
<br>
appender("console", ConsoleAppender)<br>
appender("tcp", LogstashTcpSocketAppender) {<br>
  destination = "localhost:4560"<br>
  encoder(LoggingEventCompositeJsonEncoder) {<br>
    // You MUST add an import declaration as appropriate for [Providers]<br>
    Providers... aProviders = new Providers()<br>
    aProviders.message =<br>
    aProviders.mdc =<br>
    aProviders.arguments =<br>
    if(aProviders instanceof LifeCycle)<br>
      aProviders.start()<br>
    providers = aProviders<br>
  }<br>
}<br>
root(ALL, ["console", "tcp"])<br>
<br>
You can fill in the missing statements without too much trouble.<br>
<br>
Providers _providers = new LoggingEventCompositeJsonEncoder()<br>
_providers.message = new MessageJsonProvider();<br>
_providers.mdc = new MdcJsonProvider();<br>
_providers.arguments = new ArgumentsJsonProvider();<br>
if(_providers instanceof LifeCycle)<br>
_providers .start()<br>
providers = _providers<br>
<br>
you would need to add the following imports for the above to work.<br>
<br>
It looks like logstash-logback-encoder makes extensive use of Joran's (logback's xml configurator) @DefaultClass annotation as well as its default component initialization mechanism.<br>
<br>
To cut a long story short, the more verbose but equivalent version of the logback.xml would be written as<span class=""><br>
<br>
<configuration><br>
<br>
    <shutdownHook class="ch.qos.logback.core.hook.DelayingShutdownHook"/><br>
<br>
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender"/><br>
<br>
    <appender name="tcp" class="net.logstash.logback.appender.LogstashTcpSocketAppender"><br>
        <destination>localhost:4560</destination><br>
        <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder"><br></span>
            <providers class="net.logstash.logback.composite.loggingevent.LoggingEventJsonProviders"><br>
                <message class="net.logstash.logback.composite.loggingevent.MessageJsonProvider"/><br>
                <mdc class="net.logstash.logback.composite.loggingevent.MdcJsonProvider"/><br>
                <arguments class="net.logstash.logback.composite.loggingevent.ArgumentsJsonProvider"/><span class=""><br>
            </providers><br>
        </encoder><br>
    </appender><br>
<br>
    <root level="all"><br>
        <appender-ref ref="console"/><br>
        <appender-ref ref="tcp"/><br>
    </root><br>
<br>
</configuration><br>
<br></span>
When translated with the XML to Groovy translator, the results is<br>
<br>
<br>
import ch.qos.logback.core.ConsoleAppender<br>
import net.logstash.logback.appender.LogstashTcpSocketAppender<br>
import net.logstash.logback.composite.loggingevent.ArgumentsJsonProvider<br>
import net.logstash.logback.composite.loggingevent.LoggingEventJsonProviders<span class=""><br>
import net.logstash.logback.composite.loggingevent.MdcJsonProvider<br>
import net.logstash.logback.composite.loggingevent.MessageJsonProvider<br>
import net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder<br>
<br></span>
import static ch.qos.logback.classic.Level.ALL<br>
<br>
appender("console", ConsoleAppender)<br>
appender("tcp", LogstashTcpSocketAppender) {<br>
  destination = "localhost:4560"<br>
  encoder(LoggingEventCompositeJsonEncoder) {<br>
    providers(LoggingEventJsonProviders) {<br>
      message(MessageJsonProvider)<br>
      mdc(MdcJsonProvider)<br>
      arguments(ArgumentsJsonProvider)<br>
    }<br>
  }<br>
}<br>
root(ALL, ["console", "tcp"])<br>
<br>
<br>
Both groovy scripts should work.<br>
<br>
I hope this helps,<br>
<br>
--<br>
Ceki<div><div class="h5"><br>
<br>
<br>
On 3/24/2016 10:54, Daniel Pacak wrote:<br>
</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5">
Hi,<br>
<br>
In a Grails app for my customer I'm using the LogstashTcpSocketAppender<br>
provided by this project<br>
<a href="https://github.com/logstash/logstash-logback-encoder" rel="noreferrer" target="_blank">https://github.com/logstash/logstash-logback-encoder</a>.<br>
<br>
It works perfectly fine with the logback.xml configuration which looks<br>
as follows:<br>
<br>
<?xml version="1.0" encoding="UTF-8"?><br>
<configuration><br>
<br>
<shutdownHook class="ch.qos.logback.core.hook.DelayingShutdownHook"/><br>
<br>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender"/><br>
<br>
<appender name="tcp"<br>
class="net.logstash.logback.appender.LogstashTcpSocketAppender"><br>
<destination>localhost:4560</destination><br>
<encoder<br>
class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder"><br>
<providers><br>
<message/><br>
<mdc/><br>
<arguments/><br>
</providers><br>
</encoder><br>
</appender><br>
<br>
<root level="all"><br>
<appender-ref ref="console"/><br>
<appender-ref ref="tcp"/><br>
</root><br>
<br>
</configuration><br>
<br>
<br>
However I had hard time configuring the same in logback.groovy; Do you<br>
have any idea how to configure LogstashTcpSocketAppender in groovy<br>
style? So far I came up with the following snippet which sucks a bit<br>
because it depends on Logback internals (mixed in ConfigurationDelegate<br>
properties/methods):<br>
<br>
import ch.qos.logback.core.Appender<br>
import net.logstash.logback.appender.LogstashTcpSocketAppender<br>
import net.logstash.logback.composite.JsonProviders<br>
import net.logstash.logback.composite.loggingevent.ArgumentsJsonProvider<br>
import net.logstash.logback.composite.loggingevent.MdcJsonProvider<br>
import net.logstash.logback.composite.loggingevent.MessageJsonProvider<br>
import net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder<br>
<br>
appender('STDOUT',ConsoleAppender) {<br>
     encoder(PatternLayoutEncoder) {<br>
         pattern ='%-4relative [%thread] %msg%n'<br>
}<br>
}<br>
<br>
addAppender('LOGSTASH_TCP', createLogstashTcpSocketAppender('localhost:4560'))<br>
<br>
root(DEBUG, ['STDOUT','LOGSTASH_TCP'])<br>
<br>
printlnappenderList<br>
<br>
Appender createLogstashTcpSocketAppender(String destination) {<br>
     LoggingEventCompositeJsonEncoder encoder =new LoggingEventCompositeJsonEncoder()<br></div></div>
     JsonProviders providers =new JsonProviders()<span class=""><br>
     providers.addProvider(new MessageJsonProvider())<br>
     providers.addProvider(new MdcJsonProvider())<br>
     providers.addProvider(new ArgumentsJsonProvider())<br>
     encoder.setProviders(providers)<br>
<br></span>
     Appender appender =new LogstashTcpSocketAppender()<span class=""><br>
     appender.addDestination(destination)<br>
     appender.setEncoder(encoder)<br>
     appender<br>
}<br>
<br>
void addAppender(String name, Appender appender) {<br>
     <a href="http://appender.name" rel="noreferrer" target="_blank">appender.name</a> = name<br>
     appender.context =context<br>
<br>
appenderList.add(appender)<br>
     appender.start()<br>
}<br>
<br>
<br>
Regards,<br>
Daniel Pacak<br>
<br>
<br></span>
_______________________________________________<br>
logback-user mailing list<br>
<a href="mailto:logback-user@qos.ch" target="_blank">logback-user@qos.ch</a><br>
<a href="http://mailman.qos.ch/mailman/listinfo/logback-user" rel="noreferrer" target="_blank">http://mailman.qos.ch/mailman/listinfo/logback-user</a><br>
<br>
</blockquote>
_______________________________________________<br>
logback-user mailing list<br>
<a href="mailto:logback-user@qos.ch" target="_blank">logback-user@qos.ch</a><br>
<a href="http://mailman.qos.ch/mailman/listinfo/logback-user" rel="noreferrer" target="_blank">http://mailman.qos.ch/mailman/listinfo/logback-user</a></blockquote></div><br></div>