Reputation: 15076
I try to post my log to stdout. This works fine, but I have to rewrite it to .json format.
I tried to use the following: (found the idea on github)
<Appenders>
<Console name="stdout" target="SYSTEM_OUT">
<LogStashJSONLayout>
<!-- Example of what you might do to add fields, warning values should be known to be json escaped strings -->
<KeyValuePair key="application_name" value="${sys:application.name}"/>
<KeyValuePair key="application_version" value="${sys:application.version}"/>
<KeyValuePair key="environment_type" value="${sys:deploy_env}"/>
<KeyValuePair key="cluster_location" value="${sys:cluster_location}"/>
<KeyValuePair key="cluster_name" value="${sys:cluster_name}"/>
<KeyValuePair key="hostname" value="${sys:hostname}"/>
<KeyValuePair key="host_ip" value="${sys:host_ip}"/>
<!--Example of using system property substitution -->
<KeyValuePair key="application_user" value="${sys:user.name}"/>
<!--Example of using environment property substitution env:USERNAME on windows-->
<KeyValuePair key="environment_user" value="${env:USER}"/>
</LogStashJSONLayout>
</Console>
But this does not work. I see my log in stdout in plain text. Like this:
please check your split expression
Processing bucket : entityresolution-datasets
Splitter returned no results. If this is not expected, please check your split expression
Processing bucket : mesos-exhibitors3bucket-10oeaynyppkt7
Splitter returned no results. If this is not expected, please check your split expression
Processing bucket : process-log
Splitter returned no results. If this is not expected, please check your split expression
Processing bucket : segmentation-datasets
Splitter returned no results. If this is not expected, please check your split expression
So I want to use my logstash output in the LogStashJSONLayout format and printed to console instead of posting it using socket.
Upvotes: 1
Views: 846
Reputation: 60
I had the same issue but eventually found a solution which fit my needs. There's a JSONLayout.java file on Github that I use in my applications.
I added the file to my src/main/java folder in a package (com.example.log4j2 for example) and my log4j2 xml file looks like the following:
<Configuration packages="com.example.log4j2" verbose="false">
<Appenders>
<Console name="stdout" target="SYSTEM_OUT">
<JSONLayout>
</JSONLayout>
</Console>
</Appenders>
...
<Loggers>
...
<AsyncRoot level="WARN">
<AppenderRef ref="stdout" />
</AsyncRoot>
</Loggers>
Let me know if this works for you as well. I made some changes to the name of the package and the @Plugin(name = ...) so I might have messed something up.
Upvotes: 2