[logback-user] [ConfigurationDelegate.groovy] LogstashTcpSocketAppender
Daniel Pacak
pacak.daniel at gmail.com
Thu Mar 24 09:54:57 UTC 2016
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'])
println appenderList
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qos.ch/pipermail/logback-user/attachments/20160324/be74953a/attachment.html>
More information about the logback-user
mailing list