Journaliser la sortie au format Json en utilisant lograge / lograge-sql avec RubyOnRails

À propos de lograge / lograge-sql

Lograge / lograge-sql peut être utilisé pour générer des journaux Rails et des journaux multilignes tels que l'exécution SQL dans un format tel que Json pour rendre les journaux faciles à lire.

lograge lograge est un joyau qui peut générer une seule ligne de journal (exemple: json) en utilisant uniquement les informations nécessaires pour le journal à plusieurs lignes de Rails basic Loggger. https://github.com/roidrage/lograge

lograge


{"method":"GET","path":"/xxxxx","format":"html","controller":"Test::TestController","action":"index","status":200,"duration":109.04,"view":80.24,"db":0.0,"params":{},"headers":"#<ActionDispatch::Http::Headers:0x00007faac342cd70>","view_runtime":80.24499999919499,"db_runtime":0,"custom_payload":{"timestamp":"2020-06-17 08:43:23 +0900","request_id":"25e160cb-1dcb-4b19-8eb2-9253f35b4f93","remote_ip":"127.0.0.1"},"operator_name":"Tester","email":"[email protected]","timestamp":"2020-06-17 08:43:23 +0900","request_id":"25e160cb-1dcb-4b19-8eb2-9253f35b4f93","remote_ip":"127.0.0.1"}

lograge-sql lograge-sql est une extension de lograge Gem qui génère SQL dans le journal https://github.com/iMacTia/lograge-sql

lograge-sql


{
    "sql_queries": "name1 ({duration1}) {query1}\nname2 ({duration2}) query2 ...",
    "sql_queries_count": 3
}

Comment utiliser

Commençons par la source du didacticiel Rails. https://github.com/yasslab/sample_app

lograge

  1. Ajouter lograge à Gemfile

Gemfile


gem "lograge"
  1. Réglage du lograge sur l'un des éléments suivants

config/environments/develpment.rb


  config.lograge.enabled = true
  1. Installez Gems avec bundle install '' ``
  2. Démarrage du serveur `` rails s ''

Avant d'utiliser lograge


Started GET "/" for 127.0.0.1 at 2020-06-17 15:01:01 +0900
   (0.1ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
  ↳ /Users/xxxx/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
Processing by StaticPagesController#home as HTML
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 101], ["LIMIT", 1]]
  ↳ app/helpers/sessions_helper.rb:13
  Rendering static_pages/home.html.erb within layouts/application
   (0.6ms)  SELECT COUNT(*) FROM "microposts" WHERE "microposts"."user_id" = ?  [["user_id", 101]]
  ↳ app/views/shared/_user_info.html.erb:4
  Rendered shared/_user_info.html.erb (3.3ms)
   (0.5ms)  SELECT COUNT(*) FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."followed_id" WHERE "relationships"."follower_id" = ?  [["follower_id", 101]]
  ↳ app/views/shared/_stats.html.erb:5
   (0.5ms)  SELECT COUNT(*) FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."follower_id" WHERE "relationships"."followed_id" = ?  [["followed_id", 101]]
  ↳ app/views/shared/_stats.html.erb:11
  Rendered shared/_stats.html.erb (16.1ms)
  Rendered shared/_error_messages.html.erb (0.6ms)
  Rendered shared/_micropost_form.html.erb (13.9ms)
   (0.2ms)  SELECT COUNT(*) FROM "microposts" WHERE (user_id = 101)
  ↳ app/views/shared/_feed.html.erb:1
  Rendered shared/_feed.html.erb (1.3ms)
  Rendered static_pages/home.html.erb within layouts/application (46.9ms)
  Rendered layouts/_shim.html.erb (0.4ms)
  Rendered layouts/_header.html.erb (1.0ms)
  Rendered layouts/_footer.html.erb (0.4ms)
Completed 200 OK in 610ms (Views: 543.5ms | ActiveRecord: 3.2ms)

Après avoir utilisé lograge


   (0.2ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
  ↳ /Users/xxxx/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 101], ["LIMIT", 1]]
  ↳ app/helpers/sessions_helper.rb:13
   (7.0ms)  SELECT COUNT(*) FROM "microposts" WHERE "microposts"."user_id" = ?  [["user_id", 101]]
  ↳ app/views/shared/_user_info.html.erb:4
   (0.4ms)  SELECT COUNT(*) FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."followed_id" WHERE "relationships"."follower_id" = ?  [["follower_id", 101]]
  ↳ app/views/shared/_stats.html.erb:5
   (0.4ms)  SELECT COUNT(*) FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."follower_id" WHERE "relationships"."followed_id" = ?  [["followed_id", 101]]
  ↳ app/views/shared/_stats.html.erb:11
   (0.1ms)  SELECT COUNT(*) FROM "microposts" WHERE (user_id = 101)
  ↳ app/views/shared/_feed.html.erb:1
method=GET path=/ format=html controller=StaticPagesController action=home status=200 duration=663.54 view=564.69 db=9.17

Le résultat de la connexion de l'analyse racine est affiché sur une ligne sur la dernière ligne


** Ajoutez ce qui suit si vous souhaitez sortir au format Json **

config/environments/develpment.rb


config.lograge.formatter = Lograge::Formatters::Json.new

Le résultat de la connexion à Root est affiché au format Json.

Format Json


{"method":"GET","path":"/","format":"html","controller":"StaticPagesController","action":"home","status":200,"duration":694.98,"view":592.73,"db":3.04}

** Ajoutez Hook pour personnaliser votre journal **

Ajout de config.lograge.custom_options

config/environments/develpment.rb


config.lograge.custom_options = ->(event) {
    exceptions = %w(controller action format authenticity_token)
    payload = event.payload
    payload[:params] = payload[:params].except(*exceptions) #controller,Éliminer le nom de l'action
    
    #Si une exception se produit, affiche l'exception et la traçabilité
    if payload[:exception]
      payload[:exception] = payload[:exception]
      payload[:exception_backtrace] = payload[:exception_object].backtrace[0..6]
    end
    payload
  }

Soulever l'exception au contrôleur

static_pages_controller.rb


def home
    raise "error"
end

Rogue après la connexion root

Avant d'ajouter Hook (StaticPagesController)


{"method":"GET","path":"/","format":"html","controller":"StaticPagesController","action":"home","status":500,"error":"RuntimeError: error","duration":6.77,"view":0.0,"db":0.0,"sql_queries":"' (0.13) SELECT \"schema_migrations\".\"version\" FROM \"schema_migrations\" ORDER BY \"schema_migrations\".\"version\" ASC'","sql_queries_count":1}

Avec l'ajout de Hook, exception et exception_backtrace ont été ajoutés.

Après avoir ajouté Hook


{"method":"GET","path":"/","format":"html","controller":"StaticPagesController","action":"home","status":500,"error":"RuntimeError: error","duration":0.51,"view":0.0,"db":0.0,"params":{},"headers":"#<ActionDispatch::Http::Headers:0x00007f8ed7971e78>","view_runtime":null,"db_runtime":0,"exception":["RuntimeError","error"],"exception_object":"error","exception_backtrace":["/Users/xxxx/Desktop/ruby/sample_app/app/controllers/static_pages_controller.rb:9:in `home'","/Users/xxxx/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/actionpack-5.2.3/lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'","/Users/xxxx/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/actionpack-5.2.3/lib/abstract_controller/base.rb:194:in `process_action'","/Users/xxxx/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/actionpack-5.2.3/lib/action_controller/metal/rendering.rb:30:in `process_action'","/Users/xxxx/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/actionpack-5.2.3/lib/abstract_controller/callbacks.rb:42:in `block in process_action'","/Users/xxxx/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/activesupport-5.2.3/lib/active_support/callbacks.rb:132:in `run_callbacks'","/Users/xxxx/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/actionpack-5.2.3/lib/abstract_controller/callbacks.rb:41:in `process_action'"]}
  

** Ajouter des éléments à la charge utile en remplaçant la fonction append_info_to_payload au contrôleur **

StaticPagesController


class StaticPagesController < ApplicationController
  def append_info_to_payload(payload)
    super
    payload[:host] = request.host
  end
end

Les informations de l'hôte ont été ajoutées `" host ":" localhost "`

Après l'addition


{"method":"GET","path":"/","format":"html","controller":"StaticPagesController","action":"home","status":200,"duration":1321.96,"view":1148.47,"db":3.28,"params":{},"headers":"#<ActionDispatch::Http::Headers:0x00007fc6e8869d98>","view_runtime":1148.4679999998662,"db_runtime":3.281999999217078,"host":"localhost","sql_queries":"' (0.27) SELECT \"schema_migrations\".\"version\" FROM \"schema_migrations\" ORDER BY \"schema_migrations\".\"version\" ASC\nUser Load (0.51) SELECT  \"users\".* FROM \"users\" WHERE \"users\".\"id\" = ? LIMIT ?\n (0.4) SELECT COUNT(*) FROM \"microposts\" WHERE \"microposts\".\"user_id\" = ?\n (0.51) SELECT COUNT(*) FROM \"users\" INNER JOIN \"relationships\" ON \"users\".\"id\" = \"relationships\".\"followed_id\" WHERE \"relationships\".\"follower_id\" = ?\n (0.42) SELECT COUNT(*) FROM \"users\" INNER JOIN \"relationships\" ON \"users\".\"id\" = \"relationships\".\"follower_id\" WHERE \"relationships\".\"followed_id\" = ?\n (0.15) SELECT COUNT(*) FROM \"microposts\" WHERE (user_id = 101)'","sql_queries_count":6}

lograge-sql

  1. Ajouter lograge-sql à Gemfile

Gemfile


gem "lograge-sql"
  1. Ajouter exiger

config/initializers/lograge.rb


require 'lograge/sql/extension'
  1. Installez Gems avec bundle install '' ``
  2. Démarrage du serveur `` rails s ''

J'ai pu confirmer que sql_queries et sql_queries_count ont été ajoutés.

résultat


{"method":"GET","path":"/","format":"html","controller":"StaticPagesController","action":"home","status":200,"duration":93.2,"view":84.45,"db":0.76,"params":{},"headers":"#<ActionDispatch::Http::Headers:0x00007fedb15158a0>","view_runtime":84.45099999994454,"db_runtime":0.7550000000264845,"sql_queries":"'User Load (0.13) SELECT  \"users\".* FROM \"users\" WHERE \"users\".\"id\" = ? LIMIT ?\n (0.14) SELECT COUNT(*) FROM \"microposts\" WHERE \"microposts\".\"user_id\" = ?\n (0.14) SELECT COUNT(*) FROM \"users\" INNER JOIN \"relationships\" ON \"users\".\"id\" = \"relationships\".\"followed_id\" WHERE \"relationships\".\"follower_id\" = ?\n (0.18) SELECT COUNT(*) FROM \"users\" INNER JOIN \"relationships\" ON \"users\".\"id\" = \"relationships\".\"follower_id\" WHERE \"relationships\".\"followed_id\" = ?\n (0.16) SELECT COUNT(*) FROM \"microposts\" WHERE (user_id = 101)'","sql_queries_count":5}

Si vous souhaitez sortir le journal au format Json directement sur la sortie standard, ajoutez ce qui suit

develpment.rb


  config.lograge.logger = ActiveSupport::Logger.new(STDOUT)

Sortie de journal

production


Lograge.logger.info({ type: 'type', exception: 'exception'}.to_json)

référence de la source lograge https://github.com/roidrage/lograge/blob/1729eab7956bb95c5992e4adab251e4f93ff9280/lib/lograge.rb#L185

Recommended Posts

Journaliser la sortie au format Json en utilisant lograge / lograge-sql avec RubyOnRails
Sortie du journal Spring Boot au format json
Essayez d'utiliser l'API au format JSON en Java
Préparez la sortie du journal à l'aide de log4j dans Eclipse.
Formater JSON avec org.json
Format de la sortie du journal par Tomcat lui-même dans Tomcat 8
Authentifiez 3 paramètres ou plus au format JSON à l'aide de Spring Security
Japaneseize en utilisant i18n avec Rails
Contrôle de la sortie du journal avec Doma2
La sérialisation de org.joda.time.DateTime en JSON au format ISO8601 s'accompagne d'une milliseconde
Enregistrer la sortie dans un fichier en Java
Activez la sortie du journal vers les fichiers et la console à l'aide de log4j dans Eclipse.
Date de sortie en Java au format étendu ISO 8601
Implémentation par lots dans l'environnement RubyOnRails à l'aide de Digdag
Gérez d'énormes JSON avec Java Lambda
Bean de sortie au format JSON au printemps