loggerman 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/.gitignore +11 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +133 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/loggerman.rb +62 -0
- data/lib/loggerman/formatters/formatter.rb +37 -0
- data/lib/loggerman/formatters/json_formatter.rb +34 -0
- data/lib/loggerman/formatters/key_value_formatter.rb +36 -0
- data/lib/loggerman/logger.rb +28 -0
- data/lib/loggerman/logger_formatter.rb +12 -0
- data/lib/loggerman/railtie.rb +12 -0
- data/lib/loggerman/version.rb +3 -0
- data/log/.keep +0 -0
- data/loggerman.gemspec +37 -0
- metadata +159 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 35a53f3747e6d2486cca5b13ee660a9ec98b7a59
|
|
4
|
+
data.tar.gz: d809423bcfa56d372ce3ed0651bda21f87aaed4c
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: c1a6c6589f2b1fcab505170a47bf1527b9bb5c389af29576075fb0fafc3a7620032bb32faa2988da4584ba7773c24348edcacb67ab4e4622693ed98f7a216f78
|
|
7
|
+
data.tar.gz: ef091f4e4cd5e7d52c03ae1bafcee6b15a950f862fa401638a89cd3fd22db35930b3ef74004a6e1a5126287d874e1f44c78df850dbdaf8c374d1d60db32740b7
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016 Daniel Alvarenga
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# Loggerman
|
|
2
|
+
|
|
3
|
+
Loggerman format rails logs and add a custom logger to create log files by level.
|
|
4
|
+
|
|
5
|
+
Json format example:
|
|
6
|
+
|
|
7
|
+
```ruby
|
|
8
|
+
{"INFO":{"time":"2016-09-20 13:43:52","src":"127.0.0.1","host":"de-sp-d07","system":"MySystem","details":"In my code something happens"}}
|
|
9
|
+
{"INFO":{"time":"2016-09-20 13:43:52","src":"127.0.0.1","host":"de-sp-d07","system":"MySystem","key01":"value 01","key02":"value02","key03":{"key031":"value031","key032":"value032"}}}
|
|
10
|
+
{"INFO":{"time":"2016-09-20 13:43:52","src":"127.0.0.1","host":"de-sp-d07","system":"MySystem","details":"Loggerman::Logger"}}
|
|
11
|
+
{"ERROR":{"time":"2016-09-20 13:43:52","src":"127.0.0.1","host":"de-sp-d07","system":"MySystem","exception":{"class":"KeyError","message":"key not found: :anything","location":{},"backtrace":["/home/dev/dev/ruby-projects/loggerman/spec/formatters/json_formatter_spec.rb:28:in `fetch'","/home/dev/dev/ruby-projects/loggerman/spec/formatters/json_formatter_spec.rb:28:in `block (3 levels) in \u003ctop (required)\u003e'","/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/example.rb:254:in `instance_exec'","/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/example.rb:254:in `block in run'","/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/example.rb:496:in `block in with_around_and_singleton_context_hooks'","/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/example.rb:453:in `block in with_around_example_hooks'","/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/hooks.rb:471:in `block in run'","/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/hooks.rb:609:in `run_around_example_hooks_for'","/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/hooks.rb:471:in `run'","/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/example.rb:453:in `with_around_example_hooks'","/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/example.rb:496:in `with_around_and_singleton_context_hooks'","/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/example.rb:251:in `run'","/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/example_group.rb:627:in `block in run_examples'","/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/example_group.rb:623:in `map'","/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/example_group.rb:623:in `run_examples'","/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/example_group.rb:589:in `run'","/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/example_group.rb:590:in `block in run'","/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/example_group.rb:590:in `map'","/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/example_group.rb:590:in `run'","/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/runner.rb:113:in `block (3 levels) in run_specs'","/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/runner.rb:113:in `map'","/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/runner.rb:113:in `block (2 levels) in run_specs'","/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/configuration.rb:1836:in `with_suite_hooks'","/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/runner.rb:112:in `block in run_specs'","/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/reporter.rb:77:in `report'","/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/runner.rb:111:in `run_specs'","/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/runner.rb:87:in `run'","/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/runner.rb:71:in `run'","/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/runner.rb:45:in `invoke'","/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/exe/rspec:4:in `\u003ctop (required)\u003e'","/home/dev/.rvm/gems/ruby-2.3.1/bin/rspec:23:in `load'","/home/dev/.rvm/gems/ruby-2.3.1/bin/rspec:23:in `\u003cmain\u003e'","/home/dev/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `eval'","/home/dev/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `\u003cmain\u003e'"]}}}
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Key_value format example:
|
|
15
|
+
|
|
16
|
+
```ruby
|
|
17
|
+
2016-09-20 13:43:52|INFO|src=127.0.0.1|host=de-sp-d07|system=MySystem|details=In my code something happens|
|
|
18
|
+
2016-09-20 13:43:52|INFO|src=127.0.0.1|host=de-sp-d07|system=MySystem|key01="value 01"|key02="value02"|key03={:key031=>"value031", :key032=>"value032"}|
|
|
19
|
+
2016-09-20 13:43:52|INFO|src=127.0.0.1|host=de-sp-d07|system=MySystem|details=Loggerman::Logger|
|
|
20
|
+
2016-09-20 13:43:52|ERROR|src=127.0.0.1|host=de-sp-d07|system=MySystem|exception=KeyError|message=key not found: :anything|location=/home/dev/dev/ruby-projects/loggerman/spec/formatters/key_value_formatter_spec.rb:28:in `fetch'|
|
|
21
|
+
/home/dev/dev/ruby-projects/loggerman/spec/formatters/key_value_formatter_spec.rb:28:in `fetch'
|
|
22
|
+
/home/dev/dev/ruby-projects/loggerman/spec/formatters/key_value_formatter_spec.rb:28:in `block (3 levels) in <top (required)>'
|
|
23
|
+
/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/example.rb:254:in `instance_exec'
|
|
24
|
+
/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/example.rb:254:in `block in run'
|
|
25
|
+
/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/example.rb:496:in `block in with_around_and_singleton_context_hooks'
|
|
26
|
+
/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/example.rb:453:in `block in with_around_example_hooks'
|
|
27
|
+
/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/hooks.rb:471:in `block in run'
|
|
28
|
+
/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/hooks.rb:609:in `run_around_example_hooks_for'
|
|
29
|
+
/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/hooks.rb:471:in `run'
|
|
30
|
+
/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/example.rb:453:in `with_around_example_hooks'
|
|
31
|
+
/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/example.rb:496:in `with_around_and_singleton_context_hooks'
|
|
32
|
+
/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/example.rb:251:in `run'
|
|
33
|
+
/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/example_group.rb:627:in `block in run_examples'
|
|
34
|
+
/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/example_group.rb:623:in `map'
|
|
35
|
+
/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/example_group.rb:623:in `run_examples'
|
|
36
|
+
/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/example_group.rb:589:in `run'
|
|
37
|
+
/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/example_group.rb:590:in `block in run'
|
|
38
|
+
/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/example_group.rb:590:in `map'
|
|
39
|
+
/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/example_group.rb:590:in `run'
|
|
40
|
+
/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/runner.rb:113:in `block (3 levels) in run_specs'
|
|
41
|
+
/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/runner.rb:113:in `map'
|
|
42
|
+
/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/runner.rb:113:in `block (2 levels) in run_specs'
|
|
43
|
+
/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/configuration.rb:1836:in `with_suite_hooks'
|
|
44
|
+
/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/runner.rb:112:in `block in run_specs'
|
|
45
|
+
/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/reporter.rb:77:in `report'
|
|
46
|
+
/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/runner.rb:111:in `run_specs'
|
|
47
|
+
/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/runner.rb:87:in `run'
|
|
48
|
+
/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/runner.rb:71:in `run'
|
|
49
|
+
/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/lib/rspec/core/runner.rb:45:in `invoke'
|
|
50
|
+
/home/dev/.rvm/gems/ruby-2.3.1/gems/rspec-core-3.5.2/exe/rspec:4:in `<top (required)>'
|
|
51
|
+
/home/dev/.rvm/gems/ruby-2.3.1/bin/rspec:23:in `load'
|
|
52
|
+
/home/dev/.rvm/gems/ruby-2.3.1/bin/rspec:23:in `<main>'
|
|
53
|
+
/home/dev/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `eval'
|
|
54
|
+
/home/dev/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `<main>'
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Installation
|
|
58
|
+
|
|
59
|
+
Add this line to your application's Gemfile:
|
|
60
|
+
|
|
61
|
+
```ruby
|
|
62
|
+
gem 'loggerman', git: 'https://github.com/danielalvarenga/loggerman.git', branch: 'master'
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
And then execute:
|
|
66
|
+
|
|
67
|
+
$ bundle
|
|
68
|
+
|
|
69
|
+
Configure log formatter in environment if you prefer to apply format to rails default log:
|
|
70
|
+
|
|
71
|
+
```ruby
|
|
72
|
+
# config/environments/production.rb
|
|
73
|
+
config.log_formatter = Loggerman::LoggerFormatter.new
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Customize log level for loggerman (rails log level is default) :
|
|
77
|
+
|
|
78
|
+
```ruby
|
|
79
|
+
# config/initializers/loggerman.rb
|
|
80
|
+
MyApp::Application.configure do
|
|
81
|
+
config.loggerman.level = :info
|
|
82
|
+
end
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Customize format (default is :key_value):
|
|
86
|
+
|
|
87
|
+
```ruby
|
|
88
|
+
# config/initializers/loggerman.rb
|
|
89
|
+
MyApp::Application.configure do
|
|
90
|
+
config.loggerman.format = :key_value # or :json
|
|
91
|
+
end
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Customize log files by level (rails log file is default):
|
|
95
|
+
|
|
96
|
+
```ruby
|
|
97
|
+
# config/initializers/loggerman.rb
|
|
98
|
+
MyApp::Application.configure do
|
|
99
|
+
config.loggerman.log_files = { error: 'my_error_logs.log', warn: 'other_file.log' }
|
|
100
|
+
end
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Customize progname (rails log progname is default) :
|
|
104
|
+
|
|
105
|
+
```ruby
|
|
106
|
+
# config/initializers/loggerman.rb
|
|
107
|
+
MyApp::Application.configure do
|
|
108
|
+
config.loggerman.progname = 'MySystem'
|
|
109
|
+
end
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Usage
|
|
113
|
+
|
|
114
|
+
```ruby
|
|
115
|
+
Loggerman.info 'any string'
|
|
116
|
+
Loggerman.warn key1: 'value1', key2: 'value2'
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Development
|
|
120
|
+
|
|
121
|
+
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.
|
|
122
|
+
|
|
123
|
+
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).
|
|
124
|
+
|
|
125
|
+
## Contributing
|
|
126
|
+
|
|
127
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/danielalvarenga/loggerman.
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
## License
|
|
131
|
+
|
|
132
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
|
133
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "loggerman"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/loggerman.rb
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
require "loggerman/version"
|
|
2
|
+
require 'loggerman/railtie' if defined?(Rails)
|
|
3
|
+
require 'loggerman/logger_formatter'
|
|
4
|
+
require 'loggerman/logger'
|
|
5
|
+
require 'loggerman/formatters/formatter'
|
|
6
|
+
require 'loggerman/formatters/json_formatter'
|
|
7
|
+
require 'loggerman/formatters/key_value_formatter'
|
|
8
|
+
|
|
9
|
+
module Loggerman
|
|
10
|
+
module_function
|
|
11
|
+
mattr_accessor :logger, :application, :formatters, :formatter, :log_files, :level, :progname
|
|
12
|
+
|
|
13
|
+
def setup(app)
|
|
14
|
+
self.application = app
|
|
15
|
+
set_level
|
|
16
|
+
set_progname
|
|
17
|
+
set_formatters
|
|
18
|
+
set_formatter
|
|
19
|
+
set_log_files
|
|
20
|
+
set_logger
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def set_level
|
|
24
|
+
Loggerman.level = [:debug, :info, :warn, :error, :fatal, :unknown].index( loggerman_config.level )
|
|
25
|
+
Loggerman.level ||= defined?(Rails) ? Rails.logger.level : 0
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def set_progname
|
|
29
|
+
Loggerman.progname = loggerman_config.progname
|
|
30
|
+
Loggerman.progname ||= defined?(Rails) ? Rails.logger.progname : nil
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def set_formatters
|
|
34
|
+
Loggerman.formatters = {json: Loggerman::Formatters::JsonFormatter.new, key_value: Loggerman::Formatters::KeyValueFormatter.new}
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def set_formatter
|
|
38
|
+
Loggerman.formatter = Loggerman.formatters.fetch( loggerman_config.format || :key_value )
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def set_log_files
|
|
42
|
+
Loggerman.log_files = loggerman_config.log_files || {}
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def set_logger
|
|
46
|
+
Loggerman.logger = Loggerman::Logger.new(defined?(Rails) ? "#{Rails.root}/log/#{Rails.env.to_s}.log" : 'log/loggerman.log')
|
|
47
|
+
Loggerman.logger.level = Loggerman.level
|
|
48
|
+
Loggerman.logger.progname = Loggerman.progname
|
|
49
|
+
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def loggerman_config
|
|
53
|
+
application.config.loggerman
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
%w(info debug warn error fatal unknown).each do |level|
|
|
57
|
+
define_method level.to_sym do |*args, &block|
|
|
58
|
+
Loggerman.logger.send(level.to_sym, *args, &block)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
module Loggerman
|
|
2
|
+
module Formatters
|
|
3
|
+
class Formatter
|
|
4
|
+
|
|
5
|
+
def format_log(severity, timestamp, progname, message) ; raise NotImplementedError end
|
|
6
|
+
def convert_string(message) ; raise NotImplementedError end
|
|
7
|
+
def convert_hash(args) ; raise NotImplementedError end
|
|
8
|
+
def convert_exception(exception) ; raise NotImplementedError end
|
|
9
|
+
def convert_other(message) ; raise NotImplementedError end
|
|
10
|
+
|
|
11
|
+
def clean_message(message)
|
|
12
|
+
if message.is_a?(String)
|
|
13
|
+
message = message.strip.gsub(/\e\[(\d+)m/, '')
|
|
14
|
+
message = eval(message)
|
|
15
|
+
end
|
|
16
|
+
message
|
|
17
|
+
rescue Exception => e
|
|
18
|
+
message
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def format_message(message)
|
|
22
|
+
message = clean_message(message)
|
|
23
|
+
case message
|
|
24
|
+
when ::String
|
|
25
|
+
convert_string(message)
|
|
26
|
+
when ::Hash
|
|
27
|
+
convert_hash(message)
|
|
28
|
+
when ::Exception
|
|
29
|
+
convert_exception(message)
|
|
30
|
+
else
|
|
31
|
+
convert_other(message)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
module Loggerman
|
|
2
|
+
module Formatters
|
|
3
|
+
class JsonFormatter < Formatter
|
|
4
|
+
|
|
5
|
+
def format_log(severity, timestamp, progname, message)
|
|
6
|
+
log_result = { severity => { time: timestamp.strftime("%Y-%m-%d %H:%M:%S"), src: Socket.ip_address_list[0].ip_address, host: Socket.gethostname } }
|
|
7
|
+
log_result[severity].merge!( {system: progname.to_s} ) unless progname.blank?
|
|
8
|
+
log_result[severity].merge!( format_message(message) ) unless message.blank?
|
|
9
|
+
log_result.to_json
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def convert_string(message)
|
|
13
|
+
{ details: message }
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def convert_hash(args)
|
|
17
|
+
args
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def convert_exception(exception)
|
|
21
|
+
result = { exception: { class: exception.class.to_s, message: exception.message, location: exception.backtrace_locations.try(:first) } }
|
|
22
|
+
result[:exception].merge!( { location: exception.backtrace_locations.first } ) unless exception.backtrace_locations.blank?
|
|
23
|
+
result[:exception].merge!( format_message(exception.more_attributes) ) if exception.respond_to?(:more_attributes) && !exception.more_attributes.blank?
|
|
24
|
+
result[:exception].merge!( { backtrace: exception.backtrace } ) unless exception.backtrace.blank?
|
|
25
|
+
result
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def convert_other(message)
|
|
29
|
+
{ details: message.inspect }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
module Loggerman
|
|
2
|
+
module Formatters
|
|
3
|
+
class KeyValueFormatter < Formatter
|
|
4
|
+
|
|
5
|
+
def format_log(severity, timestamp, progname, message)
|
|
6
|
+
log_result = "#{timestamp.strftime("%Y-%m-%d %H:%M:%S")}|#{severity}|src=#{Socket.ip_address_list[0].ip_address}|host=#{Socket.gethostname}|"
|
|
7
|
+
log_result << "system=#{progname}|" unless progname.blank?
|
|
8
|
+
log_result << "#{format_message(message)}" unless message.blank?
|
|
9
|
+
log_result.to_s.gsub('||',"|")
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def convert_string(message)
|
|
13
|
+
"details=#{message}|"
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def convert_hash(args)
|
|
17
|
+
result = ""
|
|
18
|
+
args.each { |k,v| result << "#{k}=#{v.inspect}|" }
|
|
19
|
+
result.to_s
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def convert_exception(exception)
|
|
23
|
+
message = "exception=#{exception.class}|message=#{exception.message}|"
|
|
24
|
+
message << "location=#{exception.backtrace_locations.first}|" unless exception.backtrace_locations.blank?
|
|
25
|
+
message << "#{format_message(exception.more_attributes)}|" if exception.respond_to?(:more_attributes) && !exception.more_attributes.blank?
|
|
26
|
+
message << "\n#{(exception.backtrace).join("\n")}" unless exception.backtrace.empty?
|
|
27
|
+
message.to_s
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def convert_other(message)
|
|
31
|
+
"details=#{message.inspect}|"
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
module Loggerman
|
|
2
|
+
class Logger < Logger
|
|
3
|
+
|
|
4
|
+
%w(info debug warn error fatal unknown).each do |level|
|
|
5
|
+
class_eval <<-METHOD, __FILE__, __LINE__ + 1
|
|
6
|
+
def #{level}(*args, &block)
|
|
7
|
+
logger_by_level('#{level}').#{level}(*args, &block)
|
|
8
|
+
end
|
|
9
|
+
METHOD
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def logger_by_level(level)
|
|
13
|
+
@logger_by_level ||= {}
|
|
14
|
+
return @logger_by_level[level] if @logger_by_level[level]
|
|
15
|
+
@logger_by_level[level] ||= defined?(Rails) ? Rails.logger : nil
|
|
16
|
+
unless Loggerman.log_files[level.to_sym].blank?
|
|
17
|
+
file = Loggerman.log_files.fetch(level.to_sym)
|
|
18
|
+
logfile = File.open(defined?(Rails) ? "#{Rails.root}/log/#{file}" : "log/#{file}", 'a')
|
|
19
|
+
logfile.sync = true
|
|
20
|
+
@logger_by_level[level] = ActiveSupport::Logger.new(logfile)
|
|
21
|
+
@logger_by_level[level].formatter = Loggerman::LoggerFormatter.new
|
|
22
|
+
@logger_by_level[level].level = Loggerman::level
|
|
23
|
+
@logger_by_level[level].progname = Loggerman::progname
|
|
24
|
+
end
|
|
25
|
+
@logger_by_level[level]
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
require 'active_support/logger'
|
|
2
|
+
|
|
3
|
+
module Loggerman
|
|
4
|
+
class LoggerFormatter < ActiveSupport::Logger::SimpleFormatter
|
|
5
|
+
|
|
6
|
+
def call(severity, timestamp, progname, msg)
|
|
7
|
+
log_message = Loggerman.formatter.format_log(severity, timestamp, progname, msg)
|
|
8
|
+
"#{log_message}\n"
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
end
|
|
12
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
require 'rails/railtie'
|
|
2
|
+
|
|
3
|
+
module Loggerman
|
|
4
|
+
class Railtie < Rails::Railtie
|
|
5
|
+
config.loggerman = ActiveSupport::OrderedOptions.new
|
|
6
|
+
# config.loggerman.enabled = false
|
|
7
|
+
|
|
8
|
+
config.after_initialize do |app|
|
|
9
|
+
Loggerman.setup(app) # if app.config.loggerman.enabled
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
data/log/.keep
ADDED
|
File without changes
|
data/loggerman.gemspec
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'loggerman/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "loggerman"
|
|
8
|
+
spec.version = Loggerman::VERSION
|
|
9
|
+
spec.authors = ["Daniel Alvarenga"]
|
|
10
|
+
spec.email = ["danielalvarengalima@gmail.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{Add a custom logger and formatter to rails logs}
|
|
13
|
+
spec.description = %q{Add a custom logger and formatter to rails logs}
|
|
14
|
+
spec.homepage = "https://github.com/danielalvarenga/loggerman"
|
|
15
|
+
spec.license = "MIT"
|
|
16
|
+
|
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
|
19
|
+
# if spec.respond_to?(:metadata)
|
|
20
|
+
# spec.metadata['allowed_push_host'] = "http://mygemserver.com"
|
|
21
|
+
# else
|
|
22
|
+
# raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
|
23
|
+
# end
|
|
24
|
+
|
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
26
|
+
spec.bindir = "exe"
|
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
28
|
+
spec.require_paths = ["lib"]
|
|
29
|
+
|
|
30
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
32
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
|
33
|
+
spec.add_development_dependency "pry"
|
|
34
|
+
|
|
35
|
+
spec.add_runtime_dependency 'activesupport', '>= 4', '< 5.1'
|
|
36
|
+
spec.add_runtime_dependency 'railties', '>= 4', '< 5.1'
|
|
37
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: loggerman
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Daniel Alvarenga
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2016-10-31 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.12'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.12'
|
|
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: pry
|
|
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
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: activesupport
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - ">="
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '4'
|
|
76
|
+
- - "<"
|
|
77
|
+
- !ruby/object:Gem::Version
|
|
78
|
+
version: '5.1'
|
|
79
|
+
type: :runtime
|
|
80
|
+
prerelease: false
|
|
81
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
82
|
+
requirements:
|
|
83
|
+
- - ">="
|
|
84
|
+
- !ruby/object:Gem::Version
|
|
85
|
+
version: '4'
|
|
86
|
+
- - "<"
|
|
87
|
+
- !ruby/object:Gem::Version
|
|
88
|
+
version: '5.1'
|
|
89
|
+
- !ruby/object:Gem::Dependency
|
|
90
|
+
name: railties
|
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
|
92
|
+
requirements:
|
|
93
|
+
- - ">="
|
|
94
|
+
- !ruby/object:Gem::Version
|
|
95
|
+
version: '4'
|
|
96
|
+
- - "<"
|
|
97
|
+
- !ruby/object:Gem::Version
|
|
98
|
+
version: '5.1'
|
|
99
|
+
type: :runtime
|
|
100
|
+
prerelease: false
|
|
101
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
102
|
+
requirements:
|
|
103
|
+
- - ">="
|
|
104
|
+
- !ruby/object:Gem::Version
|
|
105
|
+
version: '4'
|
|
106
|
+
- - "<"
|
|
107
|
+
- !ruby/object:Gem::Version
|
|
108
|
+
version: '5.1'
|
|
109
|
+
description: Add a custom logger and formatter to rails logs
|
|
110
|
+
email:
|
|
111
|
+
- danielalvarengalima@gmail.com
|
|
112
|
+
executables: []
|
|
113
|
+
extensions: []
|
|
114
|
+
extra_rdoc_files: []
|
|
115
|
+
files:
|
|
116
|
+
- ".gitignore"
|
|
117
|
+
- ".rspec"
|
|
118
|
+
- ".travis.yml"
|
|
119
|
+
- Gemfile
|
|
120
|
+
- LICENSE.txt
|
|
121
|
+
- README.md
|
|
122
|
+
- Rakefile
|
|
123
|
+
- bin/console
|
|
124
|
+
- bin/setup
|
|
125
|
+
- lib/loggerman.rb
|
|
126
|
+
- lib/loggerman/formatters/formatter.rb
|
|
127
|
+
- lib/loggerman/formatters/json_formatter.rb
|
|
128
|
+
- lib/loggerman/formatters/key_value_formatter.rb
|
|
129
|
+
- lib/loggerman/logger.rb
|
|
130
|
+
- lib/loggerman/logger_formatter.rb
|
|
131
|
+
- lib/loggerman/railtie.rb
|
|
132
|
+
- lib/loggerman/version.rb
|
|
133
|
+
- log/.keep
|
|
134
|
+
- loggerman.gemspec
|
|
135
|
+
homepage: https://github.com/danielalvarenga/loggerman
|
|
136
|
+
licenses:
|
|
137
|
+
- MIT
|
|
138
|
+
metadata: {}
|
|
139
|
+
post_install_message:
|
|
140
|
+
rdoc_options: []
|
|
141
|
+
require_paths:
|
|
142
|
+
- lib
|
|
143
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
144
|
+
requirements:
|
|
145
|
+
- - ">="
|
|
146
|
+
- !ruby/object:Gem::Version
|
|
147
|
+
version: '0'
|
|
148
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
149
|
+
requirements:
|
|
150
|
+
- - ">="
|
|
151
|
+
- !ruby/object:Gem::Version
|
|
152
|
+
version: '0'
|
|
153
|
+
requirements: []
|
|
154
|
+
rubyforge_project:
|
|
155
|
+
rubygems_version: 2.5.1
|
|
156
|
+
signing_key:
|
|
157
|
+
specification_version: 4
|
|
158
|
+
summary: Add a custom logger and formatter to rails logs
|
|
159
|
+
test_files: []
|