ougai-json-logger 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c8e366fc317576fefc223811746c6dbc59e0451d8555c18f1a35948c2faab588
4
+ data.tar.gz: 2e4a08093ad49c4aad8b191eb6bc964a68ce0291edd07b6766649047c5640232
5
+ SHA512:
6
+ metadata.gz: 2f2ccc32ea532cfe98a09c36e96246d05cba32eb23ba1c3618cac22b350ee016e15a33fe87e6f90800f59bbaeae1e8dd2a0e9003942c3e7a8956d82a463183ea
7
+ data.tar.gz: 9b911e113fb1467b85ba934b741b8bcabdd4147b176d67b7125db123eb65a1595319b6131eaa664c57058cf07b17813b44b8413dd0837d6381efefa7719efdb6
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2024 Jack.Wu
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ #Ougai Json Logger
2
+ To ensure consistency in log formatting across your Rails services, using a JSON logger is often necessary, especially when working with log services such as Kibana. This gem provides a pre-defined JSON logging format specifically tailored for Rails applications. By adding this gem to your Gemfile, you can achieve the desired log consistency across your Rails services effortlessly.
3
+
4
+
5
+ ## Usage
6
+ A JSON logger for Rails, this logger is built upon lograge and ougai. It functions as a Rails engine and works seamlessly without any additional configuration. Simply adding it as a gem is sufficient to enable its functionality.
7
+
8
+ What it does:
9
+ ```ruby
10
+ initializer(:rails_stdout_logging, before: :initialize_logger) do
11
+ Rails.application.configure do
12
+ config.logger = OugaiJson::Logger.custom_logger(name: 'Rails')
13
+
14
+ config.lograge.enabled = true
15
+ config.colorize_logging = false
16
+
17
+ config.lograge.formatter = Lograge::Formatters::Raw.new
18
+ config.lograge.custom_options = OugaiJson::Loggger::CustomOptions
19
+ end
20
+ end
21
+ ```
22
+
23
+ Log output
24
+ ```
25
+ {"@timestamp":"2024-02-28T09:07:58.098Z","message":" ActiveRecord::SchemaMigration Load (0.1ms) SELECT \"schema_migrations\".\"version\" FROM \"schema_migrations\" ORDER BY \"schema_migrations\".\"version\" ASC","log":{"level":"DEBUG","logger":"Rails"},"trace":{"id":null},"error":{},"request":{},"response":{},"process":{"id":45821},"url":{},"event":{}}
26
+ {"@timestamp":"2024-02-28T09:07:58.121Z","message":" Rendering /Users/jawu/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/railties-7.1.3.2/lib/rails/templates/rails/welcome/index.html.erb","log":{"level":"DEBUG","logger":"Rails"},"trace":{"id":null},"error":{},"request":{},"response":{},"process":{"id":45821},"url":{},"event":{}}
27
+ {"@timestamp":"2024-02-28T09:07:58.122Z","message":"Rails Request","log":{"level":"INFO","logger":"Rails"},"trace":{"id":null},"error":{},"request":{"method":"GET","body":{"content":"{\"controller\"=>\"rails/welcome\", \"action\"=>\"index\"}"}},"response":{"status_code":200},"process":{"id":45821},"url":{"path":"/"},"event":{"action":"Rails::WelcomeController#index","duration":6.79}}
28
+ {"@timestamp":"2024-02-28T09:07:59.588Z","message":" Rendering /Users/jawu/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/railties-7.1.3.2/lib/rails/templates/rails/welcome/index.html.erb","log":{"level":"DEBUG","logger":"Rails"},"trace":{"id":null},"error":{},"request":{},"response":{},"process":{"id":45821},"url":{},"event":{}}
29
+ {"@timestamp":"2024-02-28T09:07:59.588Z","message":"Rails Request","log":{"level":"INFO","logger":"Rails"},"trace":{"id":null},"error":{},"request":{"method":"GET","body":{"content":"{\"controller\"=>\"rails/welcome\", \"action\"=>\"index\"}"}},"response":{"status_code":200},"process":{"id":45821},"url":{"path":"/"},"event":{"action":"Rails::WelcomeController#index","duration":1.82}}
30
+ ```
31
+
32
+
33
+ ## Installation
34
+ Adding this gem to your Gemfile is all that is required. It is based on a Rails engine and will automatically initialize itself upon startup.
35
+
36
+ Presently, there is support for custom hooks. However, if you prefer to make modifications to the formatter in the gem, you can do so by copying it to your lib folder and making the desired changes according to your requirements.
37
+
38
+
39
+ ```ruby
40
+ gem 'ougai-json-logger'
41
+
42
+ ```
43
+
44
+ And then execute:
45
+ ```bash
46
+ $ bundle
47
+ ```
48
+
49
+
50
+ ## Contributing
51
+ Contribution directions go here.
52
+
53
+ ## License
54
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'OugaiJson::Logger'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ load 'rails/tasks/statistics.rake'
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'test'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = false
30
+ end
31
+
32
+ task default: :test
@@ -0,0 +1,2 @@
1
+ //= link_directory ../javascripts/json/logger .js
2
+ //= link_directory ../stylesheets/json/logger .css
@@ -0,0 +1,15 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file. JavaScript code in this file should be added after the last require_* statement.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require rails-ujs
14
+ //= require activestorage
15
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,7 @@
1
+ module OugaiJson
2
+ module Logger
3
+ class ApplicationController < ActionController::Base
4
+ protect_from_forgery with: :exception
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ module OugaiJson
2
+ module Logger
3
+ module ApplicationHelper
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module OugaiJson
2
+ module Logger
3
+ class ApplicationJob < ActiveJob::Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,8 @@
1
+ module OugaiJson
2
+ module Logger
3
+ class ApplicationMailer < ActionMailer::Base
4
+ default from: 'from@example.com'
5
+ layout 'mailer'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ module OugaiJson
2
+ module Logger
3
+ class ApplicationRecord < ActiveRecord::Base
4
+ self.abstract_class = true
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,16 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>OugaiJson logger</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= stylesheet_link_tag "json/logger/application", media: "all" %>
9
+ <%= javascript_include_tag "json/logger/application" %>
10
+ </head>
11
+ <body>
12
+
13
+ <%= yield %>
14
+
15
+ </body>
16
+ </html>
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveSupport
4
+ module TaggedLogging
5
+ # Patch to apply structured tags to Tagged Logs
6
+ module Formatter
7
+ def call(severity, timestamp, progname, data)
8
+ super(severity, timestamp, progname, merged_tags.merge!(data))
9
+ end
10
+
11
+ private
12
+
13
+ def merged_tags
14
+ tags = {}
15
+
16
+ current_tags.each do |tag|
17
+ tags.merge!(tag) if tag.is_a?(Hash)
18
+ end
19
+
20
+ tags
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ module Rails
27
+ module Rack
28
+ # Patch to compute structured tags for Tagged Logging
29
+ class Logger
30
+ private
31
+
32
+ def compute_tags(request)
33
+ {
34
+ trace_id: request.uuid
35
+ }
36
+ end
37
+ end
38
+ end
39
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ OugaiJson::Logger::Engine.routes.draw do
2
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OugaiJson
4
+ module Loggger
5
+ module CustomOptions
6
+ class << self
7
+ def call(event)
8
+ items = {
9
+ msg: 'Rails Request',
10
+ params: event.payload[:params].as_json.to_s,
11
+ trace_id: event.payload[:headers]['HTTP_X_REQUEST_ID'],
12
+ error: event.payload[:exception_object]
13
+ }
14
+ items.merge({ headers: event.payload[:headers] }) if event.payload[:headers].present?
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,23 @@
1
+ require 'lograge'
2
+ require 'ougai_json/logger/custom_options'
3
+
4
+ module OugaiJson
5
+ module Logger
6
+ class Engine < ::Rails::Engine
7
+ isolate_namespace OugaiJson::Logger
8
+
9
+ initializer(:rails_stdout_logging, before: :initialize_logger) do
10
+ Rails.application.configure do
11
+ config.logger = OugaiJson::Logger::Logger.custom_logger(name: 'Rails')
12
+
13
+
14
+ config.lograge.enabled = true
15
+ config.colorize_logging = false
16
+
17
+ config.lograge.formatter = Lograge::Formatters::Raw.new
18
+ config.lograge.custom_options = OugaiJson::Loggger::CustomOptions
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OugaiJson
4
+ module Logger
5
+ class Formatter < Ougai::Formatters::Bunyan
6
+ def _call(severity, time, progname, data)
7
+ payload = base_fields(data, severity, time, progname)
8
+ payload[:error] = error_fields(data)
9
+ payload[:request] = request_fields(data)
10
+ payload[:response] = response_fields(data)
11
+ payload[:process] = process_fields(data)
12
+ payload[:url] = url_fields(data)
13
+ payload[:event] = event_fields(data)
14
+ dump(payload)
15
+ end
16
+
17
+ # Ougai::Formatters::ForJson requires this be present
18
+ def convert_time(data)
19
+ data[:@timestamp] = data[:@timestamp].utc.iso8601(3)
20
+ end
21
+
22
+ private
23
+
24
+ def base_fields(data, severity, time, progname)
25
+ {
26
+ :@timestamp => time,
27
+ message: data[:msg],
28
+ log: { level: severity, logger: progname || @app_name },
29
+ trace: { id: data[:trace_id] }
30
+ }
31
+ end
32
+
33
+ def error_fields(data)
34
+ return {} unless data[:error] || data[:err]
35
+
36
+ error = data[:err] || serialize_exc(data[:error])
37
+
38
+ {
39
+ message: error[:message],
40
+ stack_trace: error[:stack],
41
+ type: error[:name]
42
+ }.compact
43
+ end
44
+
45
+ def request_fields(data)
46
+ request = data.slice(:method, :params).merge(data.fetch(:request, {}))
47
+
48
+ {
49
+ method: request[:method],
50
+ body: ({ content: request[:params] } if request.key? :params)
51
+ }.compact
52
+ end
53
+
54
+ def response_fields(data)
55
+ response = data.slice(:status).merge(data.fetch(:response, {}))
56
+
57
+ {
58
+ status_code: response[:status],
59
+ body: ({ content: response[:params] } if response.key? :params),
60
+ }.compact
61
+ end
62
+
63
+ def process_fields(_data)
64
+ { id: Process.pid }
65
+ end
66
+
67
+ # url
68
+ def url_fields(data)
69
+ {
70
+ full: data.dig(:url, :full),
71
+ path: data[:path]
72
+ }.compact
73
+ end
74
+
75
+ # controller and action
76
+ def event_fields(data)
77
+ full_action = "#{data[:controller]}##{data[:action]}"
78
+
79
+ {
80
+ action: (full_action unless full_action == '#'),
81
+ duration: data[:duration]
82
+ }.compact
83
+ end
84
+ end
85
+
86
+ end
87
+ end
@@ -0,0 +1,18 @@
1
+ module OugaiJson
2
+ module Logger
3
+ class Logger < Ougai::Logger
4
+ def initialize(logdev, **)
5
+ super
6
+ @logdev = MonoLogger::LocklessLogDevice.new(logdev)
7
+ end
8
+
9
+ def create_formatter
10
+ return OugaiJson::Logger::Formatter.new
11
+ end
12
+
13
+ def self.custom_logger(name: '', level: Rails.logger.present? ? Rails.logger.level : MonoLogger::INFO, type: :ougai)
14
+ new($stdout, level: level, progname: name)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,5 @@
1
+ module OugaiJson
2
+ module Logger
3
+ VERSION = '0.1.3'
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ require 'ougai'
2
+ require 'mono_logger'
3
+ require 'ougai_json/logger/formatter'
4
+ require "ougai_json/logger/engine"
5
+ require "ougai_json/logger/logger"
6
+
7
+ module OugaiJson
8
+ module Logger
9
+ end
10
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :json_logger do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,179 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ougai-json-logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Jack.Wu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-02-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: lograge
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mono_logger
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: ougai
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: ougai-formatters-customizable
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.0.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 1.0.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '5'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '5'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: sqlite3
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: A Ougai json looger for Rails, it is based on lograge and ougai, and
126
+ it is a rails engine, it works out of the box, just add it as gem is enough
127
+ email:
128
+ - jwu4000@outlook.com
129
+ - xuwupeng2000@gmail.com
130
+ executables: []
131
+ extensions: []
132
+ extra_rdoc_files: []
133
+ files:
134
+ - MIT-LICENSE
135
+ - README.md
136
+ - Rakefile
137
+ - app/assets/config/json_logger_manifest.js
138
+ - app/assets/javascripts/json/logger/application.js
139
+ - app/assets/stylesheets/json/logger/application.css
140
+ - app/controllers/json/logger/application_controller.rb
141
+ - app/helpers/json/logger/application_helper.rb
142
+ - app/jobs/json/logger/application_job.rb
143
+ - app/mailers/json/logger/application_mailer.rb
144
+ - app/models/json/logger/application_record.rb
145
+ - app/views/layouts/json/logger/application.html.erb
146
+ - config/initializers/tagged_logging.rb
147
+ - config/routes.rb
148
+ - lib/ougai_json/logger.rb
149
+ - lib/ougai_json/logger/custom_options.rb
150
+ - lib/ougai_json/logger/engine.rb
151
+ - lib/ougai_json/logger/formatter.rb
152
+ - lib/ougai_json/logger/logger.rb
153
+ - lib/ougai_json/logger/version.rb
154
+ - lib/tasks/json/logger_tasks.rake
155
+ homepage: https://github.com/xuwupeng2000/json-logger
156
+ licenses:
157
+ - MIT
158
+ metadata: {}
159
+ post_install_message:
160
+ rdoc_options: []
161
+ require_paths:
162
+ - lib
163
+ required_ruby_version: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ version: '2.5'
168
+ required_rubygems_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ requirements: []
174
+ rubygems_version: 3.5.3
175
+ signing_key:
176
+ specification_version: 4
177
+ summary: A Ougai based json looger for Rails, it is based on lograge and ougai, and
178
+ it is a rails engine
179
+ test_files: []