[logback-user] [ConfigurationDelegate.groovy] LogstashTcpSocketAppender

Daniel Pacak pacak.daniel at gmail.com
Thu Mar 31 19:08:35 UTC 2016


Works as a charm. Thank you Ceki for detailed explanation and pointing the
XML to Groovy translator out!

On Thu, Mar 24, 2016 at 11:46 AM, Ceki Gulcu <ceki at qos.ch> wrote:

>
> Have you tried the logback.xml to logback.groovy translator? It is
> available at
>
>   http://logback.qos.ch/translator/asGroovy.html
>
> Anyway, the logback.xml you provided translates into the following
> logback.groovy file:
>
> import ch.qos.logback.core.ConsoleAppender
> import ch.qos.logback.core.spi.LifeCycle
> import net.logstash.logback.appender.LogstashTcpSocketAppender
> import net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder
>
> import static ch.qos.logback.classic.Level.ALL
>
> appender("console", ConsoleAppender)
> appender("tcp", LogstashTcpSocketAppender) {
>   destination = "localhost:4560"
>   encoder(LoggingEventCompositeJsonEncoder) {
>     // You MUST add an import declaration as appropriate for [Providers]
>     Providers... aProviders = new Providers()
>     aProviders.message =
>     aProviders.mdc =
>     aProviders.arguments =
>     if(aProviders instanceof LifeCycle)
>       aProviders.start()
>     providers = aProviders
>   }
> }
> root(ALL, ["console", "tcp"])
>
> You can fill in the missing statements without too much trouble.
>
> Providers _providers = new LoggingEventCompositeJsonEncoder()
> _providers.message = new MessageJsonProvider();
> _providers.mdc = new MdcJsonProvider();
> _providers.arguments = new ArgumentsJsonProvider();
> if(_providers instanceof LifeCycle)
> _providers .start()
> providers = _providers
>
> you would need to add the following imports for the above to work.
>
> 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.
>
> To cut a long story short, the more verbose but equivalent version of the
> logback.xml would be written as
>
> <configuration>
>
>     <shutdownHook class="ch.qos.logback.core.hook.DelayingShutdownHook"/>
>
>     <appender name="console" class="ch.qos.logback.core.ConsoleAppender"/>
>
>     <appender name="tcp"
> class="net.logstash.logback.appender.LogstashTcpSocketAppender">
>         <destination>localhost:4560</destination>
>         <encoder
> class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
>             <providers
> class="net.logstash.logback.composite.loggingevent.LoggingEventJsonProviders">
>                 <message
> class="net.logstash.logback.composite.loggingevent.MessageJsonProvider"/>
>                 <mdc
> class="net.logstash.logback.composite.loggingevent.MdcJsonProvider"/>
>                 <arguments
> class="net.logstash.logback.composite.loggingevent.ArgumentsJsonProvider"/>
>             </providers>
>         </encoder>
>     </appender>
>
>     <root level="all">
>         <appender-ref ref="console"/>
>         <appender-ref ref="tcp"/>
>     </root>
>
> </configuration>
>
> When translated with the XML to Groovy translator, the results is
>
>
> import ch.qos.logback.core.ConsoleAppender
> import net.logstash.logback.appender.LogstashTcpSocketAppender
> import net.logstash.logback.composite.loggingevent.ArgumentsJsonProvider
> import
> net.logstash.logback.composite.loggingevent.LoggingEventJsonProviders
> import net.logstash.logback.composite.loggingevent.MdcJsonProvider
> import net.logstash.logback.composite.loggingevent.MessageJsonProvider
> import net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder
>
> import static ch.qos.logback.classic.Level.ALL
>
> appender("console", ConsoleAppender)
> appender("tcp", LogstashTcpSocketAppender) {
>   destination = "localhost:4560"
>   encoder(LoggingEventCompositeJsonEncoder) {
>     providers(LoggingEventJsonProviders) {
>       message(MessageJsonProvider)
>       mdc(MdcJsonProvider)
>       arguments(ArgumentsJsonProvider)
>     }
>   }
> }
> root(ALL, ["console", "tcp"])
>
>
> Both groovy scripts should work.
>
> I hope this helps,
>
> --
> Ceki
>
>
>
> On 3/24/2016 10:54, Daniel Pacak wrote:
>
>> Hi,
>>
>> In a Grails app for my customer I'm using the LogstashTcpSocketAppender
>> provided by this project
>> https://github.com/logstash/logstash-logback-encoder.
>>
>> It works perfectly fine with the logback.xml configuration which looks
>> as follows:
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <configuration>
>>
>> <shutdownHook class="ch.qos.logback.core.hook.DelayingShutdownHook"/>
>>
>> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"/>
>>
>> <appender name="tcp"
>> class="net.logstash.logback.appender.LogstashTcpSocketAppender">
>> <destination>localhost:4560</destination>
>> <encoder
>> class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
>> <providers>
>> <message/>
>> <mdc/>
>> <arguments/>
>> </providers>
>> </encoder>
>> </appender>
>>
>> <root level="all">
>> <appender-ref ref="console"/>
>> <appender-ref ref="tcp"/>
>> </root>
>>
>> </configuration>
>>
>>
>> However I had hard time configuring the same in logback.groovy; Do you
>> have any idea how to configure LogstashTcpSocketAppender in groovy
>> style? So far I came up with the following snippet which sucks a bit
>> because it depends on Logback internals (mixed in ConfigurationDelegate
>> properties/methods):
>>
>> import ch.qos.logback.core.Appender
>> import net.logstash.logback.appender.LogstashTcpSocketAppender
>> import net.logstash.logback.composite.JsonProviders
>> import net.logstash.logback.composite.loggingevent.ArgumentsJsonProvider
>> import net.logstash.logback.composite.loggingevent.MdcJsonProvider
>> import net.logstash.logback.composite.loggingevent.MessageJsonProvider
>> import net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder
>>
>> appender('STDOUT',ConsoleAppender) {
>>      encoder(PatternLayoutEncoder) {
>>          pattern ='%-4relative [%thread] %msg%n'
>> }
>> }
>>
>> addAppender('LOGSTASH_TCP',
>> createLogstashTcpSocketAppender('localhost:4560'))
>>
>> root(DEBUG, ['STDOUT','LOGSTASH_TCP'])
>>
>> printlnappenderList
>>
>> Appender createLogstashTcpSocketAppender(String destination) {
>>      LoggingEventCompositeJsonEncoder encoder =new
>> LoggingEventCompositeJsonEncoder()
>>      JsonProviders providers =new JsonProviders()
>>      providers.addProvider(new MessageJsonProvider())
>>      providers.addProvider(new MdcJsonProvider())
>>      providers.addProvider(new ArgumentsJsonProvider())
>>      encoder.setProviders(providers)
>>
>>      Appender appender =new LogstashTcpSocketAppender()
>>      appender.addDestination(destination)
>>      appender.setEncoder(encoder)
>>      appender
>> }
>>
>> void addAppender(String name, Appender appender) {
>>      appender.name = name
>>      appender.context =context
>>
>> appenderList.add(appender)
>>      appender.start()
>> }
>>
>>
>> Regards,
>> Daniel Pacak
>>
>>
>> _______________________________________________
>> logback-user mailing list
>> logback-user at qos.ch
>> http://mailman.qos.ch/mailman/listinfo/logback-user
>>
>> _______________________________________________
> logback-user mailing list
> logback-user at qos.ch
> http://mailman.qos.ch/mailman/listinfo/logback-user
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qos.ch/pipermail/logback-user/attachments/20160331/11ca6121/attachment-0001.html>


More information about the logback-user mailing list