From 35dec2c3e87f2f44c3ab0269e7f737afdc28801a Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Thu, 7 Sep 2017 07:48:13 -0700 Subject: 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 -- cgit v1.2.1