2

I have Filebeat pulling logs from stdout. I want to ensure my logs are outputted as JSON, so they can be properly parsed.

Thus far, here's what I've found:

  • org.jboss.logmanager.formatters doesn't have a JSON formatter
  • There's an "extension" module that provides a JsonFormatter class.
  • I can use it in my logging.properties by doing something like this:
handler.CONSOLE=org.jboss.logmanager.handlers.ConsoleHandler
handler.CONSOLE.properties=autoFlush,target
handler.CONSOLE.autoFlush=true
handler.CONSOLE.formatter=JSON-FORMATTER
handler.CONSOLE.target=SYSTEM_OUT

formatter.JSON-FORMATTER=org.jboss.logmanager.ext.formatters.JSONFormatter

I need to know:

  • Am I missing anything with this configuration?
  • How can I customise the JSON output (i.e. add or remove fields)?
shalvah
  • 881
  • 10
  • 21

1 Answers1

5

There is a json-formatter in WildFly 14. I would not suggest editing the logging.properties. The following CLI commands are an example of configuring a json-formatter.

/subsystem=logging/json-formatter=json:add(exception-output-type=formatted, pretty-print=false, meta-data={label=value})
/subsystem=logging/console-handler=CONSOLE:write-attribute(name=named-formatter, value=json)

Note the meta-data attribute is just a key/value pair separated by commas.

How can I customise the JSON output (i.e. add or remove fields)?

You can really only add metadata or change field names. You can't remove fields though.

James R. Perkins
  • 16,800
  • 44
  • 60
  • How can i add custom fields e.g "httpRequest",'Service',meta-data field ?i have added mata-data="Service:Demo" then it gave me error as meta-data field is not allowed.I am making changes in standalone.xml file - --.Can you guide how can i add custom fileds as well as meta-data attribute? – Neha Mar 11 '21 at 11:32
  • It's better to use CLI or the web console to make configuration changes. Editing the XML is not advised especially for logging as there is a `logging.propeties` that is also used at boot time. – James R. Perkins Mar 11 '21 at 18:54
  • Thanks James R. ,But if in a case i want to revert this then how can i revert the changes ? – Neha Mar 12 '21 at 08:01
  • Where should i execute this command ? it is not working. – Neha Mar 12 '21 at 13:21
  • If there is currently no value you'd simply do `/subsystem=logging/json-formatter=json:undefine-attribute(name=meta-data)`. You execute it on a running server using `jboss-cli.sh`. If the server is not running you can start the embedded server to configure it. – James R. Perkins Mar 12 '21 at 15:33
  • sudo bash jboss-cli.sh /subsystem=logging/json-formatter=json:add(exception-output-type=formatted, pretty-print=true, meta-data={Service=demo}) is this correct ? because runnig this is giving as error near unexpeted token ( – Neha Mar 16 '21 at 10:57
  • 2
    `sudo bash jboss-cli.sh -c "/subsystem=logging/json-formatter=json:add(exception-output-type=formatted, pretty-print=true, meta-data={Service=demo})"` should work. Make sure you connect CLI to the running server and quote the command if you're doing it all from the command line. – James R. Perkins Mar 16 '21 at 14:36
  • Thanks James,It worked like a charm.Just one last thing ,if i want to enable a logs in json format for a particular class , will making changes in in standalone.xml -profile-subsystem- should work right ? m i making any mistake ? this com.demo is avaialble inside war file. – Neha Mar 17 '21 at 09:45
  • As long as that is what the logger name is. Again though I'd use CLI and not edit the XML directly. – James R. Perkins Mar 17 '21 at 15:07
  • Can i get a reference link which i can refer? I do not know how to excute this cli commands ans where to execute and what file should i give as argument? – Neha Mar 18 '21 at 06:01
  • 1
    You could also use the web console if you do not want to use CLI. It's really just best not to edit the XML directly. It can be done, but it's not ideal. You can look at https://docs.wildfly.org/14/Admin_Guide.html#CLI_Recipes. But your command would be something like `/subsystem=logging/logger=com.demo:add(level=INFO)` – James R. Perkins Mar 18 '21 at 18:28
  • Like this ? sudo bash jboss-cli.sh -c "/subsystem=logging/json-formatter=json:com.demo.add(exception-output-type=formatted, pretty-print=true, meta-data={Service=demo})". – Neha Mar 19 '21 at 09:47
  • Yeah that looks right to me. You can leave the command off to start an interactive session as well. – James R. Perkins Mar 19 '21 at 19:15
  • I have one question which i have posted here https://stackoverflow.com/questions/66797363/how-to-print-logs-in-jsonformat-with-metadata-field-in-jbpm-wildfly could you please update/review once. – Neha Mar 25 '21 at 10:32
  • I tried this sudo bash jboss-cli.sh -c "/subsystem=logging/json-formatter=json:com.demo.add(exception-output-type=formatted, pretty-print=true, meta-data={Service=demo})" but it says error near token com.demo.add. – Neha Mar 31 '21 at 06:13
  • That's because `com.demo.add` is not an operation. The `:` separator denotes an operation. What do you want the `com.demo` to represent? – James R. Perkins Mar 31 '21 at 14:39
  • Want to apply logger json format for com.demo package only. – Neha Apr 01 '21 at 10:19
  • A formatter is not a logger. You create a formatter and assign it to a handler. You can assign a handler to a logger. So you'd need 3 steps to get the formatter assigned to a logger. – James R. Perkins Apr 01 '21 at 14:51