Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: CI
on: [push, pull_request]

jobs:
rubocop:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: 3.4.4
- name: Install dependencies
run: bundle install --jobs 4 --retry 3
- name: Run rubocop
run: bundle exec rake rubocop

specs:
runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: [3.4.4, 3.3.8, 3.2.8, 3.1.7]
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
- name: Install dependencies
run: bundle install --jobs 4 --retry 3
- name: Run specs
run: bundle exec rake spec
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
Expand Down
16 changes: 16 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
inherit_from: .rubocop_todo.yml

AllCops:
Exclude:
- grape_logging.gemspec
NewCops: enable
TargetRubyVersion: 3.1

Layout/LineLength:
Max: 100

Style/Documentation:
Enabled: false

Style/FrozenStringLiteralComment:
Enabled: false
82 changes: 82 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2025-06-03 20:45:19 UTC using RuboCop version 1.75.8.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 2
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: Max, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, SplitStrings.
# URISchemes: http, https
Layout/LineLength:
Exclude:
- 'spec/lib/grape_logging/loggers/request_headers_spec.rb'

# Offense count: 1
# Configuration parameters: IgnoreLiteralBranches, IgnoreConstantBranches, IgnoreDuplicateElseBranch.
Lint/DuplicateBranch:
Exclude:
- 'lib/grape_logging/util/parameter_filter.rb'

# Offense count: 1
# Configuration parameters: AllowedParentClasses.
Lint/MissingSuper:
Exclude:
- 'lib/grape_logging/loggers/filter_parameters.rb'

# Offense count: 3
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 37

# Offense count: 8
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
# AllowedMethods: refine
Metrics/BlockLength:
Max: 88

# Offense count: 1
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 104

# Offense count: 3
# Configuration parameters: AllowedMethods, AllowedPatterns.
Metrics/CyclomaticComplexity:
Max: 18

# Offense count: 6
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
Metrics/MethodLength:
Max: 22

# Offense count: 2
# Configuration parameters: AllowedMethods, AllowedPatterns.
Metrics/PerceivedComplexity:
Max: 19

# Offense count: 2
# Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers, AllowedPatterns.
# SupportedStyles: snake_case, normalcase, non_integer
# AllowedIdentifiers: TLS1_1, TLS1_2, capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339, x86_64
Naming/VariableNumber:
Exclude:
- 'spec/lib/grape_logging/formatters/rails_spec.rb'

# Offense count: 10
Style/OpenStructUse:
Exclude:
- 'spec/lib/grape_logging/loggers/client_env_spec.rb'
- 'spec/lib/grape_logging/loggers/filter_parameters_spec.rb'
- 'spec/lib/grape_logging/loggers/request_headers_spec.rb'
- 'spec/lib/grape_logging/loggers/response_spec.rb'

# Offense count: 2
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: Mode.
Style/StringConcatenation:
Exclude:
- 'lib/grape_logging/formatters/json.rb'
- 'lib/grape_logging/formatters/logstash.rb'
6 changes: 0 additions & 6 deletions .travis.yml

This file was deleted.

130 changes: 130 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
PATH
remote: .
specs:
grape_logging (2.1.0)
grape
rack

GEM
remote: https://rubygems.org/
specs:
activesupport (8.0.2)
base64
benchmark (>= 0.3)
bigdecimal
concurrent-ruby (~> 1.0, >= 1.3.1)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
logger (>= 1.4.2)
minitest (>= 5.1)
securerandom (>= 0.3)
tzinfo (~> 2.0, >= 2.0.5)
uri (>= 0.13.1)
ast (2.4.3)
base64 (0.3.0)
benchmark (0.4.1)
bigdecimal (3.2.1)
concurrent-ruby (1.3.5)
connection_pool (2.5.3)
diff-lcs (1.6.2)
drb (2.2.3)
dry-core (1.1.0)
concurrent-ruby (~> 1.0)
logger
zeitwerk (~> 2.6)
dry-inflector (1.2.0)
dry-logic (1.6.0)
bigdecimal
concurrent-ruby (~> 1.0)
dry-core (~> 1.1)
zeitwerk (~> 2.6)
dry-types (1.8.2)
bigdecimal (~> 3.0)
concurrent-ruby (~> 1.0)
dry-core (~> 1.0)
dry-inflector (~> 1.0)
dry-logic (~> 1.4)
zeitwerk (~> 2.6)
grape (2.3.0)
activesupport (>= 6)
dry-types (>= 1.1)
mustermann-grape (~> 1.1.0)
rack (>= 2)
zeitwerk
i18n (1.14.7)
concurrent-ruby (~> 1.0)
json (2.12.2)
language_server-protocol (3.17.0.5)
lint_roller (1.1.0)
logger (1.7.0)
minitest (5.25.5)
mustermann (3.0.3)
ruby2_keywords (~> 0.0.1)
mustermann-grape (1.1.0)
mustermann (>= 1.0.0)
parallel (1.27.0)
parser (3.3.8.0)
ast (~> 2.4.1)
racc
prism (1.4.0)
racc (1.8.1)
rack (3.1.15)
rainbow (3.1.1)
rake (13.3.0)
regexp_parser (2.10.0)
rspec (3.13.1)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.4)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.5)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.5)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.4)
rubocop (1.75.8)
json (~> 2.3)
language_server-protocol (~> 3.17.0.2)
lint_roller (~> 1.1.0)
parallel (~> 1.10)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 2.9.3, < 3.0)
rubocop-ast (>= 1.44.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 4.0)
rubocop-ast (1.45.0)
parser (>= 3.3.7.2)
prism (~> 1.4)
rubocop-rspec (3.6.0)
lint_roller (~> 1.1)
rubocop (~> 1.72, >= 1.72.1)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
securerandom (0.4.1)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (3.1.4)
unicode-emoji (~> 4.0, >= 4.0.4)
unicode-emoji (4.0.4)
uri (1.0.3)
zeitwerk (2.7.3)

PLATFORMS
arm64-darwin-23
ruby

DEPENDENCIES
bundler (~> 2.5)
grape_logging!
rake (~> 13.2)
rspec (~> 3.13)
rubocop (~> 1.75)
rubocop-rspec (~> 3.6)

BUNDLED WITH
2.6.9
11 changes: 11 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
require 'bundler/gem_tasks'

require 'rspec/core'
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |spec|
spec.rspec_opts = ['-fd']
spec.pattern = FileList['spec/**/*_spec.rb']
end

require 'rubocop/rake_task'
RuboCop::RakeTask.new(:rubocop)

task default: %i[rubocop spec]
5 changes: 5 additions & 0 deletions grape_logging.gemspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@


lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'grape_logging/version'
Expand Down Expand Up @@ -26,4 +28,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'bundler', '~> 2.5'
spec.add_development_dependency 'rake', '~> 13.2'
spec.add_development_dependency 'rspec', '~> 3.13'
spec.add_development_dependency 'rubocop', '~> 1.75'
spec.add_development_dependency 'rubocop-rspec', '~> 3.6'
spec.metadata['rubygems_mfa_required'] = 'true'
end
14 changes: 10 additions & 4 deletions lib/grape_logging/formatters/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,24 @@ def call(severity, datetime, _, data)
end

def format(data)
if data.is_a?(String)
case data
when String
data
elsif data.is_a?(Exception)
when Exception
format_exception(data)
elsif data.is_a?(Hash)
"#{data.delete(:status)} -- #{format_hash(data.delete(:time))} -- #{data.delete(:method)} #{data.delete(:path)} #{format_hash(data)}"
when Hash
[
data.delete(:status),
format_hash(data.delete(:time)),
"#{data.delete(:method)} #{data.delete(:path)} #{format_hash(data)}"
].join(' -- ')
else
data.inspect
end
end

private

def format_hash(hash)
hash.keys.sort.map { |key| "#{key}=#{hash[key]}" }.join(' ')
end
Expand Down
2 changes: 1 addition & 1 deletion lib/grape_logging/formatters/lograge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def call(severity, datetime, _, data)
view: time[:view],
datetime: datetime.iso8601
}.merge(data)
::Lograge.formatter.call(attributes) + "\n"
"#{::Lograge.formatter.call(attributes)}\n"
end
end
end
Expand Down
13 changes: 7 additions & 6 deletions lib/grape_logging/formatters/logstash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ module Formatters
class Logstash
def call(severity, datetime, _, data)
{
:'@timestamp' => datetime.iso8601,
:'@version' => '1',
:severity => severity
'@timestamp': datetime.iso8601,
'@version': '1',
severity: severity
}.merge!(format(data)).to_json + "\n"
end

private

def format(data)
if data.is_a?(Hash)
case data
when Hash
data
elsif data.is_a?(String)
when String
{ message: data }
elsif data.is_a?(Exception)
when Exception
format_exception(data)
else
{ message: data.inspect }
Expand Down
Loading