xyeger 0.1.1 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3e103637d758bf273e499346bfbba7ae255126ac
4
- data.tar.gz: e09123e3824b1c8613092b91bdf86f5e822111c7
3
+ metadata.gz: e6fd8d7c1c20bb9ad7c4dc82ef8520dee72ba414
4
+ data.tar.gz: b332c76dca53975e4aac6a237ef0b3c07fcc3b5a
5
5
  SHA512:
6
- metadata.gz: 8b728fc45afff886a7d0d498e8aa1a85febc4a5d609cd0451cfbb34b2b2d440c613e70d2b073c9aab8b6729fb28b1bd3447a29401220f1968ebb6acb3dfcab68
7
- data.tar.gz: 9a254f25e04fa2e463e004d19806f0e38c7660881368669b2f5ba7eab444dc1bb35903e0c5b891ebdb1c70d556d5cd131198a7d7736c71f5d1a13a4637965b94
6
+ metadata.gz: 02c6f7aa84b8a72ea2f2dc9fcc140cc86c3f053e1ef803ca8f8a9c0d293184a35b4e7565d595080b8a17c0652cbf3c49e5d01b1e5be91f9cb7b9fa2eca853682
7
+ data.tar.gz: f6a41e97e0a35025f52913ab8e13dfa0bc248f591222795bc4b8d4cecf9ddf82f79486c8411a3ce93430d207b6f06057d4a4d64255c2cff7243ebc28c9f35a53
data/README.md CHANGED
@@ -25,13 +25,37 @@ Add environments:
25
25
  XYEGER_HOSTNAME='LoggerApplication' #f.e.: rails, sidekiq, sneakers
26
26
  ```
27
27
 
28
- Add into environment file:
28
+ Add into initializer file:
29
29
  ```ruby
30
- #config/environments/production.rb
31
- Rails.application.configure do
32
- config.xyeger.enabled = true
30
+ #config/initializers/xyeger.rb
31
+ Xyeger.configure do |config|
32
+ config.filter_parameters = Rails.application.config.filter_parameters
33
33
  end
34
34
  ```
35
+ | Formatter | Description | Options |
36
+ | ---------------------------- | ---------------- | ------- |
37
+ | `Xyeger::Formatters::Base` | | colored |
38
+ | `Xyeger::Formatters::Json` | default format | colored |
39
+ | `Xyeger::Formatters::Values` | show only values | colored |
40
+
41
+ Default options:
42
+ ```ruby
43
+ #config/initializers/xyeger.rb
44
+ Xyeger.configure do |config|
45
+ config.output = STDOUT
46
+ config.formatter = Xyeger::Formatters::Values.new
47
+ config.filter_parameters = Rails.application.config.filter_parameters
48
+ config.hostname = ENV['XYEGER_HOSTNAME']
49
+ config.pid = $$
50
+ config.app = Rails.application.class.parent_name
51
+ config.env = Rails.env
52
+ end
53
+ ```
54
+
55
+ For colored output use option `colored` (gem [Paint](https://github.com/janlelis/paint))
56
+ ```ruby
57
+ config.xyeger.formatter = Xyeger::Formatters::Values.new(colored: true)
58
+ ```
35
59
 
36
60
  ## Output results
37
61
 
@@ -104,6 +128,42 @@ Rails.logger.info('Logger message', { content: '1', params: 'b' })
104
128
  }
105
129
 
106
130
  ```
131
+
132
+ ## Grape usage
133
+ WIth ```gem 'grape_logging'```
134
+ ```ruby
135
+ class Root < Grape::API
136
+ # Message: {
137
+ # "status":200,
138
+ # "time":
139
+ # {
140
+ # "total":86.84,
141
+ # "db":15.27,
142
+ # "view":71.57
143
+ # },
144
+ # "method":"GET",
145
+ # "path":"/api/v2/tickers",
146
+ # "params":{"password":"[FILTERED]","a":"b"},
147
+ # "ip":"127.0.0.1",
148
+ # "ua":"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:54.0) Gecko/20100101 Firefox/54.0"
149
+ #}
150
+ formatter = Xyeger::Formatters::Json.new(
151
+ message: ->(message, _context) { "#{message[:method]} #{message[:path]}" },
152
+ context: ->(message, _context) { message }
153
+ )
154
+
155
+ use GrapeLogging::Middleware::RequestLogger,
156
+ logger: logger,
157
+ formatter: formatter,
158
+ include: [
159
+ GrapeLogging::Loggers::Response.new,
160
+ GrapeLogging::Loggers::FilterParameters.new,
161
+ GrapeLogging::Loggers::ClientEnv.new
162
+ ]
163
+ end
164
+ ```
165
+
166
+
107
167
  ## License
108
168
 
109
169
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,23 @@
1
+ module Xyeger
2
+ class Config
3
+ attr_accessor :output,
4
+ :formatter,
5
+ :filter_parameters,
6
+ :filter,
7
+ :hostname,
8
+ :pid,
9
+ :app,
10
+ :env
11
+
12
+ def initialize
13
+ @output = STDOUT
14
+ @formatter = Xyeger::Formatters::Json.new
15
+ @filter_parameters = Rails.application.config.filter_parameters
16
+
17
+ @hostname = ENV['XYEGER_HOSTNAME']
18
+ @pid = $$
19
+ @app = Rails.application.class.parent_name
20
+ @env = Rails.env
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,62 @@
1
+ module Xyeger
2
+ module Formatters
3
+ class Base
4
+ attr_reader :attributes, :colors
5
+
6
+ def initialize(attributes = {})
7
+ @attributes = attributes
8
+ @colors = Array.new(9) { Paint.random } if attributes[:colored]
9
+ end
10
+
11
+ def call(severity, timestamp, context, message)
12
+ message, context = prepare(message, context)
13
+
14
+ context = Xyeger.config.filter.filter(context) if Xyeger.config.filter
15
+
16
+ result = {
17
+ hostname: Xyeger.config.hostname,
18
+ pid: Xyeger.config.pid,
19
+ app: Xyeger.config.app,
20
+ env: Xyeger.config.env,
21
+ level: severity,
22
+ time: timestamp,
23
+ caller: caller_message(caller_locations),
24
+ message: message,
25
+ context: context
26
+ }
27
+
28
+ colored(result) if attributes[:colored]
29
+
30
+ result
31
+ end
32
+
33
+ private def caller_message(callers)
34
+ if location = callers.find{|x| x.path.include?(Rails.root.to_s)}
35
+ "#{location.path}:#{location.lineno}"
36
+ end
37
+ end
38
+
39
+ private def prepare(message, context)
40
+ new_message = attributes[:message].call(message, context) if attributes[:message]
41
+ new_context = attributes[:context].call(message, context) if attributes[:context]
42
+
43
+ return [new_message, new_context] if attributes[:message] || attributes[:context]
44
+
45
+ case message
46
+ when LogrageRaw
47
+ ['Lograge', message.data]
48
+ when ::StandardError
49
+ ['StandardError', { class: message.class.name, error: message.to_s }]
50
+ else
51
+ [message.to_s, context]
52
+ end
53
+ end
54
+
55
+ private def colored(result)
56
+ result.each_with_index do |(key, value), index|
57
+ result[key] = Paint[value, colors[index]]
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -1,37 +1,10 @@
1
1
  module Xyeger
2
2
  module Formatters
3
- class Json < ActiveSupport::Logger::SimpleFormatter
4
- def call(severity, timestamp, context, message)
5
- message, context = prepare(message, context)
3
+ class Json < Base
4
+ def call(*args)
5
+ result = super(*args)
6
6
 
7
- {
8
- logger: ENV.fetch('XYEGER_HOSTNAME', $0),
9
- pid: $$,
10
- app: Rails.application.class.parent_name,
11
- env: Rails.env,
12
- level: severity,
13
- time: timestamp,
14
- caller: caller_message(caller_locations),
15
- message: message,
16
- context: context
17
- }.to_json + "\n"
18
- end
19
-
20
- def caller_message(callers)
21
- if location = callers.find{|x| x.path.include?(Rails.root.to_s)}
22
- "#{location.path}:#{location.lineno}"
23
- end
24
- end
25
-
26
- def prepare(message, context)
27
- case message
28
- when LogrageRaw
29
- ['Lograge', message.data]
30
- when ::StandardError
31
- ['StandardError', { class: message.class.name, error: message.to_s }]
32
- else
33
- [message.to_s, context]
34
- end
7
+ result.compact.to_json + "\n"
35
8
  end
36
9
  end
37
10
  end
@@ -0,0 +1,11 @@
1
+ module Xyeger
2
+ module Formatters
3
+ class KeyValue < Base
4
+ def call(*args)
5
+ result = super(*args)
6
+
7
+ result.compact.to_json + "\n"
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Xyeger
2
+ module Formatters
3
+ class Values < Base
4
+ def call(*args)
5
+ result = super(*args)
6
+
7
+ result.values.join(' ') + "\n"
8
+ end
9
+ end
10
+ end
11
+ end
@@ -2,7 +2,9 @@ module Xyeger
2
2
  module Formatters
3
3
  extend ActiveSupport::Autoload
4
4
 
5
+ autoload :Base
5
6
  autoload :Json
7
+ autoload :Values
6
8
  autoload :LogrageRaw
7
9
  end
8
10
  end
data/lib/xyeger/logger.rb CHANGED
@@ -1,10 +1,5 @@
1
1
  module Xyeger
2
2
  class Logger < ActiveSupport::Logger
3
- def initialize(*args)
4
- super
5
- @formatter = Formatters::Json.new
6
- end
7
-
8
3
  Logger::Severity.constants.each do |severity|
9
4
  define_method "#{severity.downcase}" do |message, context={}|
10
5
  add(Logger::Severity.const_get(severity), message, context)
@@ -1,3 +1,3 @@
1
1
  module Xyeger
2
- VERSION = '0.1.1'.freeze
2
+ VERSION = '0.2.1'.freeze
3
3
  end
data/lib/xyeger.rb CHANGED
@@ -1,21 +1,41 @@
1
1
  require 'active_support/logger'
2
2
  require 'active_support/dependencies/autoload'
3
3
  require 'active_support/ordered_options'
4
- require "xyeger/version"
4
+ require 'paint'
5
+ require 'lograge'
6
+ require 'xyeger/version'
5
7
 
6
8
  module Xyeger
7
9
  module_function
8
10
 
9
11
  extend ActiveSupport::Autoload
10
12
 
13
+ autoload :Config
11
14
  autoload :Logger
12
15
  autoload :Formatters
13
16
 
14
- def setup(app)
17
+ class << self
18
+ attr_reader :config
19
+
20
+ def configure
21
+ @config ||= Xyeger::Config.new()
22
+
23
+ yield(@config)
24
+
25
+ if @config.filter_parameters
26
+ @config.filter ||= ActionDispatch::Http::ParameterFilter.new(@config.filter_parameters)
27
+ end
28
+ Xyeger.setup
29
+ end
30
+ end
31
+
32
+ def setup
33
+ app = Rails.application
15
34
  setup_lograge(app)
16
35
 
17
- app.config.logger = Logger.new(app.config.xyeger.output)
36
+ app.config.logger = Logger.new(config.output)
18
37
  Rails.logger = app.config.logger
38
+ Rails.logger.formatter = config.formatter
19
39
  end
20
40
 
21
41
  def setup_lograge(app)
@@ -27,5 +47,3 @@ module Xyeger
27
47
  Lograge.setup(app)
28
48
  end
29
49
  end
30
-
31
- require 'xyeger/railtie' if defined?(Rails)
data/xyeger.gemspec CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
12
12
 
13
13
  spec.summary = 'Uniq logger for project'
14
14
  spec.description = 'Uniq logger for project'
15
- spec.homepage = ''
15
+ spec.homepage = 'https://github.com/vsevolod/xyeger'
16
16
  spec.license = 'MIT'
17
17
 
18
18
  # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
@@ -32,6 +32,8 @@ Gem::Specification.new do |spec|
32
32
  spec.require_paths = ["lib"]
33
33
 
34
34
  spec.add_dependency 'lograge', '>= 0.4'
35
+ spec.add_dependency 'paint', '~> 2.0'
36
+
35
37
  spec.add_runtime_dependency 'activesupport', '>= 4'
36
38
  spec.add_runtime_dependency 'actionpack', '>= 4'
37
39
  spec.add_runtime_dependency 'railties', '>= 4'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xyeger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Avramov Vsevolod
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-30 00:00:00.000000000 Z
11
+ date: 2017-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lograge
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: paint
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: activesupport
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -124,14 +138,17 @@ files:
124
138
  - bin/console
125
139
  - bin/setup
126
140
  - lib/xyeger.rb
141
+ - lib/xyeger/config.rb
127
142
  - lib/xyeger/formatters.rb
143
+ - lib/xyeger/formatters/base.rb
128
144
  - lib/xyeger/formatters/json.rb
145
+ - lib/xyeger/formatters/key_value.rb
129
146
  - lib/xyeger/formatters/lograge_raw.rb
147
+ - lib/xyeger/formatters/values.rb
130
148
  - lib/xyeger/logger.rb
131
- - lib/xyeger/railtie.rb
132
149
  - lib/xyeger/version.rb
133
150
  - xyeger.gemspec
134
- homepage: ''
151
+ homepage: https://github.com/vsevolod/xyeger
135
152
  licenses:
136
153
  - MIT
137
154
  metadata:
@@ -1,15 +0,0 @@
1
- require 'rails/railtie'
2
- require 'action_view/log_subscriber'
3
- require 'action_controller/log_subscriber'
4
-
5
- module Xyeger
6
- class Raitie < Rails::Railtie
7
- config.xyeger = ActiveSupport::OrderedOptions.new
8
- config.xyeger.enabled = false
9
- config.xyeger.output = STDOUT
10
-
11
- config.after_initialize do |app|
12
- Xyeger.setup(app) if app.config.xyeger.enabled
13
- end
14
- end
15
- end