xlog 0.1.5 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +59 -5
- data/lib/xlog/middleware.rb +1 -1
- data/lib/xlog/version.rb +1 -1
- data/lib/xlog/xlogger.rb +13 -20
- data/lib/xlog.rb +15 -11
- metadata +28 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 460bcad515dfdef4370712e91ac0110afe3fd1884386abcb0f464bd2cc9606d2
|
4
|
+
data.tar.gz: b0d8baf9e37cae99ed51cfa5afbe99152d6112bdc3ae6be2e11431419d6f20f6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b3003bd928eb552f2038704753e0f5490aab2d6efa271ad2b1a2db56f2946a443d9cd1a722cfae45dc21f21a469ff9797e3758432a337ef7cd9f5ce16da0715
|
7
|
+
data.tar.gz: 6688c324b9a71b430df3942018bf28fa06c2b667d0103edc7ca8e69c39bff0c4d1cd5d2562a687ac9001665635c783a05873e3f2aaeb82fcea7ad44aa66806f9
|
data/README.md
CHANGED
@@ -1,19 +1,35 @@
|
|
1
|
-
# Xlog v0.1.
|
1
|
+
# Xlog v0.1.8 - [changelog](https://github.com/coaxsoft/xlog/blob/master/CHANGELOG.md)
|
2
2
|
|
3
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
|
+
### Tested on second ruby versions:
|
6
|
+
- 2.7.3
|
7
|
+
- 3.0.0
|
8
|
+
- 3.1.1
|
9
|
+
|
5
10
|
## Usage
|
6
|
-
|
11
|
+
|
12
|
+
### `.info`
|
13
|
+
|
7
14
|
Log any info with `.info` method
|
15
|
+
|
8
16
|
```ruby
|
9
17
|
Xlog.info('Some info text') # [2019-04-30 12:29:13 UTC] [ArtilesController.show] [info] Message: Some info text
|
10
18
|
```
|
19
|
+
|
20
|
+
|
21
|
+
### `.warn`
|
22
|
+
|
11
23
|
Log important info with `.warn` method
|
24
|
+
|
12
25
|
```ruby
|
13
26
|
Xlog.warn('Validation failed') # [2019-04-30 12:29:13 UTC] [ArticlesController.update] [warn] Message: Validation failed
|
14
27
|
```
|
15
28
|
|
29
|
+
### `.error` and `.and_raise_error`
|
30
|
+
|
16
31
|
Xlog has awesome `.error` and `.and_raise_error` methods
|
32
|
+
|
17
33
|
```ruby
|
18
34
|
def index
|
19
35
|
10 / 0
|
@@ -22,7 +38,9 @@ def index
|
|
22
38
|
Xlog.and_raise_error(e, data: { params: params }, message: 'Some message text here')
|
23
39
|
end
|
24
40
|
```
|
41
|
+
|
25
42
|
...and the output
|
43
|
+
|
26
44
|
```
|
27
45
|
[2019-04-30 11:48:33 UTC] [Admin::OrdersController.index] [error] ZeroDivisionError: divided by 0.
|
28
46
|
| Message: Some message text here
|
@@ -31,23 +49,56 @@ end
|
|
31
49
|
| /home/me/test_app/app/controllers/admin/orders_controller.rb:7:in `/'
|
32
50
|
| /home/me/test_app/app/controllers/admin/orders_controller.rb:7:in `index'
|
33
51
|
```
|
52
|
+
|
34
53
|
The only difference between `Xlog.error` and `Xlog.and_raise_error` is that second one raises error after logging.
|
35
54
|
|
55
|
+
Log any info with `.info` method
|
56
|
+
|
36
57
|
Xlog automatically defines Rails application name and environment.
|
37
58
|
It writes logs into `log/xlog_[environement].log`
|
38
59
|
|
39
|
-
|
60
|
+
### Data
|
61
|
+
|
62
|
+
Any log method (`.info`, `.warn`, `.error`, `.and_raise_error`) supports `data: ` - named argument. Put any object as `data: my_object`
|
63
|
+
and it will be logged as "inspected" object.
|
64
|
+
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
Xlog.info('test info', data: { my: 'hash' })
|
68
|
+
# [2020-10-01 15:41:45 +0300] [(irb):4:in `irbBinding'.irb_binding] [info] Message: test info
|
69
|
+
# | Data: {:my=>"hash"}
|
70
|
+
|
71
|
+
```
|
72
|
+
|
73
|
+
### Tags
|
74
|
+
|
75
|
+
As far as `.tag_logger` is deprecated as it's not thread-safe, the new tags mechanism is presented.
|
76
|
+
Any log method (`.info`, `.warn`, `.error`, `.and_raise_error`) supports `tag: ` - named argument
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
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
|
80
|
+
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
|
81
|
+
Xlog.warn(error, tags: %w[fatal]) # [2019-04-30 12:29:13 UTC] [ArticlesController.update] [error] [fatal] Message: Zero division error
|
82
|
+
```
|
83
|
+
|
84
|
+
|
85
|
+
### `.tag_logger` [DEPRECATED]
|
86
|
+
|
40
87
|
```ruby
|
41
88
|
Xlog.tag_logger('custom_tag')
|
42
89
|
Xlog.info('Some text') # [2019-04-30 12:29:13 UTC] [ArtilesController.show] [info] [custom_tag] Message: Some info text
|
43
90
|
```
|
44
91
|
|
45
|
-
Clear tags with:
|
92
|
+
Clear tags with: [DEPRECATED]
|
93
|
+
|
46
94
|
```ruby
|
47
95
|
Xlog.clear_tags
|
48
96
|
```
|
49
|
-
|
97
|
+
|
98
|
+
## Middleware
|
99
|
+
|
50
100
|
From version 0.1.4 Xlog could be used as Rails middleware. It catches `StandardError` using `Xlog.and_raise_error`.
|
101
|
+
|
51
102
|
```ruby
|
52
103
|
# /config/application.rb
|
53
104
|
module MyApp
|
@@ -60,6 +111,7 @@ From version 0.1.4 Xlog could be used as Rails middleware. It catches `StandardE
|
|
60
111
|
```
|
61
112
|
|
62
113
|
## Configuration
|
114
|
+
|
63
115
|
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.
|
64
116
|
|
65
117
|
```ruby
|
@@ -67,7 +119,9 @@ Xlog.configure do |config|
|
|
67
119
|
config.custom_logger = Logger.new(STDOUT) # or Logger.new('foo.log', 10, 1024000) or any other
|
68
120
|
end
|
69
121
|
```
|
122
|
+
|
70
123
|
It's possible to set third-party logger like Logentries(r7rapid)
|
124
|
+
|
71
125
|
```ruby
|
72
126
|
require 'le'
|
73
127
|
|
data/lib/xlog/middleware.rb
CHANGED
@@ -9,7 +9,7 @@ module Xlog
|
|
9
9
|
def call(env)
|
10
10
|
@status, @headers, @response = @app.call(env)
|
11
11
|
rescue StandardError => e
|
12
|
-
Xlog.and_raise_error(e, data: { request_params: Rack::Request.new(env).params, status: @status, headers: @headers
|
12
|
+
Xlog.and_raise_error(e, data: { request_params: Rack::Request.new(env).params, status: @status, headers: @headers })
|
13
13
|
end
|
14
14
|
end
|
15
15
|
end
|
data/lib/xlog/version.rb
CHANGED
data/lib/xlog/xlogger.rb
CHANGED
@@ -5,43 +5,36 @@ module Xlog
|
|
5
5
|
include Singleton
|
6
6
|
|
7
7
|
attr_accessor :app_name, :app_root, :base_logger
|
8
|
+
|
8
9
|
def initialize
|
9
|
-
@base_logger = ActiveSupport::TaggedLogging.new(Logger.new("log/xlog_#{Rails.env}.log"))
|
10
|
+
@base_logger = ActiveSupport::TaggedLogging.new(Logger.new("log/xlog_#{Rails.env}.log", 'weekly'))
|
10
11
|
|
11
12
|
@app_name = Rails.application.class.to_s.split('::')[0].underscore
|
12
13
|
@app_root = Rails.root.to_s
|
13
14
|
@folder_names_to_remove = Dir.glob('app/*').map { |f| f.gsub('app/', '') }
|
14
15
|
end
|
15
16
|
|
16
|
-
def
|
17
|
-
|
18
|
-
end
|
19
|
-
|
20
|
-
def clear_tags
|
21
|
-
@tags = nil
|
22
|
-
end
|
23
|
-
|
24
|
-
def log(type, text)
|
25
|
-
tags = [time_stamp, called_from(type), type] + Array.wrap(@tags)
|
17
|
+
def log(type, text, tags)
|
18
|
+
tags = [time_stamp, called_from(type), type] + Array.wrap(tags)
|
26
19
|
@base_logger.tagged(tags.compact) { @base_logger.send(type, text) }
|
27
20
|
end
|
28
21
|
|
29
|
-
def info(message, data)
|
30
|
-
log(:info, compose_log(message, data))
|
22
|
+
def info(message, data, tags)
|
23
|
+
log(:info, compose_log(message, data), tags)
|
31
24
|
end
|
32
25
|
|
33
|
-
def warn(message, data)
|
34
|
-
log(:warn, compose_log(message, data))
|
26
|
+
def warn(message, data, tags)
|
27
|
+
log(:warn, compose_log(message, data), tags)
|
35
28
|
end
|
36
29
|
|
37
30
|
# do NOT refactor error and and_raise_error
|
38
|
-
def error(e, message, data)
|
31
|
+
def error(e, message, data, tags)
|
39
32
|
# they MUST BE NOT DRY in order to log correct backtrace
|
40
|
-
log(:error, "#{e.class}: #{e.try(:message)}. \n #{compose_log(message, data)} \n Error backtrace: \n#{backtrace(e)}")
|
33
|
+
log(:error, "#{e.class}: #{e.try(:message)}. \n #{compose_log(message, data)} \n Error backtrace: \n#{backtrace(e)}", tags)
|
41
34
|
end
|
42
35
|
|
43
|
-
def and_raise_error(e, message, data)
|
44
|
-
log(:error, "#{e.class}: #{e.try(:message)}. #{newline} #{compose_log(message, data)} #{newline} Error backtrace: #{newline} #{backtrace(e)}")
|
36
|
+
def and_raise_error(e, message, data, tags)
|
37
|
+
log(:error, "#{e.class}: #{e.try(:message)}. #{newline} #{compose_log(message, data)} #{newline} Error backtrace: #{newline} #{backtrace(e)}", tags)
|
45
38
|
message.present? ? raise(e, message) : raise(e)
|
46
39
|
end
|
47
40
|
|
@@ -56,7 +49,7 @@ module Xlog
|
|
56
49
|
end
|
57
50
|
|
58
51
|
def time_stamp
|
59
|
-
Time.
|
52
|
+
Time.current
|
60
53
|
end
|
61
54
|
|
62
55
|
def compose_log(message, data)
|
data/lib/xlog.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'rails'
|
4
|
+
require 'active_support/core_ext/time/zones'
|
5
|
+
require 'active_support/core_ext/time/calculations'
|
4
6
|
require_relative 'xlog/xlogger'
|
5
7
|
require_relative 'xlog/version'
|
6
8
|
require_relative 'xlog/middleware'
|
@@ -9,28 +11,30 @@ module Xlog
|
|
9
11
|
class << self
|
10
12
|
attr_accessor :config, :app_name, :app_root, :base_logger, :xlogger
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
+
# rubocop:disable Layout/LineLength
|
15
|
+
def tag_logger(*_tags)
|
16
|
+
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"
|
14
17
|
end
|
18
|
+
# rubocop:enable Layout/LineLength
|
15
19
|
|
16
20
|
def clear_tags
|
17
|
-
|
21
|
+
puts "\e[33mWARING: 'clear_tags' is no longer supported as it's not thread safe\e[0m"
|
18
22
|
end
|
19
23
|
|
20
|
-
def info(message, data: nil)
|
21
|
-
config.xlogger.info(message, data)
|
24
|
+
def info(message, data: nil, tags: [])
|
25
|
+
config.xlogger.info(message, data, tags)
|
22
26
|
end
|
23
27
|
|
24
|
-
def warn(message, data: nil)
|
25
|
-
config.xlogger.warn(message, data)
|
28
|
+
def warn(message, data: nil, tags: [])
|
29
|
+
config.xlogger.warn(message, data, tags)
|
26
30
|
end
|
27
31
|
|
28
|
-
def error(e, message: nil, data: nil)
|
29
|
-
config.xlogger.error(e, message, data)
|
32
|
+
def error(e, message: nil, data: nil, tags: [])
|
33
|
+
config.xlogger.error(e, message, data, tags)
|
30
34
|
end
|
31
35
|
|
32
|
-
def and_raise_error(e, message: nil, data: nil)
|
33
|
-
config.xlogger.and_raise_error(e, message, data)
|
36
|
+
def and_raise_error(e, message: nil, data: nil, tags: [])
|
37
|
+
config.xlogger.and_raise_error(e, message, data, tags)
|
34
38
|
end
|
35
39
|
end
|
36
40
|
|
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.
|
4
|
+
version: 0.1.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- OrestF
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-03-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -16,14 +16,20 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 2.
|
19
|
+
version: '2.3'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 2.3.8
|
20
23
|
type: :development
|
21
24
|
prerelease: false
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
23
26
|
requirements:
|
24
27
|
- - "~>"
|
25
28
|
- !ruby/object:Gem::Version
|
26
|
-
version: 2.
|
29
|
+
version: '2.3'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 2.3.8
|
27
33
|
- !ruby/object:Gem::Dependency
|
28
34
|
name: rake
|
29
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -80,6 +86,20 @@ dependencies:
|
|
80
86
|
- - ">="
|
81
87
|
- !ruby/object:Gem::Version
|
82
88
|
version: '0'
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: byebug
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
type: :development
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
83
103
|
description: Xlog - awesome logger for your Rails app. Logs everything you need in
|
84
104
|
well-formatted view with timestamp, caller path and tags.
|
85
105
|
email:
|
@@ -97,7 +117,7 @@ homepage: https://github.com/coaxsoft/xlog
|
|
97
117
|
licenses:
|
98
118
|
- MIT
|
99
119
|
metadata: {}
|
100
|
-
post_install_message:
|
120
|
+
post_install_message:
|
101
121
|
rdoc_options: []
|
102
122
|
require_paths:
|
103
123
|
- lib
|
@@ -112,8 +132,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
112
132
|
- !ruby/object:Gem::Version
|
113
133
|
version: '0'
|
114
134
|
requirements: []
|
115
|
-
rubygems_version: 3.1.
|
116
|
-
signing_key:
|
135
|
+
rubygems_version: 3.1.6
|
136
|
+
signing_key:
|
117
137
|
specification_version: 4
|
118
138
|
summary: Xlog - awesome logger for your Rails app. Logs everything you need in well-formatted
|
119
139
|
view with timestamp, caller path and tags.
|