xlog 0.1.0
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 +7 -0
- data/README.md +92 -0
- data/lib/xlog/version.rb +5 -0
- data/lib/xlog/xlogger.rb +88 -0
- data/lib/xlog.rb +50 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: fd95d2da4868dd8be558341be8c5e8fb6506f1d6e7fb353d22fbc58d1db84c4c
|
4
|
+
data.tar.gz: 334ff24b034fe2c92ed2915b7be4aefae461d9f4d65fa8422a88e005bf83fa86
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 229ac2f213663efeab5e08303df6530c6023c37b9b628d3cb4ec6d9aba452b0833dedba73179e64fae1c20efea10d471447740eebb7fcfbd9c1125755d3da007
|
7
|
+
data.tar.gz: b87ccd85daeab222e3ecdf5d6cee9b279c9995fcf2661a213ae553b6be5d21a01d8df653f2488ba3f4c60fb3716c3e7c0a766a6511a2314494fd3944fffa5b85
|
data/README.md
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
# Xlog v0.0.1
|
2
|
+
|
3
|
+
Xlog - awesome logger for your Rails app. Logs everything you need in well-formatted view with timestamp, caller path and tags.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
Log any info with `.info` method
|
7
|
+
```ruby
|
8
|
+
Xlog.info('Some info text') # [2019-04-30 12:29:13 UTC] [ArtilesController.show] [info] Message: Some info text
|
9
|
+
```
|
10
|
+
Log important info with `.warn` method
|
11
|
+
```ruby
|
12
|
+
Xlog.warn('Validation failed') # [2019-04-30 12:29:13 UTC] [ArticlesController.update] [warn] Message: Validation failed
|
13
|
+
```
|
14
|
+
|
15
|
+
Xlog has awesome `.error` and `.and_raise_error` methods
|
16
|
+
```ruby
|
17
|
+
def index
|
18
|
+
10 / 0
|
19
|
+
@orders = Order.all
|
20
|
+
rescue StandardError => e
|
21
|
+
Xlog.and_raise_error(e, data: { params: params }, message: 'Some message text here')
|
22
|
+
end
|
23
|
+
```
|
24
|
+
...and the output
|
25
|
+
```
|
26
|
+
[2019-04-30 11:48:33 UTC] [Admin::OrdersController.index] [error] ZeroDivisionError: divided by 0.
|
27
|
+
| Message: Some message text here
|
28
|
+
| Data: {:params=><ActionController::Parameters {"controller"=>"admin/orders", "action"=>"index"} permitted: false>}
|
29
|
+
| Error backtrace:
|
30
|
+
| /home/me/test_app/app/controllers/admin/orders_controller.rb:7:in `/'
|
31
|
+
| /home/me/test_app/app/controllers/admin/orders_controller.rb:7:in `index'
|
32
|
+
```
|
33
|
+
The only difference between `Xlog.error` and `Xlog.and_raise_error` is that second one raises error after logging.
|
34
|
+
|
35
|
+
Xlog automatically defines Rails application name and environment.
|
36
|
+
It writes logs into `log/xlog_[environement].log`
|
37
|
+
|
38
|
+
## Configuration
|
39
|
+
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.
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
Xlog.configure do |config|
|
43
|
+
config.custom_logger = Logger.new(STDOUT) # or Logger.new('foo.log', 10, 1024000) or any other
|
44
|
+
end
|
45
|
+
```
|
46
|
+
It's possible to set third-party logger like Logentries(r7rapid)
|
47
|
+
```ruby
|
48
|
+
require 'le'
|
49
|
+
|
50
|
+
Xlog.configure do |config|
|
51
|
+
config.custom_logger = Le.new(logentries_key, 'eu', tag: true)
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
Look [here](https://ruby-doc.org/stdlib-2.4.0/libdoc/logger/rdoc/Logger.html) to know more about `Logger` configuration.
|
56
|
+
|
57
|
+
## Installation
|
58
|
+
|
59
|
+
Add this line to your application's Gemfile:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
gem 'xlog'
|
63
|
+
```
|
64
|
+
|
65
|
+
And then execute:
|
66
|
+
|
67
|
+
$ bundle
|
68
|
+
|
69
|
+
Or install it yourself as:
|
70
|
+
|
71
|
+
$ gem install xlog
|
72
|
+
|
73
|
+
## Development
|
74
|
+
|
75
|
+
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.
|
76
|
+
|
77
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
78
|
+
|
79
|
+
## Contributing
|
80
|
+
|
81
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/OrestF/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.
|
82
|
+
|
83
|
+
## License
|
84
|
+
|
85
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
86
|
+
|
87
|
+
## Code of Conduct
|
88
|
+
|
89
|
+
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/OrestF/xlog/blob/master/CODE_OF_CONDUCT.md).
|
90
|
+
|
91
|
+
## Idea
|
92
|
+
Initially designed and created by [Orest Falchuk (OrestF)](https://github.com/OrestF)
|
data/lib/xlog/version.rb
ADDED
data/lib/xlog/xlogger.rb
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Xlog
|
4
|
+
class Xlogger
|
5
|
+
include Singleton
|
6
|
+
|
7
|
+
attr_accessor :app_name, :app_root, :base_logger
|
8
|
+
def initialize
|
9
|
+
@base_logger = ActiveSupport::TaggedLogging.new(Logger.new("log/xlog_#{Rails.env}.log"))
|
10
|
+
|
11
|
+
@app_name = Rails.application.class.to_s.split('::')[0].underscore
|
12
|
+
@app_root = Rails.root.to_s
|
13
|
+
@folder_names_to_remove = Dir.glob('app/*').map { |f| f.gsub('app/', '') }
|
14
|
+
end
|
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)
|
22
|
+
@base_logger.tagged(tags.compact) { @base_logger.send(type, text) }
|
23
|
+
end
|
24
|
+
|
25
|
+
def info(message, data)
|
26
|
+
log(:info, compose_log(message, data))
|
27
|
+
end
|
28
|
+
|
29
|
+
def warn(message, data)
|
30
|
+
log(:warn, compose_log(message, data))
|
31
|
+
end
|
32
|
+
|
33
|
+
# do NOT refactor error and and_raise_error
|
34
|
+
def error(e, message, data)
|
35
|
+
# 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)}")
|
37
|
+
end
|
38
|
+
|
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)}")
|
41
|
+
message.present? ? raise(e, message) : raise(e)
|
42
|
+
end
|
43
|
+
|
44
|
+
def custom_logger=(logger)
|
45
|
+
@base_logger = ActiveSupport::TaggedLogging.new(logger)
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def newline
|
51
|
+
"\n |"
|
52
|
+
end
|
53
|
+
|
54
|
+
def time_stamp
|
55
|
+
Time.zone&.now || Time.current
|
56
|
+
end
|
57
|
+
|
58
|
+
def compose_log(message, data)
|
59
|
+
message = "Message: #{message}"
|
60
|
+
message += "#{newline} Data: #{data.try(:inspect)}" if data.present?
|
61
|
+
message
|
62
|
+
end
|
63
|
+
|
64
|
+
def backtrace(e)
|
65
|
+
backtrace_cleaner.clean(e.try(:backtrace)).try(:join, "#{newline} ")
|
66
|
+
end
|
67
|
+
|
68
|
+
def backtrace_cleaner
|
69
|
+
return @backtrace_cleaner if @backtrace_cleaner.present?
|
70
|
+
|
71
|
+
bc = ActiveSupport::BacktraceCleaner.new
|
72
|
+
bc.add_filter { |line| line.gsub(app_root, '') }
|
73
|
+
bc.add_silencer { |line| line =~ /puma|rubygems|gems/ }
|
74
|
+
@backtrace_cleaner = bc
|
75
|
+
end
|
76
|
+
|
77
|
+
def called_from(type)
|
78
|
+
caller_position = type == :error ? 5 : 4
|
79
|
+
caller(caller_position..caller_position)[0]
|
80
|
+
.split("/#{app_name}/*")[-1]
|
81
|
+
.split('.rb')[0]
|
82
|
+
.remove(*@folder_names_to_remove)
|
83
|
+
.split('//')[-1]
|
84
|
+
.camelize
|
85
|
+
.concat(".#{caller_locations(caller_position, caller_position + 1)[0].label}")
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
data/lib/xlog.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails'
|
4
|
+
require 'xlog/xlogger'
|
5
|
+
require 'xlog/version'
|
6
|
+
|
7
|
+
module Xlog
|
8
|
+
class << self
|
9
|
+
attr_accessor :config, :app_name, :app_root, :base_logger, :xlogger
|
10
|
+
|
11
|
+
def tag_logger(*tags)
|
12
|
+
config.xlogger.tag_logger(tags)
|
13
|
+
end
|
14
|
+
|
15
|
+
def info(message, data: nil)
|
16
|
+
config.xlogger.info(message, data)
|
17
|
+
end
|
18
|
+
|
19
|
+
def warn(message, data: nil)
|
20
|
+
config.xlogger.warn(message, data)
|
21
|
+
end
|
22
|
+
|
23
|
+
def error(e, message: nil, data: nil)
|
24
|
+
config.xlogger.error(e, message, data)
|
25
|
+
end
|
26
|
+
|
27
|
+
def and_raise_error(e, message: nil, data: nil)
|
28
|
+
config.xlogger.and_raise_error(e, message, data)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.configure
|
33
|
+
self.config ||= Config.new
|
34
|
+
yield(config)
|
35
|
+
end
|
36
|
+
|
37
|
+
class Config
|
38
|
+
attr_accessor :xlogger
|
39
|
+
|
40
|
+
def initialize
|
41
|
+
@xlogger = Xlogger.instance
|
42
|
+
end
|
43
|
+
|
44
|
+
def custom_logger=(logger)
|
45
|
+
xlogger.custom_logger = logger
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
configure {}
|
50
|
+
end
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: xlog
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- OrestF
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-04-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.17'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.17'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: faker
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Xlog is just awesome logger
|
70
|
+
email:
|
71
|
+
- falchuko@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- README.md
|
77
|
+
- lib/xlog.rb
|
78
|
+
- lib/xlog/version.rb
|
79
|
+
- lib/xlog/xlogger.rb
|
80
|
+
homepage: https://github.com/OrestF/xlog
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
metadata: {}
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubygems_version: 3.0.1
|
100
|
+
signing_key:
|
101
|
+
specification_version: 4
|
102
|
+
summary: Xlog is just awesome logger
|
103
|
+
test_files: []
|