본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

로깅 플러그인 출력 형식 수정 방법

원문 보기 위키 갱신

로깅 플러그인 출력 형식 수정 방법 (How to modify the logging plugins output format)

로깅 플러그인의 JSON 출력 구조를 변경하고 싶다면 custom_fields_by_lua 설정을 활용할 수 있어요. 표준 로그 데이터를 message 같은 커스텀 최상위 요소 아래에 중첩시키고, 나머지 기본 필드는 nil로 설정하여 원하는 형식으로 가공할 수 있답니다. Kong Gateway 버전에 따라 추가 필드가 있을 수 있으니 주의해야 해요.

출처: 문서

본문

요약

custom_fields_by_lua를 사용하여 로깅 플러그인의 출력을 사용자 정의 최상위 요소 아래에 중첩하려면 어떻게 해야 하나요? custom_fields_by_lua 구성 필드를 사용하여 kong.log.serialize()를 반환하는 새 최상위 요소(예: message)를 추가한 다음, 다른 모든 표준 최상위 필드를 return nil로 설정하여 중첩된 요소와 함께 유출되지 않도록 하세요. 일부 Kong Gateway 버전의 kong.log.serialize()는 workspace_name, upstream_status, source도 반환하므로, 이들도 nil 목록에 포함하지 않으면 추가 최상위 키로 나타나요.

개요

로깅 플러그인을 사용할 때 로그 정보는 문서에 설명된 형식에 따라 JSON의 최상위에 있어요. 중앙 집중식 로깅 수집기에 필요한 메타데이터 값 아래에 로깅 콘텐츠를 중첩하려면 이 형식을 어떻게 변경할 수 있나요?

단계

custom_fields_by_lua 기능을 사용하여 로깅 출력의 구조를 변경하세요.

이 예제에서는 로깅 콘텐츠를 message라는 요소 아래에 추가해야 하고 file-log 플러그인을 사용한다고 가정해요.

목표를 달성하려면 두 가지 구성 변경이 필요해요:

  1. 표준 로그 직렬화 콘텐츠를 포함하는 message라는 새 요소를 추가하세요.
  2. 표준 로깅의 최상위 요소를 제거하세요. 이는 해당 요소를 nil로 설정하여 수행할 수 있어요.

플러그인을 생성하는 전체 예제는 다음과 같아요:

curl -s -X POST 'http://<kong-admin-api>:8001/default/routes/<route_id>/plugins/' \
-H 'kong-admin-token: <token>' \
-H 'Content-Type: application/json' \
--data-raw '{
    "name": "file-log",
    "config": {
        "reopen": false,
        "path": "/dev/stdout",
        "custom_fields_by_lua": {
            "message": "return kong.log.serialize()",
            "tries": "return nil",
            "latencies": "return nil",
            "workspace": "return nil",
            "workspace_name": "return nil",
            "upstream_status": "return nil",
            "source": "return nil",
            "started_at": "return nil",
            "request": "return nil",
            "client_ip": "return nil",
            "response": "return nil",
            "service": "return nil",
            "upstream_uri": "return nil",
            "route": "return nil",
            "authenticated_entity": "return nil",
            "consumer": "return nil"
        }
    }
}'

참고: 일부 버전의 kong.log.serialize() 기본 테이블에는 이 레시피의 이전 예제에서 더 짧은 nil 목록에 포함되지 않은 추가 필드(예: workspace_name, upstream_status, source)가 포함되어 있어요. 이 필드들이 custom_fields_by_lua에서 생략되면 message 아래에 완전히 중첩되지 않고 message와 함께 추가 최상위 키로 유출돼요. 위 목록에는 이 필드들이 포함되어 있어 출력이 깔끔하게 중첩돼요. 사용 중인 Kong Gateway 버전의 kong.log.serialize() 출력에서 반환되는 필드를 확인하세요.

그러면 모든 표준 로그 콘텐츠가 message라는 상위 요소 아래에 중첩되어 출력돼요. 예를 들면 다음과 같아요:

{
	"message": {
		"started_at": 1788360462457,
		"tries": [{
			"balancer_start": 1788360462457,
			"balancer_latency": 1,
			"balancer_latency_ns": 134912,
			"ip": "172.18.0.6",
			"port": 80,
			"target_id": "unknown",
			"hostname": "httpbin",
			"keepalive": true
		}],
		"client_ip": "162.159.140.245",
		"workspace": "a1513093-a2ad-4b7a-ba3d-8cb6646274dd",
		"response": {
			"status": 200,
			"size": 1077,
			"headers": {
				"kong-service-name": "kb-nr-ae-flog-svc",
				"via": "1.1 kong/3.14.0.0-enterprise-edition",
				"x-kong-proxy-latency": "1",
				"x-kong-upstream-latency": "1",
				"content-length": "514",
				"server": "gunicorn/19.9.0",
				"connection": "close",
				"kong-route-id": "1feccf91-85bd-4a8f-a160-2edf9606767d",
				"kong-route-name": "kb-nr-ae-flog-route",
				"kong-service-id": "37709fba-90db-4a4e-8425-7a9b42f56543",
				"content-type": "application/json",
				"date": "Wed, 02 Sep 2026 14:47:42 GMT",
				"access-control-allow-origin": "*",
				"access-control-allow-credentials": "true",
				"x-kong-request-id": "43b55b00daeb4866b9649223abadee50"
			}
		},
		"latencies": {
			"third_party": 0.009984,
			"redis": 0,
			"socket": 0,
			"client": 0.59136,
			"kong": 1,
			"request": 2,
			"kong_internal": 0.30469926330566,
			"dns": 0.009984,
			"http_client": 0,
			"receive": 0,
			"proxy": 1
		},
		"service": {
			"id": "37709fba-90db-4a4e-8425-7a9b42f56543",
			"retries": 5,
			"port": 80,
			"enabled": true,
			"updated_at": 1788360381,
			"write_timeout": 60000,
			"name": "kb-nr-ae-flog-svc",
			"protocol": "http",
			"connect_timeout": 60000,
			"host": "httpbin",
			"read_timeout": 60000,
			"created_at": 1788360381,
			"ws_id": "a1513093-a2ad-4b7a-ba3d-8cb6646274dd",
			"path": "/anything"
		}
	}
}

관련 리소스

더 알아보기 (Learn more)