Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

API Logging in Json format

I would like to log API request/response as json format.

The expected LogEntry is something like

{ 
 "timestamp" : "...",
 "level" : "DEBUG",
 "headers" : [
    "header1" : "value1",
    "header2" : "value2",
    "header3" : "value3"
 ],
 "requestPayload" : "<Request Json>"   // prefereablly as sub-document, worst case string is fine.   
 "labels" : [        //key fields which can be used for searching the logentry
     "searchField1" : "....",
     "searchField2" : "....",
     "searchField3" : "...."
  ]
}

My Question is :

Using Logback, How to log nested fields (e.g. headers, labels, requestPaylod in above example), as json sub-document. I tried MDC, but its limited to Map of 'String, String' only, and considers all fields after first level as String.

I hate to write my custom logger for this, and would like to use the goodness of proven logging frameworks(logback/log4j) to control the logging level, time stamping log event, etc.

like image 708
kiran Avatar asked Aug 14 '17 20:08

kiran


People also ask

What is JSON format in API?

JSON (JavaScript Object Notation) is our most commonly used format. JSON is a text-based open standard derived from the format used to represent simple data structures in JavaScript. Although it is rooted in JavaScript, it is language-agnostic and parsers exist for all popular (and many unpopular) languages.

What is JSON logging?

JSON logging is a kind of structured logging, Meaning you'll have your log data parsed and stored in a structured JSON format.

How do I enable JSON logging?

To enable parsing JSON log, you add parse: json to a pipeline in the ClusterLogForwarder CR, as shown in the following example. When you enable parsing JSON logs by using parse: json , the CR copies the JSON-structured log entry in a structured field, as shown in the following example.

How do I view JSON logs?

Click Expand JSON on the upper right side of the Messages table to open the structure. You can see the full contents and structure of the log messages. Click Collapse JSON to see the initial collapsed view.


1 Answers

For request/response logging try Logbook and logstash-logback-encoder. You'll be able to use Markers to add structured contents.

For adding 'your own' fields in a JSON structure, I've written a code generator you might find interesting, it adds builder support: json-log-domain

like image 88
ThomasRS Avatar answered Nov 16 '22 03:11

ThomasRS