Search code examples
javalogstashlog4j2gelf

How to add a static field to gelf logstash appender in properties file? biz.paluch.logging


I am using GELF-logstash appender together with log4j2 as logging appender, to send logs from my application through logstash to ElasticSearch/Kibana.
In each log message I want to configure a static field, namely the region my application is deployed to, which is gathered from an environment variable differing by the region the application is running in.

I found out that Static Literals are supported by the XML configuration like this: <Field name="fieldName1" literal="your literal value" /> https://logging.paluch.biz/examples/log4j-2.x.html But I would like to stay with my log4j2.properties file based configuration if possible, but could not find a documentation to add a static literal there.


Solution

  • Yes, this is possible. You can use the pre-defined field facility, or define as many custom static fields as you wish in your log4j2.properties file:

    # required configuration for GELF
    appender.gelf.type = Gelf
    appender.gelf.name = GELF
    appender.gelf.host = ${sys:LOG_HOST}
    appender.gelf.port = ${sys:LOG_PORT}
    
    # pre-defined field facility
    appender.gelf.facility = a_static_value
    
    # custom static field using environment variable
    appender.gelf.environment.type = Field
    appender.gelf.environment.name = environment_logging_field_name
    appender.gelf.environment.literal = ${sys:ENV_VAR_NAME}
    
    # custom static field with static value
    appender.gelf.secondfield.type = Field
    appender.gelf.secondfield.name = fieldName
    appender.gelf.secondfield.literal = your_value_here