项目作者: rahul619anand

项目描述 :
A log appender for GELF formatting.
高级语言: Java
项目地址: git://github.com/rahul619anand/gelf-log-appender-dropwizard.git


logback-gelf

A Logback appender that encodes logs to
GELF and transports them
to Graylog servers.

Support for dropwizard logging.

Dependency information

Latest version:

Gradle

  1. compile "com.bornconfused:logback-gelf:0.6"

Maven

  1. <dependency>
  2. <groupId>com.bornconfused</groupId>
  3. <artifactId>logback-gelf</artifactId>
  4. <version>0.6</version>
  5. </dependency>

Features

  • Append via TCP or UDP (with chunking) to a remote graylog server
  • MDC k/v converted to fields
  • Fields may have types
  • Auto include logger_name
  • Auto include Markers
  • Auto include Thread name
  • Static fields (E.g facility)
  • Very Few dependencies (Logback and GSON)

Configuring Logback

The minimal possible logback.xml you can write is something like.

  1. <configuration>
  2. <appender name="GELF UDP APPENDER" class="com.bornconfused.logbackgelf.GelfUDPAppender">
  3. <encoder class="com.bornconfused.logbackgelf.GZIPEncoder">
  4. <layout class="com.bornconfused.logbackgelf.GelfLayout"></layout>
  5. </encoder>
  6. </appender>
  7. <root level="debug">
  8. <appender-ref ref="GELF UDP APPENDER" ></appender-ref>
  9. </root>
  10. </configuration>

A more complete example that shows how you would overwrite many
default values:

  1. <configuration>
  2. <!--Use TCP instead of UDP-->
  3. <appender name="GELF TCP APPENDER" class="com.bornconfused.logback.net.SocketEncoderAppender">
  4. <remoteHost>somehost.com</remoteHost>
  5. <port>12201</port>
  6. <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
  7. <layout class="com.bornconfused.logbackgelf.GelfLayout">
  8. <!--An example of overwriting the short message pattern-->
  9. <shortMessageLayout class="ch.qos.logback.classic.PatternLayout">
  10. <pattern>%ex{short}%.100m</pattern>
  11. </shortMessageLayout>
  12. <!-- Use HTML output of the full message. Yes, any layout can be used (please don't actually do this)-->
  13. <fullMessageLayout class="ch.qos.logback.classic.html.HTMLLayout">
  14. <pattern>%relative%thread%mdc%level%logger%msg</pattern>
  15. </fullMessageLayout>
  16. <useLoggerName>true</useLoggerName>
  17. <useThreadName>true</useThreadName>
  18. <useMarker>true</useMarker>
  19. <host>Test</host>
  20. <additionalField>ipAddress:_ip_address</additionalField>
  21. <additionalField>requestId:_request_id</additionalField>
  22. <includeFullMDC>true</includeFullMDC>
  23. <fieldType>requestId:long</fieldType>
  24. <!--Facility is not officially supported in GELF anymore, but you can use staticFields to do the same thing-->
  25. <staticField class="com.bornconfused.logbackgelf.Field">
  26. <key>_facility</key>
  27. <value>GELF</value>
  28. </staticField>
  29. </layout>
  30. </encoder>
  31. </appender>
  32. <root level="debug">
  33. <appender-ref ref="GELF TCP APPENDER" ></appender-ref>
  34. </root>
  35. </configuration>

GelfLayout

com.bornconfused.logbackgelf.GelfLayout

This is where most configuration resides, since it’s the part that
actually converts a log event into a GELF compatible JSON string.

  • useLoggerName: 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. Default:
    false
  • useThreadName: If true, an additional field call “_threadName”
    will be added to each gelf message. Its contents will be the name of
    the thread. Default: false
  • host The hostname of the host from which the log is being sent.
    Displayed under source on web interface. Default:
    getLocalHostName()
  • useMarker: If true, and the user has set a
    slf4j Marker on their
    log, then the marker.toString() will be added to the gelf message
    as the field “_marker”. Default: false
  • shortMessageLayout: The
    Layout used to create
    the gelf short_message field. Shows up in the message column of
    the log summary in the web interface. Default: "%ex{short}%.100m"
    (PatternLayout)
  • fullMessageLayout: The
    Layout used to create
    the gelf full_message field. Shows up in the message field of the
    log details in the web interface. Default: "%rEx%m"
    (PatternLayout)
  • additionalFields: See additional fields below. Default: empty
  • fieldType: See field type conversion below. Default: empty
    (fields sent as string)
  • staticFields: See static fields below. Note, now that facility
    is deprecated, use this to set a facility Default: empty
  • staticAdditionalFields: deprecated. Use staticFields. Default:
    empty
  • includeFullMDC: See additional fields below. Default: false

Transports

Both UDP and TCP transports are supported. UDP is the recommended
graylog transport.

UDP

UDP can be configured using the
com.bornconfused.logbackgelf.GelfUDPAppender appender. Once messages reach
a certain size, they will be chunked according to the
gelf spec. A maximum of
128 chunks can be sent per log. If the encoded log is bigger than
that, the log will be dropped. Assuming the default 512 max packet
size, this allows for 65536 bytes (64kb) total per log message
(unzipped).

  • remoteHost: The remote graylog server host to send log messages
    to (DNS or IP). Default: "localhost"
  • port: The remote graylog server port. Default: 12201
  • maxPacketSize: The maximum number of bytes per datagram packet.
    Once the limit is reached, packets will be chunked. Default: 512

GZIP

For UDP, you have the option of Gzipping the Gelf JSON before sending
over UDP. To do this, replace the
ch.qos.logback.core.encoder.LayoutWrappingEncoder encoder with the
com.bornconfused.logbackgelf.GZIPEncoder encoder. E.g

  1. <appender name="GELF UDP APPENDER" class="com.bornconfused.logbackgelf.GelfUDPAppender">
  2. <encoder class="com.bornconfused.logbackgelf.GZIPEncoder">
  3. <layout class="com.bornconfused.logbackgelf.GelfLayout"></layout>
  4. </encoder>
  5. </appender>

Remember, The GZIP encoder should NOT be used with TCP

TCP

TCP transport can be configured using the
com.bornconfused.logback.net.SocketEncoderAppender appender. Unfortunately,
the built in Logback Socket
Appender

doesn’t give you control of how logs are encoded before being sent
over TCP, which is why you have to use this appender. Note
that due to an unresolved Graylog
issue
, GZIP
is not supported when using TCP.

  1. <appender name="GELF TCP APPENDER" class="com.bornconfused.logback.net.SocketEncoderAppender">
  2. <port>12201</port>
  3. <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
  4. <layout class="com.bornconfused.logbackgelf.GelfLayout">
  5. ....
  6. </layout>
  7. </encoder>
  8. </appender>
  • remoteHost: The remote graylog server host to send log messages
    to (DNS or IP). Default: "localhost"
  • port: The remote graylog server port. Required.
  • maxPacketSize: The number of logs to keep in memory while the
    graylog server can’t be reached. Default: 128
  • acceptConnectionTimeout: Milliseconds to wait for a connection
    to be established to the server before failing. Default: 1000

Extra features

Additional Fields

Additional Fields are extra k/v pairs that can be added to the GELF
json, and thus searched as structured data using graylog. In the slf4j
world, MDC (Mapped Diagnostic
Context) is an excellent way of programmatically adding fields to your
GELF messages.

Let’s take an example of adding the ip address of the client to every
logged message. To do this we add the ip address as a key/value to the
MDC so that the information persists for the length of the request,
and then we inform logback-gelf to look out for this mapping every
time a message is logged.

  1. Store IP address in MDC
  1. // Somewhere in server code that wraps every request
  2. ...
  3. org.slf4j.MDC.put("ipAddress", getClientIpAddress());
  4. ...
  1. Inform logback-gelf of MDC mapping
  1. <layout class="com.bornconfused.logbackgelf.GelfLayout">
  2. <additionalField>ipAddress:_ip_address</additionalField>
  3. </layout>

If the property includeFullMDC is set to true, all fields from the
MDC will be added to the gelf message. Any key, which is not listed as
additionalField will be prefixed with an underscore. Otherwise the
field name will be obtained from the corresponding additionalField
mapping.

If the property includeFullMDC is set to false (default value) then
only the keys listed as additionalField will be added to a gelf
message.

Static Fields

Use static additional fields when you want to add a static key value
pair to every GELF message. Key is the additional field key (and
should thus begin with an underscore). The value is a static string.

Now that the GELF facility is deprecated, this is how you add a
static facility. StaticFields replace staticAdditionalFields

E.g in the appender configuration:

  1. <layout class="com.bornconfused.logbackgelf.GelfLayout">
  2. <staticField class="com.bornconfused.logbackgelf.Field">
  3. <key>_facility</key>
  4. <value>GELF</value>
  5. </staticField>
  6. <staticField class="com.bornconfused.logbackgelf.Field">
  7. <key>_node_name</key>
  8. <value>www013</value>
  9. </staticField>
  10. </layout>

Static Additional Fields (deprecated)

Static Additional fields have been deprecated and superceded by
staticFields. While they offered a more concise way of expressing the
key/value pair, it was impossible to include a colon in the value.
staticFields are fully structured and don’t have this problem.

Field type conversion

You can configure a specific field to be converted to a numeric type.
Key is the additional field key as inserted into the MDC, value is the
type to convert to. Currently supported types are int, long, float and double.

  1. <layout class="com.bornconfused.logbackgelf.GelfLayout">
  2. <additionalField>requestId:_request_id</additionalField>
  3. <fieldType>requestId:long</fieldType>
  4. </layout>

If the conversion fails, logback-gelf will leave the field value alone
(i.e.: send it as String) and print the stacktrace