summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSean McGivern <sean@mcgivern.me.uk>2017-09-07 17:39:14 +0000
committerSean McGivern <sean@mcgivern.me.uk>2017-09-07 17:39:14 +0000
commit7e19b5bfb6e1e209388fa5464a980aa742fdd8c3 (patch)
tree9296679fbbe1ff75eddd47f85dd44dc1dcd55b9c
parent66f08aad714b7d43333db1251474a11f1815ec4c (diff)
parent35dec2c3e87f2f44c3ab0269e7f737afdc28801a (diff)
downloadgitlab-ce-7e19b5bfb6e1e209388fa5464a980aa742fdd8c3.tar.gz
Merge branch 'sh-add-grape-logging' into 'master'
Add JSON logger in `log/api_json.log` for Grape API endpoints Closes #36189 See merge request !14102
-rw-r--r--Gemfile1
-rw-r--r--Gemfile.lock3
-rw-r--r--changelogs/unreleased/sh-add-grape-logging.yml5
-rw-r--r--lib/api/api.rb11
-rw-r--r--lib/gitlab/grape_logging/formatters/lograge_with_timestamp.rb19
5 files changed, 39 insertions, 0 deletions
diff --git a/Gemfile b/Gemfile
index c5eb4ab51ca..a022319ae2c 100644
--- a/Gemfile
+++ b/Gemfile
@@ -407,3 +407,4 @@ gem 'flipper-active_record', '~> 0.10.2'
# Structured logging
gem 'lograge', '~> 0.5'
+gem 'grape_logging', '~> 1.6'
diff --git a/Gemfile.lock b/Gemfile.lock
index 9fbf08d80e2..d7e1c7581d5 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -355,6 +355,8 @@ GEM
activesupport
grape (>= 0.16.0)
rake
+ grape_logging (1.6.0)
+ grape
grpc (1.4.5)
google-protobuf (~> 3.1)
googleauth (~> 0.5.1)
@@ -1035,6 +1037,7 @@ DEPENDENCIES
grape (~> 1.0)
grape-entity (~> 0.6.0)
grape-route-helpers (~> 2.1.0)
+ grape_logging (~> 1.6)
haml_lint (~> 0.26.0)
hamlit (~> 2.6.1)
hashie-forbidden_attributes
diff --git a/changelogs/unreleased/sh-add-grape-logging.yml b/changelogs/unreleased/sh-add-grape-logging.yml
new file mode 100644
index 00000000000..eaf6cb045d5
--- /dev/null
+++ b/changelogs/unreleased/sh-add-grape-logging.yml
@@ -0,0 +1,5 @@
+---
+title: Add JSON logger in `log/api_json.log` for Grape API endpoints
+merge_request:
+author:
+type: added
diff --git a/lib/api/api.rb b/lib/api/api.rb
index d9a62bffb6d..ee4e1688e12 100644
--- a/lib/api/api.rb
+++ b/lib/api/api.rb
@@ -2,6 +2,17 @@ module API
class API < Grape::API
include APIGuard
+ LOG_FILENAME = Rails.root.join("log", "api_json.log")
+
+ use GrapeLogging::Middleware::RequestLogger,
+ logger: Logger.new(LOG_FILENAME),
+ formatter: Gitlab::GrapeLogging::Formatters::LogrageWithTimestamp.new,
+ include: [
+ GrapeLogging::Loggers::Response.new,
+ GrapeLogging::Loggers::FilterParameters.new,
+ GrapeLogging::Loggers::ClientEnv.new
+ ]
+
allow_access_with_scope :api
prefix :api
diff --git a/lib/gitlab/grape_logging/formatters/lograge_with_timestamp.rb b/lib/gitlab/grape_logging/formatters/lograge_with_timestamp.rb
new file mode 100644
index 00000000000..1e1fdabca93
--- /dev/null
+++ b/lib/gitlab/grape_logging/formatters/lograge_with_timestamp.rb
@@ -0,0 +1,19 @@
+module Gitlab
+ module GrapeLogging
+ module Formatters
+ class LogrageWithTimestamp
+ def call(severity, datetime, _, data)
+ time = data.delete :time
+ attributes = {
+ time: datetime.utc.iso8601(3),
+ severity: severity,
+ duration: time[:total],
+ db: time[:db],
+ view: time[:view]
+ }.merge(data)
+ ::Lograge.formatter.call(attributes) + "\n"
+ end
+ end
+ end
+ end
+end