Docker GELF logging additional fields

1.7k views Asked by At

I am trying to make my docker-compose file write its logging to a Graylog server, using the GELF protocol. This works fine, using the following configuration (snippet of docker-compose.yml):

     logging:
      driver: gelf
      options:
        gelf-address: ${GELF_ADDRESS}

The Graylog server receives the messages I log in the JBoss instance in my Docker container. It also adds some extra GELF fields, like container_name and image_name.

My question is, how can I add extra GELF fields myself? I want it to pass _username as an extra field. I have this field available in my MDC context. I could add the information to the message by using a formatter (Conversion Pattern) in my CONSOLE logger, by adding the following to this logger:

%X{_user_name}

But this is not what I want, as it will be in the GELF message field, not added as seperate extra field.

Any thoughts?

1

There are 1 answers

1
Paul S On BEST ANSWER

It does seem impossible in the current docker-compose version (1.8.0) to include the extra fields.

I ended up removing any logging configuration from the docker-compose file and instead integrate the GELF logging in the docker container's application. Since I am using JBoss AS 7, I have used the steps as described here: http://logging.paluch.biz/examples/jbossas7.html

To log the container id, I have added the following configuration:

<custom-handler name="GelfLogger" class="biz.paluch.logging.gelf.jboss7.JBoss7GelfLogHandler" module="biz.paluch.logging">
<level name="INFO" />
<properties>
    <property name="host" value="udp:${GRAYLOG_HOST}" />
    <property name="port" value="${GRAYLOG_PORT}" />
    <property name="version" value="1.1" />
    <property name="additionalFields" value="dockerContainer=${HOSTNAME}" />
    <property name="includeFullMdc" value="true" />
</properties>

Field dockerContainer is substituted by the HOSTNAME environment variable on the docker container and contains the containerId. The other placeholders are substituted by docker-compose environment variables.

By including the full MDC, I was able to put the username (and some other fields) as an additional GELF field. (For more information about MDC, see http://logback.qos.ch/manual/mdc.html)