me.moocar.logbackgelf
Class GelfAppender

java.lang.Object
  extended by ch.qos.logback.core.spi.ContextAwareBase
      extended by ch.qos.logback.core.AppenderBase<ch.qos.logback.classic.spi.ILoggingEvent>
          extended by me.moocar.logbackgelf.GelfAppender
All Implemented Interfaces:
ch.qos.logback.core.Appender<ch.qos.logback.classic.spi.ILoggingEvent>, ch.qos.logback.core.spi.ContextAware, ch.qos.logback.core.spi.FilterAttachable<ch.qos.logback.classic.spi.ILoggingEvent>, ch.qos.logback.core.spi.LifeCycle

public class GelfAppender
extends ch.qos.logback.core.AppenderBase<ch.qos.logback.classic.spi.ILoggingEvent>

Responsible for Formatting a log event and sending it to a Graylog2 Server. Note that you can't swap in a different Layout since the GELF format is static.


Field Summary
 
Fields inherited from class ch.qos.logback.core.AppenderBase
name, started
 
Fields inherited from class ch.qos.logback.core.spi.ContextAwareBase
context
 
Constructor Summary
GelfAppender()
           
 
Method Summary
 void addAdditionalField(String keyValue)
          Add an additional field.
 void addStaticAdditionalField(String keyValue)
          Add a staticAdditional field.
protected  void append(ch.qos.logback.classic.spi.ILoggingEvent logEvent)
          The main append method.
 Map<String,String> getAdditionalFields()
          additional fields to add to the gelf message.
 int getChunkThreshold()
           
 String getFacility()
          The name of your service.
 String getGraylog2ServerHost()
          The hostname of the graylog2 server to send messages to
 int getGraylog2ServerPort()
          The port of the graylog2 server to send messages to
 String getGraylog2ServerVersion()
           
 String getHostName()
          Override the local hostname using a config option
 String getMessagePattern()
           
 int getShortMessageLength()
          The length of the message to truncate to
 String getShortMessagePattern()
           
 Map<String,String> getStaticAdditionalFields()
          static additional fields to add to every gelf message.
 boolean isIncludeFullMDC()
          Indicates if all values from the MDC should be included in the gelf message or only the once listed as additional fields.
 boolean isUseLoggerName()
          If true, an additional field call "_loggerName" will be added to each gelf message.
 boolean isUseMarker()
           
 boolean isUseThreadName()
          If true, an additional field call "_threadName" will be added to each gelf message.
 void setAdditionalFields(Map<String,String> additionalFields)
           
 void setChunkThreshold(int chunkThreshold)
           
 void setFacility(String facility)
           
 void setGraylog2ServerHost(String graylog2ServerHost)
           
 void setGraylog2ServerPort(int graylog2ServerPort)
           
 void setGraylog2ServerVersion(String graylog2ServerVersion)
           
 void setHostName(String hostName)
           
 void setIncludeFullMDC(boolean includeFullMDC)
           
 void setMessagePattern(String messagePattern)
           
 void setShortMessageLength(int shortMessageLength)
           
 void setShortMessagePattern(String shortMessagePattern)
           
 void setStaticAdditionalFields(Map<String,String> staticAdditionalFields)
           
 void setUseLoggerName(boolean useLoggerName)
           
 void setUseMarker(boolean useMarker)
           
 void setUseThreadName(boolean useThreadName)
           
 void start()
           
 
Methods inherited from class ch.qos.logback.core.AppenderBase
addFilter, clearAllFilters, doAppend, getCopyOfAttachedFiltersList, getFilterChainDecision, getName, isStarted, setName, stop, toString
 
Methods inherited from class ch.qos.logback.core.spi.ContextAwareBase
addError, addError, addInfo, addInfo, addStatus, addWarn, addWarn, getContext, getDeclaredOrigin, getStatusManager, setContext
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface ch.qos.logback.core.spi.ContextAware
addError, addError, addInfo, addInfo, addStatus, addWarn, addWarn, getContext, setContext
 

Constructor Detail

GelfAppender

public GelfAppender()
Method Detail

append

protected void append(ch.qos.logback.classic.spi.ILoggingEvent logEvent)
The main append method. Takes the event that is being logged, formats if for GELF and then sends it over the wire to the log server

Specified by:
append in class ch.qos.logback.core.AppenderBase<ch.qos.logback.classic.spi.ILoggingEvent>
Parameters:
logEvent - The event that we are logging

start

public void start()
Specified by:
start in interface ch.qos.logback.core.spi.LifeCycle
Overrides:
start in class ch.qos.logback.core.AppenderBase<ch.qos.logback.classic.spi.ILoggingEvent>

getFacility

public String getFacility()
The name of your service. Appears in facility column in graylog2-web-interface


setFacility

public void setFacility(String facility)

getGraylog2ServerHost

public String getGraylog2ServerHost()
The hostname of the graylog2 server to send messages to


setGraylog2ServerHost

public void setGraylog2ServerHost(String graylog2ServerHost)

getGraylog2ServerPort

public int getGraylog2ServerPort()
The port of the graylog2 server to send messages to


setGraylog2ServerPort

public void setGraylog2ServerPort(int graylog2ServerPort)

isUseLoggerName

public boolean isUseLoggerName()
If true, an additional field call "_loggerName" will be added to each gelf message. Its contents will be the fully qualified name of the logger. e.g: com.company.Thingo.


setUseLoggerName

public void setUseLoggerName(boolean useLoggerName)

isUseMarker

public boolean isUseMarker()

setUseMarker

public void setUseMarker(boolean useMarker)

isUseThreadName

public boolean isUseThreadName()
If true, an additional field call "_threadName" will be added to each gelf message. Its contents will be the Name of the thread. Defaults to "false".


setUseThreadName

public void setUseThreadName(boolean useThreadName)

getAdditionalFields

public Map<String,String> getAdditionalFields()
additional fields to add to the gelf message. Here's how these work:
Let's take an example. I want to log the client's ip address of every request that comes into my web server. To do this, I add the ipaddress to the slf4j MDC on each request as follows: ... MDC.put("ipAddress", "44.556.345.657"); ... Now, to include the ip address in the gelf message, i just add the following to my logback.groovy: appender("GELF", GelfAppender) { ... additionalFields = [identity:"_identity"] ... } in the additionalFields map, the key is the name of the MDC to look up. the value is the name that should be given to the key in the additional field in the gelf message.


setAdditionalFields

public void setAdditionalFields(Map<String,String> additionalFields)

getStaticAdditionalFields

public Map<String,String> getStaticAdditionalFields()
static additional fields to add to every gelf message. Key is the additional field key (and should thus begin with an underscore). The value is a static string.


setStaticAdditionalFields

public void setStaticAdditionalFields(Map<String,String> staticAdditionalFields)

isIncludeFullMDC

public boolean isIncludeFullMDC()
Indicates if all values from the MDC should be included in the gelf message or only the once listed as additional fields.

If true, the gelf message will contain all values available in the MDC. Each MDC key will be converted to a gelf custom field by adding an underscore prefix. If an entry exists in additional field it will be used instead.

If false, only the fields listed in additional field will be included in the message.

Returns:
the includeFullMDC

setIncludeFullMDC

public void setIncludeFullMDC(boolean includeFullMDC)

getHostName

public String getHostName()
Override the local hostname using a config option

Returns:
the local hostname (defaults to getLocalHost() if not overridden in config

setHostName

public void setHostName(String hostName)

addAdditionalField

public void addAdditionalField(String keyValue)
Add an additional field. This is mainly here for compatibility with logback.xml

Parameters:
keyValue - This must be in format key:value where key is the MDC key, and value is the GELF field name. e.g "ipAddress:_ip_address"

addStaticAdditionalField

public void addStaticAdditionalField(String keyValue)
Add a staticAdditional field. This is mainly here for compatibility with logback.xml

Parameters:
keyValue - This must be in format key:value where key is the additional field key, and value is a static string. e.g "_node_name:www013"

getShortMessageLength

public int getShortMessageLength()
The length of the message to truncate to


setShortMessageLength

public void setShortMessageLength(int shortMessageLength)

getGraylog2ServerVersion

public String getGraylog2ServerVersion()

setGraylog2ServerVersion

public void setGraylog2ServerVersion(String graylog2ServerVersion)

getChunkThreshold

public int getChunkThreshold()

setChunkThreshold

public void setChunkThreshold(int chunkThreshold)

getMessagePattern

public String getMessagePattern()

setMessagePattern

public void setMessagePattern(String messagePattern)

getShortMessagePattern

public String getShortMessagePattern()

setShortMessagePattern

public void setShortMessagePattern(String shortMessagePattern)


Copyright © 2011-2014. All Rights Reserved.