Commit f7c8032e authored by Stan Hu's avatar Stan Hu

Add JSON logger in `log/api_json.log` for Grape API endpoints

Closes #36189
parent 21935d85
...@@ -407,3 +407,4 @@ gem 'flipper-active_record', '~> 0.10.2' ...@@ -407,3 +407,4 @@ gem 'flipper-active_record', '~> 0.10.2'
# Structured logging # Structured logging
gem 'lograge', '~> 0.5' gem 'lograge', '~> 0.5'
gem 'grape_logging', '~> 1.6'
...@@ -355,6 +355,8 @@ GEM ...@@ -355,6 +355,8 @@ GEM
activesupport activesupport
grape (>= 0.16.0) grape (>= 0.16.0)
rake rake
grape_logging (1.6.0)
grape
grpc (1.4.5) grpc (1.4.5)
google-protobuf (~> 3.1) google-protobuf (~> 3.1)
googleauth (~> 0.5.1) googleauth (~> 0.5.1)
...@@ -1034,6 +1036,7 @@ DEPENDENCIES ...@@ -1034,6 +1036,7 @@ DEPENDENCIES
grape (~> 1.0) grape (~> 1.0)
grape-entity (~> 0.6.0) grape-entity (~> 0.6.0)
grape-route-helpers (~> 2.1.0) grape-route-helpers (~> 2.1.0)
grape_logging (~> 1.6)
haml_lint (~> 0.26.0) haml_lint (~> 0.26.0)
hamlit (~> 2.6.1) hamlit (~> 2.6.1)
hashie-forbidden_attributes hashie-forbidden_attributes
......
---
title: Add JSON logger in `log/api_json.log` for Grape API endpoints
merge_request:
author:
type: added
...@@ -2,6 +2,15 @@ module API ...@@ -2,6 +2,15 @@ module API
class API < Grape::API class API < Grape::API
include APIGuard include APIGuard
LOG_FILENAME = Rails.root.join("log", "api_json.log")
use GrapeLogging::Middleware::RequestLogger,
logger: ::Gitlab::ApiLogger.new(LOG_FILENAME),
formatter: GrapeLogging::Formatters::Json.new,
include: [ GrapeLogging::Loggers::Response.new,
GrapeLogging::Loggers::FilterParameters.new,
GrapeLogging::Loggers::ClientEnv.new ]
allow_access_with_scope :api allow_access_with_scope :api
prefix :api prefix :api
......
module Gitlab
class ApiLogger < ::Logger
def format_message(severity, timestamp, progname, message)
super + "\n"
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment