0

I'm trying to log all my requests, responses and application logs in the ECS Format - https://www.elastic.co/guide/en/ecs/current/ecs-field-reference.html

Reading their guides I'm using morgan as my logger and I get the following format out of the box

{
  "@timestamp": "2022-09-29T11:29:34.003Z",
  "log.level": "info",
  "message": "::1 - - [29/Sep/2022:11:29:34 +0000] \"GET /collections/9ad4c37b-b9d1-4e0a-af36-2674903250dd HTTP/1.1\" 200 1217 \"-\" \"PostmanRuntime/7.29.2\"",
  "ecs": {
    "version": "1.6.0"
  },
  "http": {
    "version": "1.1",
    "request": {
      "method": "GET",
      "headers": {
        "user-agent": "PostmanRuntime/7.29.2",
        "accept": "*/*",
        "cache-control": "no-cache",
        "postman-token": "430e11f2-3b45-4f46-9e61-5f858d460f74",
        "host": "localhost:4000",
        "accept-encoding": "gzip, deflate, br",
        "connection": "keep-alive"
      }
    },
    "response": {
      "status_code": 200,
      "headers": {
        "x-powered-by": "Express",
        "access-control-allow-origin": "*",
        "content-type": "application/json; charset=utf-8",
        "content-length": "1217",
        "etag": "W/\"4c1-AcHAjm2O54LEkw0o77l3WqK+Ybw\""
      },
      "body": {
        "bytes": 1217
      }
    }
  },
  "url": {
    "path": "/collections/9ad4c37b-b9d1-4e0a-af36-2674903250dd",
    "domain": "localhost",
    "full": "http://localhost:4000/collections/9ad4c37b-b9d1-4e0a-af36-2674903250dd"
  },
  "client": {
    "address": "::1",
    "ip": "::1",
    "port": 53225
  },
  "user_agent": {
    "original": "PostmanRuntime/7.29.2"
  }
}

I would also like some extra fields like Error Fields in case I get a 4xx error on my API. How do I map what fields I want into morgan? or pino or winston. Anything is fine.

I've searched a lot on the internet and couldn't find help. Thank you in advance.

0 Answers0