From f7c8032e0993a6dc6bb808b0f2234324d3fe9707 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Wed, 6 Sep 2017 22:41:15 -0700 Subject: [PATCH 1/3] Add JSON logger in `log/api_json.log` for Grape API endpoints Closes #36189 --- Gemfile | 1 + Gemfile.lock | 3 +++ changelogs/unreleased/sh-add-grape-logging.yml | 5 +++++ lib/api/api.rb | 9 +++++++++ lib/gitlab/api_logger.rb | 8 ++++++++ 5 files changed, 26 insertions(+) create mode 100644 changelogs/unreleased/sh-add-grape-logging.yml create mode 100644 lib/gitlab/api_logger.rb diff --git a/Gemfile b/Gemfile index 0341f2609ad..d5e224c417c 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 320d42b8974..9fc47bbf848 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) @@ -1034,6 +1036,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 1405a5d0f0e..63df22c508b 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -2,6 +2,15 @@ module API class API < Grape::API 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 prefix :api diff --git a/lib/gitlab/api_logger.rb b/lib/gitlab/api_logger.rb new file mode 100644 index 00000000000..09122b233ea --- /dev/null +++ b/lib/gitlab/api_logger.rb @@ -0,0 +1,8 @@ +module Gitlab + class ApiLogger < ::Logger + + def format_message(severity, timestamp, progname, message) + super + "\n" + end + end +end From c304dfd4d6ca0f52537044742bb6dd6c219bdbbf Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Thu, 7 Sep 2017 07:02:46 -0700 Subject: [PATCH 2/3] Fix Rubocop failures in API logger --- lib/api/api.rb | 8 +++++--- lib/gitlab/api_logger.rb | 1 - 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/api/api.rb b/lib/api/api.rb index 63df22c508b..0ab0c8c490a 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -7,9 +7,11 @@ module API 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 ] + 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/api_logger.rb b/lib/gitlab/api_logger.rb index 09122b233ea..dcb194a1b89 100644 --- a/lib/gitlab/api_logger.rb +++ b/lib/gitlab/api_logger.rb @@ -1,6 +1,5 @@ module Gitlab class ApiLogger < ::Logger - def format_message(severity, timestamp, progname, message) super + "\n" end From 35dec2c3e87f2f44c3ab0269e7f737afdc28801a Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Thu, 7 Sep 2017 07:48:13 -0700 Subject: [PATCH 3/3] Use a custom GrapeLogging formatter to get the timestamp --- lib/api/api.rb | 4 ++-- lib/gitlab/api_logger.rb | 7 ------- .../formatters/lograge_with_timestamp.rb | 19 +++++++++++++++++++ 3 files changed, 21 insertions(+), 9 deletions(-) delete mode 100644 lib/gitlab/api_logger.rb create mode 100644 lib/gitlab/grape_logging/formatters/lograge_with_timestamp.rb diff --git a/lib/api/api.rb b/lib/api/api.rb index 0ab0c8c490a..af0b38963f5 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -5,8 +5,8 @@ module API LOG_FILENAME = Rails.root.join("log", "api_json.log") use GrapeLogging::Middleware::RequestLogger, - logger: ::Gitlab::ApiLogger.new(LOG_FILENAME), - formatter: GrapeLogging::Formatters::Json.new, + logger: Logger.new(LOG_FILENAME), + formatter: Gitlab::GrapeLogging::Formatters::LogrageWithTimestamp.new, include: [ GrapeLogging::Loggers::Response.new, GrapeLogging::Loggers::FilterParameters.new, diff --git a/lib/gitlab/api_logger.rb b/lib/gitlab/api_logger.rb deleted file mode 100644 index dcb194a1b89..00000000000 --- a/lib/gitlab/api_logger.rb +++ /dev/null @@ -1,7 +0,0 @@ -module Gitlab - class ApiLogger < ::Logger - def format_message(severity, timestamp, progname, message) - super + "\n" - end - end -end 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