diff options
author | Sean McGivern <sean@mcgivern.me.uk> | 2017-09-07 17:39:14 +0000 |
---|---|---|
committer | Sean McGivern <sean@mcgivern.me.uk> | 2017-09-07 17:39:14 +0000 |
commit | 7e19b5bfb6e1e209388fa5464a980aa742fdd8c3 (patch) | |
tree | 9296679fbbe1ff75eddd47f85dd44dc1dcd55b9c /lib | |
parent | 66f08aad714b7d43333db1251474a11f1815ec4c (diff) | |
parent | 35dec2c3e87f2f44c3ab0269e7f737afdc28801a (diff) | |
download | gitlab-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
Diffstat (limited to 'lib')
-rw-r--r-- | lib/api/api.rb | 11 | ||||
-rw-r--r-- | lib/gitlab/grape_logging/formatters/lograge_with_timestamp.rb | 19 |
2 files changed, 30 insertions, 0 deletions
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 |