├── .gitignore ├── CODE_OF_CONDUCT.md ├── Gemfile ├── Gemfile.lock ├── LICENSE.txt ├── README.md ├── Rakefile ├── bin ├── console └── setup ├── graphql-rails_logger.gemspec ├── lib └── graphql │ ├── rails_logger.rb │ └── rails_logger │ ├── configuration.rb │ ├── railtie.rb │ ├── subscriber.rb │ └── version.rb └── misc ├── screenshot_after.png └── screenshot_before.png /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /_yardoc/ 4 | /coverage/ 5 | /doc/ 6 | /pkg/ 7 | /spec/reports/ 8 | /tmp/ 9 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level of experience, 9 | nationality, personal appearance, race, religion, or sexual identity and 10 | orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at ifuelen@gmail.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at [http://contributor-covenant.org/version/1/4][version] 72 | 73 | [homepage]: http://contributor-covenant.org 74 | [version]: http://contributor-covenant.org/version/1/4/ 75 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | git_source(:github) { |repo_name| "https://github.com/#{repo_name}" } 4 | 5 | gem 'rouge' # highlight graphql queries in logs 6 | gemspec 7 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | PATH 2 | remote: . 3 | specs: 4 | graphql-rails_logger (1.2.4) 5 | actionpack (> 5.0) 6 | activesupport (> 5.0) 7 | railties (> 5.0) 8 | rouge (>= 3.0) 9 | 10 | GEM 11 | remote: https://rubygems.org/ 12 | specs: 13 | actionpack (6.1.4.7) 14 | actionview (= 6.1.4.7) 15 | activesupport (= 6.1.4.7) 16 | rack (~> 2.0, >= 2.0.9) 17 | rack-test (>= 0.6.3) 18 | rails-dom-testing (~> 2.0) 19 | rails-html-sanitizer (~> 1.0, >= 1.2.0) 20 | actionview (6.1.4.7) 21 | activesupport (= 6.1.4.7) 22 | builder (~> 3.1) 23 | erubi (~> 1.4) 24 | rails-dom-testing (~> 2.0) 25 | rails-html-sanitizer (~> 1.1, >= 1.2.0) 26 | activesupport (6.1.4.7) 27 | concurrent-ruby (~> 1.0, >= 1.0.2) 28 | i18n (>= 1.6, < 2) 29 | minitest (>= 5.1) 30 | tzinfo (~> 2.0) 31 | zeitwerk (~> 2.3) 32 | builder (3.3.0) 33 | concurrent-ruby (1.3.4) 34 | crass (1.0.6) 35 | erubi (1.13.0) 36 | i18n (1.14.6) 37 | concurrent-ruby (~> 1.0) 38 | loofah (2.23.1) 39 | crass (~> 1.0.2) 40 | nokogiri (>= 1.12.0) 41 | method_source (1.1.0) 42 | minitest (5.25.1) 43 | nokogiri (1.16.7-aarch64-linux) 44 | racc (~> 1.4) 45 | nokogiri (1.16.7-arm-linux) 46 | racc (~> 1.4) 47 | nokogiri (1.16.7-arm64-darwin) 48 | racc (~> 1.4) 49 | nokogiri (1.16.7-x86-linux) 50 | racc (~> 1.4) 51 | nokogiri (1.16.7-x86_64-darwin) 52 | racc (~> 1.4) 53 | nokogiri (1.16.7-x86_64-linux) 54 | racc (~> 1.4) 55 | racc (1.8.1) 56 | rack (2.2.10) 57 | rack-test (2.1.0) 58 | rack (>= 1.3) 59 | rails-dom-testing (2.2.0) 60 | activesupport (>= 5.0.0) 61 | minitest 62 | nokogiri (>= 1.6) 63 | rails-html-sanitizer (1.6.0) 64 | loofah (~> 2.21) 65 | nokogiri (~> 1.14) 66 | railties (6.1.4.7) 67 | actionpack (= 6.1.4.7) 68 | activesupport (= 6.1.4.7) 69 | method_source 70 | rake (>= 0.13) 71 | thor (~> 1.0) 72 | rake (10.5.0) 73 | rouge (4.5.1) 74 | thor (1.3.2) 75 | tzinfo (2.0.6) 76 | concurrent-ruby (~> 1.0) 77 | zeitwerk (2.7.1) 78 | 79 | PLATFORMS 80 | aarch64-linux 81 | arm-linux 82 | arm64-darwin 83 | x86-linux 84 | x86_64-darwin 85 | x86_64-linux 86 | 87 | DEPENDENCIES 88 | bundler 89 | graphql-rails_logger! 90 | rake (~> 10.0) 91 | rouge 92 | 93 | BUNDLED WITH 94 | 2.5.20 95 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 JetRuby Agency 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GraphQL::RailsLogger 2 | 3 | ## About 4 | This gem allows you to inspect graphql queries in a more readable format. 5 | 6 | This... 7 | 8 | ![screenshot_before](misc/screenshot_before.png) 9 | 10 | becomes this ... 11 | 12 | ![screenshot_after](misc/screenshot_after.png) 13 | 14 | ## Installation 15 | 16 | Add this line to your application's Gemfile: 17 | 18 | ```ruby 19 | gem 'graphql-rails_logger' 20 | ``` 21 | 22 | And then execute: 23 | 24 | $ bundle 25 | 26 | ## Configuration 27 | 28 | By default this gem formats params only for `GraphqlController#execute`. 29 | 30 | If you want to change this behaviour, add `config/initializers/graphql_rails_logger.rb` file and set proper controller and actions like this: 31 | 32 | ```ruby 33 | GraphQL::RailsLogger.configure do |config| 34 | config.white_list = { 35 | 'QueriesController' => %w(create) 36 | } 37 | end 38 | ``` 39 | 40 | There is an option to suppress (hide) the GraphQL Introspection Query from the console output. This may be helpful to declutter the console during client testing as these can be rather lengthy. 41 | 42 | ```ruby 43 | GraphQL::RailsLogger.configure do |config| 44 | config.skip_introspection_query = true 45 | end 46 | ``` 47 | 48 | The theme can be configured as well. The theme is applied using the [rouge](https://github.com/jneen/rouge) gem, where all available options can be found. The default value is `Rouge::Themes::ThankfulEyes.new`. 49 | 50 | ```ruby 51 | GraphQL::RailsLogger.configure do |config| 52 | config.theme = Rouge::Themes::Pastie.new 53 | end 54 | ``` 55 | 56 | ## Contributing 57 | 58 | Bug reports and pull requests are welcome on GitHub at https://github.com/jetruby/graphql-rails_logger. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. 59 | 60 | ## License 61 | 62 | The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). 63 | 64 | ## Code of Conduct 65 | 66 | Everyone interacting in the GraphQL::RailsLogger project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/jetruby/graphql-rails_logger/blob/master/CODE_OF_CONDUCT.md). 67 | 68 | ## About JetRuby 69 | 70 | GraphQL::RailsLogger is maintained and founded by JetRuby Agency. 71 | 72 | We love open source software! 73 | See [our projects][portfolio] or 74 | [contact us][contact] to design, develop, and grow your product. 75 | 76 | [portfolio]: http://jetruby.com/portfolio/ 77 | [contact]: http://jetruby.com/#contactUs 78 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'bundler/gem_tasks' 2 | task default: :spec 3 | -------------------------------------------------------------------------------- /bin/console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require 'bundler/setup' 4 | require 'graphql/rails_logger' 5 | 6 | # You can add fixtures and/or initialization code here to make experimenting 7 | # with your gem easier. You can also use a different console, if you like. 8 | 9 | # (If you use this, don't forget to add pry to your Gemfile!) 10 | # require "pry" 11 | # Pry.start 12 | 13 | require 'irb' 14 | IRB.start(__FILE__) 15 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -euo pipefail 3 | IFS=$'\n\t' 4 | set -vx 5 | 6 | bundle install 7 | 8 | # Do any other automated setup that you need to do here 9 | -------------------------------------------------------------------------------- /graphql-rails_logger.gemspec: -------------------------------------------------------------------------------- 1 | lib = File.expand_path('../lib', __FILE__) 2 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 3 | require 'graphql/rails_logger/version' 4 | 5 | Gem::Specification.new do |spec| 6 | spec.name = 'graphql-rails_logger' 7 | spec.version = GraphQL::RailsLogger::VERSION 8 | spec.authors = ['JetRuby'] 9 | spec.email = ['engineering@jetruby.com'] 10 | 11 | spec.summary = 'Pretty logger for Rails + GraphQL applications.' 12 | spec.description = 'Display GraphQL queries in a more readable format.' 13 | spec.homepage = 'https://github.com/jetruby/graphql-rails_logger' 14 | spec.license = 'MIT' 15 | 16 | spec.files = `git ls-files -z`.split("\x0").reject do |f| 17 | f.match(%r{^(test|spec|features)/}) 18 | end 19 | spec.require_paths = ['lib'] 20 | 21 | spec.add_dependency 'rouge', '>= 3.0' 22 | 23 | # 5 because not tested with 4 24 | spec.add_dependency 'actionpack', '> 5.0' 25 | spec.add_dependency 'activesupport', '> 5.0' 26 | spec.add_dependency 'railties', '> 5.0' 27 | 28 | spec.add_development_dependency 'bundler' 29 | spec.add_development_dependency 'rake', '~> 10.0' 30 | end 31 | -------------------------------------------------------------------------------- /lib/graphql/rails_logger.rb: -------------------------------------------------------------------------------- 1 | require 'graphql/rails_logger/version' 2 | require 'graphql/rails_logger/subscriber' 3 | require 'graphql/rails_logger/railtie' 4 | -------------------------------------------------------------------------------- /lib/graphql/rails_logger/configuration.rb: -------------------------------------------------------------------------------- 1 | require 'rouge' 2 | 3 | module GraphQL 4 | module RailsLogger 5 | class Configuration 6 | attr_accessor :skip_introspection_query, :white_list, :theme 7 | 8 | def initialize 9 | @skip_introspection_query = nil 10 | 11 | # controller => [actions] 12 | @white_list = { 13 | 'GraphqlController' => %w[execute] 14 | } 15 | 16 | @theme = Rouge::Themes::ThankfulEyes.new 17 | end 18 | end 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /lib/graphql/rails_logger/railtie.rb: -------------------------------------------------------------------------------- 1 | require 'graphql/rails_logger/subscriber' 2 | 3 | module GraphQL 4 | module RailsLogger 5 | class Railtie < Rails::Railtie 6 | initializer 'graphql.unsubscribe_default_logger' do 7 | ActiveSupport::Notifications.unsubscribe 'start_processing.action_controller' 8 | Subscriber.attach_to :action_controller 9 | end 10 | end 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /lib/graphql/rails_logger/subscriber.rb: -------------------------------------------------------------------------------- 1 | require 'graphql/rails_logger/configuration' 2 | require 'action_controller/log_subscriber' 3 | require 'rouge' 4 | require 'pp' 5 | 6 | module GraphQL 7 | module RailsLogger 8 | class << self 9 | attr_accessor :configuration 10 | end 11 | 12 | def self.configuration 13 | @configuration ||= Configuration.new 14 | end 15 | 16 | def self.configure 17 | yield(configuration) 18 | end 19 | 20 | class Subscriber < ActionController::LogSubscriber 21 | def start_processing(event) 22 | return unless logger.info? 23 | 24 | payload = event.payload 25 | params = payload[:params].except(*INTERNAL_PARAMS) 26 | format = payload[:format] 27 | format = format.to_s.upcase if format.is_a?(Symbol) 28 | 29 | config = GraphQL::RailsLogger.configuration 30 | 31 | info "Processing by #{payload[:controller]}##{payload[:action]} as #{format}" 32 | 33 | if config.white_list.fetch(payload[:controller], []).include?(payload[:action]) 34 | formatter = Rouge::Formatters::Terminal256.new(config.theme) 35 | query_lexer = Rouge::Lexers::GraphQL.new 36 | variables_lexer = Rouge::Lexers::Ruby.new 37 | 38 | (params['_json'] || [params.slice('query', 'variables', 'extensions')]).each do |data| 39 | 40 | next if config.skip_introspection_query && data['query']&.index(/query IntrospectionQuery/) 41 | 42 | # Cleanup and indent params for logging 43 | query = indent(data.fetch('query', '')) 44 | variables = indent(pretty(data.fetch('variables', ''))) 45 | extensions = indent(pretty(data.fetch('extensions', ''))) 46 | 47 | info "\nVariables:\n#{formatter.format(variables_lexer.lex(variables))}" if variables.present? 48 | info "\nQuery:\n#{formatter.format(query_lexer.lex(query))}" if query.present? 49 | info "\nExtensions:\n#{formatter.format(variables_lexer.lex(extensions))}" if extensions.present? 50 | end 51 | else 52 | info "\nParameters:\n#{params.inspect}" unless params.empty? 53 | end 54 | end 55 | 56 | private 57 | 58 | def indent(data) 59 | data.lines.map { |line| " #{line}" }.join.chomp 60 | end 61 | 62 | def pretty(data) 63 | return '' if data.blank? 64 | 65 | data = JSON.parse(data) if data.is_a?(String) 66 | PP.pp(data, '') 67 | end 68 | end 69 | end 70 | end 71 | -------------------------------------------------------------------------------- /lib/graphql/rails_logger/version.rb: -------------------------------------------------------------------------------- 1 | module GraphQL 2 | module RailsLogger 3 | VERSION = '1.2.5'.freeze 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /misc/screenshot_after.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jetruby/graphql-rails_logger/7c71fc236b5e3dc23a6f356f5e31f5995f9bc6dc/misc/screenshot_after.png -------------------------------------------------------------------------------- /misc/screenshot_before.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jetruby/graphql-rails_logger/7c71fc236b5e3dc23a6f356f5e31f5995f9bc6dc/misc/screenshot_before.png --------------------------------------------------------------------------------