xlog 0.1.1 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9e093d33c6d4650e69e6319057c9f3c6d0657821c94fa339ec1fea3c0ba40dac
4
- data.tar.gz: 82bb2b2910223c9af42cf19a159673b7e46daa94c448f5c3c43457f8489a7e49
3
+ metadata.gz: 8863710eaab6d3927f227924b036248dd2bb416839a3df5b955f12c7a51cd4b5
4
+ data.tar.gz: 7259415eb72a926f64d91d4eb9f888476a39927a13335873aeb7e179a92fc7db
5
5
  SHA512:
6
- metadata.gz: cd086b221db39453401dd0bb5d1064cea47a6f747a3aa508f2a2060c497ffa3aefbb09b058491a2009f02bc1e86b0ff3998801c38306ce8be699009da9f272c6
7
- data.tar.gz: 3af328da784bc4f1f7c7278d4e4e07bc13e2c6094134476569f38cbe30e5ed497dbaa45f34fa2ea0fbc80d6b074823d198b9dbaad3c106dac3d537c466307d10
6
+ metadata.gz: f9da1a515bb2cf27829dc81a947a224aaba7eea61eb15d5f48a2221bbf0f06d3e077ab8d75f8cde273a9be827d6d99b1b3c9a55bbb9eacb8b65bc69abc78a0ab
7
+ data.tar.gz: e360df5da61c0d47353d95f4850bd4e7a524c29c54fcf4e0e8f0dc9a3055f78e84b32b1caf6b668bbe3cced6c4f6f36b91373bc499df4a1954a513892bb54544
data/README.md CHANGED
@@ -1,8 +1,131 @@
1
- # Xlog
1
+ # Xlog v0.1.6
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/xlog`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ Xlog - awesome logger for your Rails app. Logs everything you need in well-formatted view with timestamp, caller path and tags.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ ## Usage
6
+
7
+ ### `.info`
8
+
9
+ Log any info with `.info` method
10
+
11
+ ```ruby
12
+ Xlog.info('Some info text') # [2019-04-30 12:29:13 UTC] [ArtilesController.show] [info] Message: Some info text
13
+ ```
14
+
15
+
16
+ ### `.warn`
17
+
18
+ Log important info with `.warn` method
19
+
20
+ ```ruby
21
+ Xlog.warn('Validation failed') # [2019-04-30 12:29:13 UTC] [ArticlesController.update] [warn] Message: Validation failed
22
+ ```
23
+
24
+ ### `.error` and `.and_raise_error`
25
+
26
+ Xlog has awesome `.error` and `.and_raise_error` methods
27
+
28
+ ```ruby
29
+ def index
30
+ 10 / 0
31
+ @orders = Order.all
32
+ rescue StandardError => e
33
+ Xlog.and_raise_error(e, data: { params: params }, message: 'Some message text here')
34
+ end
35
+ ```
36
+
37
+ ...and the output
38
+
39
+ ```
40
+ [2019-04-30 11:48:33 UTC] [Admin::OrdersController.index] [error] ZeroDivisionError: divided by 0.
41
+ | Message: Some message text here
42
+ | Data: {:params=><ActionController::Parameters {"controller"=>"admin/orders", "action"=>"index"} permitted: false>}
43
+ | Error backtrace:
44
+ | /home/me/test_app/app/controllers/admin/orders_controller.rb:7:in `/'
45
+ | /home/me/test_app/app/controllers/admin/orders_controller.rb:7:in `index'
46
+ ```
47
+
48
+ The only difference between `Xlog.error` and `Xlog.and_raise_error` is that second one raises error after logging.
49
+
50
+ Log any info with `.info` method
51
+
52
+ Xlog automatically defines Rails application name and environment.
53
+ It writes logs into `log/xlog_[environement].log`
54
+
55
+ ### Data
56
+
57
+ Any log method (`.info`, `.warn`, `.error`, `.and_raise_error`) supports `data: ` - named argument. Put any object as `data: my_object`
58
+ and it will be logged as "inspected" object.
59
+
60
+
61
+ ```ruby
62
+ Xlog.info('test info', data: { my: 'hash' })
63
+ # [2020-10-01 15:41:45 +0300] [(irb):4:in `irbBinding'.irb_binding] [info] Message: test info
64
+ # | Data: {:my=>"hash"}
65
+
66
+ ```
67
+
68
+ ### Tags
69
+
70
+ As far as `.tag_logger` is deprecated as it's not thread-safe, the new tags mechanism is presented.
71
+ Any log method (`.info`, `.warn`, `.error`, `.and_raise_error`) supports `tag: ` - named argument
72
+
73
+ ```ruby
74
+ Xlog.info('Some info text', tags: 'my_custom_tag') # [2019-04-30 12:29:13 UTC] [ArtilesController.show] [info] [my_custom_tag] Message: Some info text
75
+ Xlog.warn('Validation failed', tags: %w[validation input_error]) # [2019-04-30 12:29:13 UTC] [ArticlesController.update] [warn] [validation] [input_error] Message: Validation failed
76
+ Xlog.warn(error, tags: %w[fatal]) # [2019-04-30 12:29:13 UTC] [ArticlesController.update] [error] [fatal] Message: Zero division error
77
+ ```
78
+
79
+
80
+ ### `.tag_logger` [DEPRECATED]
81
+
82
+ ```ruby
83
+ Xlog.tag_logger('custom_tag')
84
+ Xlog.info('Some text') # [2019-04-30 12:29:13 UTC] [ArtilesController.show] [info] [custom_tag] Message: Some info text
85
+ ```
86
+
87
+ Clear tags with: [DEPRECATED]
88
+
89
+ ```ruby
90
+ Xlog.clear_tags
91
+ ```
92
+
93
+ ## Middleware
94
+
95
+ From version 0.1.4 Xlog could be used as Rails middleware. It catches `StandardError` using `Xlog.and_raise_error`.
96
+
97
+ ```ruby
98
+ # /config/application.rb
99
+ module MyApp
100
+ class Application < Rails::Application
101
+ # some configs...
102
+
103
+ config.middleware.use Xlog::Middleware
104
+ end
105
+ end
106
+ ```
107
+
108
+ ## Configuration
109
+
110
+ Xlog is ready to use right out of the box, but it's possible to reconfigure default logger. Default logger is simple `Logger.new`. Add this code to `config/initializers/xlog.rb` and set any custom logger you want.
111
+
112
+ ```ruby
113
+ Xlog.configure do |config|
114
+ config.custom_logger = Logger.new(STDOUT) # or Logger.new('foo.log', 10, 1024000) or any other
115
+ end
116
+ ```
117
+
118
+ It's possible to set third-party logger like Logentries(r7rapid)
119
+
120
+ ```ruby
121
+ require 'le'
122
+
123
+ Xlog.configure do |config|
124
+ config.custom_logger = Le.new(logentries_key, 'eu', tag: true)
125
+ end
126
+ ```
127
+
128
+ Look [here](https://ruby-doc.org/stdlib-2.4.0/libdoc/logger/rdoc/Logger.html) to know more about `Logger` configuration.
6
129
 
7
130
  ## Installation
8
131
 
@@ -20,10 +143,6 @@ Or install it yourself as:
20
143
 
21
144
  $ gem install xlog
22
145
 
23
- ## Usage
24
-
25
- TODO: Write usage instructions here
26
-
27
146
  ## Development
28
147
 
29
148
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -32,7 +151,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
151
 
33
152
  ## Contributing
34
153
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/xlog. 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.
154
+ Bug reports and pull requests are welcome on GitHub at https://github.com/coaxsoft/xlog. 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.
36
155
 
37
156
  ## License
38
157
 
@@ -40,4 +159,7 @@ The gem is available as open source under the terms of the [MIT License](https:/
40
159
 
41
160
  ## Code of Conduct
42
161
 
43
- Everyone interacting in the Xlog project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/xlog/blob/master/CODE_OF_CONDUCT.md).
162
+ Everyone interacting in the Xlog project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/coaxsoft/xlog/blob/master/CODE_OF_CONDUCT.md).
163
+
164
+ ## Idea
165
+ Initially designed and created by [Orest Falchuk (OrestF)](https://github.com/OrestF)
@@ -3,29 +3,36 @@
3
3
  require 'rails'
4
4
  require_relative 'xlog/xlogger'
5
5
  require_relative 'xlog/version'
6
+ require_relative 'xlog/middleware'
6
7
 
7
8
  module Xlog
8
9
  class << self
9
10
  attr_accessor :config, :app_name, :app_root, :base_logger, :xlogger
10
11
 
11
- def tag_logger(*tags)
12
- config.xlogger.tag_logger(tags)
12
+ # rubocop:disable Layout/LineLength
13
+ def tag_logger(*_tags)
14
+ puts "\e[33mWARING: 'tag_logger' is no longer supported as it's not thread safe. Use 'tags: ' named argument instead for 'info', 'warn', 'error', 'and_raise_error'.\e[0m"
13
15
  end
16
+ # rubocop:enable Layout/LineLength
14
17
 
15
- def info(message, data: nil)
16
- config.xlogger.info(message, data)
18
+ def clear_tags
19
+ puts "\e[33mWARING: 'clear_tags' is no longer supported as it's not thread safe\e[0m"
17
20
  end
18
21
 
19
- def warn(message, data: nil)
20
- config.xlogger.warn(message, data)
22
+ def info(message, data: nil, tags: [])
23
+ config.xlogger.info(message, data, tags)
21
24
  end
22
25
 
23
- def error(e, message: nil, data: nil)
24
- config.xlogger.error(e, message, data)
26
+ def warn(message, data: nil, tags: [])
27
+ config.xlogger.warn(message, data, tags)
25
28
  end
26
29
 
27
- def and_raise_error(e, message: nil, data: nil)
28
- config.xlogger.and_raise_error(e, message, data)
30
+ def error(e, message: nil, data: nil, tags: [])
31
+ config.xlogger.error(e, message, data, tags)
32
+ end
33
+
34
+ def and_raise_error(e, message: nil, data: nil, tags: [])
35
+ config.xlogger.and_raise_error(e, message, data, tags)
29
36
  end
30
37
  end
31
38
 
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Xlog
4
+ class Middleware
5
+ def initialize(app)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ @status, @headers, @response = @app.call(env)
11
+ rescue StandardError => e
12
+ Xlog.and_raise_error(e, data: { request_params: Rack::Request.new(env).params, status: @status, headers: @headers, response: @response })
13
+ end
14
+ end
15
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Xlog
4
- VERSION = '0.1.1'
4
+ VERSION = '0.1.6'
5
5
  end
@@ -13,31 +13,27 @@ module Xlog
13
13
  @folder_names_to_remove = Dir.glob('app/*').map { |f| f.gsub('app/', '') }
14
14
  end
15
15
 
16
- def tag_logger(*tags)
17
- @tags = tags
18
- end
19
-
20
- def log(type, text)
21
- tags = [time_stamp, called_from(type), type] + Array.wrap(@tags)
16
+ def log(type, text, tags)
17
+ tags = [time_stamp, called_from(type), type] + Array.wrap(tags)
22
18
  @base_logger.tagged(tags.compact) { @base_logger.send(type, text) }
23
19
  end
24
20
 
25
- def info(message, data)
26
- log(:info, compose_log(message, data))
21
+ def info(message, data, tags)
22
+ log(:info, compose_log(message, data), tags)
27
23
  end
28
24
 
29
- def warn(message, data)
30
- log(:warn, compose_log(message, data))
25
+ def warn(message, data, tags)
26
+ log(:warn, compose_log(message, data), tags)
31
27
  end
32
28
 
33
29
  # do NOT refactor error and and_raise_error
34
- def error(e, message, data)
30
+ def error(e, message, data, tags)
35
31
  # they MUST BE NOT DRY in order to log correct backtrace
36
- log(:error, "#{e.class}: #{e.try(:message)}. \n #{compose_log(message, data)} \n Error backtrace: \n#{backtrace(e)}")
32
+ log(:error, "#{e.class}: #{e.try(:message)}. \n #{compose_log(message, data)} \n Error backtrace: \n#{backtrace(e)}", tags)
37
33
  end
38
34
 
39
- def and_raise_error(e, message, data)
40
- log(:error, "#{e.class}: #{e.try(:message)}. #{newline} #{compose_log(message, data)} #{newline} Error backtrace: #{newline} #{backtrace(e)}")
35
+ def and_raise_error(e, message, data, tags)
36
+ log(:error, "#{e.class}: #{e.try(:message)}. #{newline} #{compose_log(message, data)} #{newline} Error backtrace: #{newline} #{backtrace(e)}", tags)
41
37
  message.present? ? raise(e, message) : raise(e)
42
38
  end
43
39
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xlog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - OrestF
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-05-02 00:00:00.000000000 Z
11
+ date: 2020-10-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.17'
19
+ version: 2.1.4
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.17'
26
+ version: 2.1.4
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '13.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '13.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -66,7 +66,22 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
- description: Xlog is just awesome logger
69
+ - !ruby/object:Gem::Dependency
70
+ name: rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Xlog - awesome logger for your Rails app. Logs everything you need in
84
+ well-formatted view with timestamp, caller path and tags.
70
85
  email:
71
86
  - falchuko@gmail.com
72
87
  executables: []
@@ -75,9 +90,10 @@ extra_rdoc_files: []
75
90
  files:
76
91
  - README.md
77
92
  - lib/xlog.rb
93
+ - lib/xlog/middleware.rb
78
94
  - lib/xlog/version.rb
79
95
  - lib/xlog/xlogger.rb
80
- homepage: https://github.com/OrestF/xlog
96
+ homepage: https://github.com/coaxsoft/xlog
81
97
  licenses:
82
98
  - MIT
83
99
  metadata: {}
@@ -96,8 +112,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
112
  - !ruby/object:Gem::Version
97
113
  version: '0'
98
114
  requirements: []
99
- rubygems_version: 3.0.1
115
+ rubygems_version: 3.1.2
100
116
  signing_key:
101
117
  specification_version: 4
102
- summary: Xlog is just awesome logger
118
+ summary: Xlog - awesome logger for your Rails app. Logs everything you need in well-formatted
119
+ view with timestamp, caller path and tags.
103
120
  test_files: []