shog 0.0.1pre → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/shog.rb +6 -0
- data/lib/shog/formatter.rb +149 -0
- data/lib/shog/formatters.rb +2 -0
- data/lib/shog/formatters/defaults.rb +20 -0
- data/lib/shog/formatters/requests.rb +48 -0
- data/lib/shog/rails.rb +14 -0
- data/lib/shog/version.rb +2 -2
- data/shog.gemspec +1 -0
- data/spec/lib/shog/formatter_spec.rb +67 -0
- metadata +24 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e02b8810b0926489472a9c3ae7172681c903d247
|
4
|
+
data.tar.gz: 82e7c9c7d3fbc7ea093b79f3afd0c638c9bab046
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a851ea89f9357838dd1d6b2f31108757b1b846cdcfec47004887a08cf825c4afd086217251c49e403f2589f1697b15d5d1094043e715ba821c2e70258bf25c1
|
7
|
+
data.tar.gz: 515b080f51d3f66fa3d49f48c1a9e6fbc26916c46e27261195c38eb3aa244a384981b229ef3584e8f45314a198be31842cd0a4907b55f35c2f291fae4fe384e6
|
data/lib/shog.rb
CHANGED
@@ -0,0 +1,149 @@
|
|
1
|
+
require 'active_support/tagged_logging'
|
2
|
+
require 'colored'
|
3
|
+
|
4
|
+
module Shog
|
5
|
+
class Formatter < ::ActiveSupport::Logger::SimpleFormatter
|
6
|
+
|
7
|
+
include ActiveSupport::TaggedLogging::Formatter
|
8
|
+
|
9
|
+
def call( severity, time, progname, msg )
|
10
|
+
return if msg.blank?
|
11
|
+
tagged formatted_severity_tag( severity ) do
|
12
|
+
msg = formatted_message( severity, msg )
|
13
|
+
msg = timestamped_message( time, msg )
|
14
|
+
msg = prognamed_message( progname, msg )
|
15
|
+
|
16
|
+
super severity, time, progname, msg
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def initialize
|
21
|
+
reset_config!
|
22
|
+
end
|
23
|
+
|
24
|
+
# Formats the message according to the configured settings.
|
25
|
+
# @param [String] msg to format.
|
26
|
+
def formatted_message( severity, msg )
|
27
|
+
msg = String === msg ? msg : msg.inspect
|
28
|
+
|
29
|
+
if args = matched( msg )
|
30
|
+
args.first.call msg, args.last
|
31
|
+
elsif proc = configuration[:severities][severity]
|
32
|
+
proc.call msg
|
33
|
+
else
|
34
|
+
msg
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# Formats the severity indicator prefixed before each line when writing to
|
39
|
+
# the log.
|
40
|
+
# @param [String] the severity of the message (ex DEBUG, WARN, etc.)
|
41
|
+
# @return [String] formatted version of the severity
|
42
|
+
def formatted_severity_tag( severity )
|
43
|
+
length = configuration[:severity_tags][:_length] ||= begin
|
44
|
+
configuration[:severity_tags].reduce(0){ |l,(k,_)| [k.length,l].max }
|
45
|
+
end
|
46
|
+
|
47
|
+
padded_severity = severity.ljust length
|
48
|
+
|
49
|
+
if proc = configuration[:severity_tags][severity]
|
50
|
+
proc.call padded_severity
|
51
|
+
else
|
52
|
+
padded_severity
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# Formats a ms time value.
|
57
|
+
def format_time( time, expected = 30 )
|
58
|
+
timef = time.to_f
|
59
|
+
case
|
60
|
+
when timef > expected * 2 then time.to_s.red
|
61
|
+
when timef > expected then time.to_s.yellow
|
62
|
+
else time
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
# ==========================================================================
|
68
|
+
# @!group Configuration
|
69
|
+
|
70
|
+
# Configure messages formatting for this formatter.
|
71
|
+
def configure( &block )
|
72
|
+
instance_eval( &block )
|
73
|
+
self
|
74
|
+
end
|
75
|
+
|
76
|
+
# Format the severity tagged before each line.
|
77
|
+
def severity_tag( level, &block )
|
78
|
+
configuration[:severity_tags][ level.to_s.upcase ] = block
|
79
|
+
end
|
80
|
+
|
81
|
+
# Provide customized formatting for messages of the given severity when they
|
82
|
+
# a custom matcher cannot be found.
|
83
|
+
# @param [String,Symbol] level to format.
|
84
|
+
def severity( level, &block )
|
85
|
+
configuration[:severities][ level.to_s.upcase ] = block
|
86
|
+
end
|
87
|
+
|
88
|
+
# Resets any previously configured formatting settings.
|
89
|
+
def reset_config!
|
90
|
+
@configuration ||= {
|
91
|
+
severity_tags: {},
|
92
|
+
severities: {},
|
93
|
+
matchers: {}
|
94
|
+
}
|
95
|
+
self
|
96
|
+
end
|
97
|
+
|
98
|
+
# When a log message matches the given pattern, provide a custom format
|
99
|
+
# for it.
|
100
|
+
def match( pattern, &block )
|
101
|
+
configuration[:matchers][pattern] = block
|
102
|
+
end
|
103
|
+
|
104
|
+
# Adds the named matchers to the log
|
105
|
+
def formatter( mod )
|
106
|
+
unless mod.is_a? Module
|
107
|
+
mod = "Shog::Formatters::#{mod.to_s.camelize}".constantize
|
108
|
+
end
|
109
|
+
|
110
|
+
mod.configure self
|
111
|
+
end
|
112
|
+
|
113
|
+
# Include timestamp in logged messages.
|
114
|
+
def timestamp( enabled = true )
|
115
|
+
configuration[:timestamp] = enabled
|
116
|
+
end
|
117
|
+
|
118
|
+
# Include the progname in logged messages.
|
119
|
+
def progname( enabled = true )
|
120
|
+
configuration[:progname] = enabled
|
121
|
+
end
|
122
|
+
|
123
|
+
# @!endgroup
|
124
|
+
|
125
|
+
private
|
126
|
+
|
127
|
+
attr_accessor :configuration
|
128
|
+
|
129
|
+
def matched( msg )
|
130
|
+
if matched = configuration[:matchers].find do |pattern,_|
|
131
|
+
pattern === msg
|
132
|
+
end
|
133
|
+
[matched.last, Regexp.last_match]
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
def timestamped_message( time, msg )
|
138
|
+
return msg unless configuration[:timestamp]
|
139
|
+
|
140
|
+
"[#{time}] #{msg}"
|
141
|
+
end
|
142
|
+
|
143
|
+
def prognamed_message( progname, msg )
|
144
|
+
return msg unless configuration[:progname]
|
145
|
+
|
146
|
+
"[#{progname}] #{msg}"
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Shog
|
2
|
+
module Formatters
|
3
|
+
# Default formatting options
|
4
|
+
module Defaults
|
5
|
+
module_function
|
6
|
+
|
7
|
+
def configure( formatter )
|
8
|
+
formatter.configure do
|
9
|
+
severity_tag( :debug ) { |msg| msg.black.bold }
|
10
|
+
severity_tag( :warn ) { |msg| msg.yellow }
|
11
|
+
severity_tag( :error ) { |msg| msg.red }
|
12
|
+
severity_tag( :fatal ) { |msg| msg.white_on_red }
|
13
|
+
|
14
|
+
severity( :error ){ |msg| msg.red }
|
15
|
+
severity( :fatal ){ |msg| msg.red }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Shog
|
2
|
+
module Formatters
|
3
|
+
# Format log messages from the request processing such as controller endpoints and views.
|
4
|
+
module Requests
|
5
|
+
module_function
|
6
|
+
|
7
|
+
def configure( formatter )
|
8
|
+
formatter.configure do
|
9
|
+
match /Started\s+(?<method>PUT|PATCH|GET|POST|DELETE)\s+(?<path>"[^"]*")[^\d\.]+(?<ip>[\d\.]+)(?<time>.*)/ do |msg,match|
|
10
|
+
# http://refiddle.com/ge6
|
11
|
+
"#{match["method"].ljust 6} ".green.bold + " #{match["path"]} ".white.bold + " for " + "#{match["ip"]}".yellow + " #{match["time"]}".black.bold
|
12
|
+
end
|
13
|
+
|
14
|
+
match /\s*Rendered\s+(?<view>[^\s]+)\swithin\s(?<layout>[^\s]+)\s\((?<time>.*)\)/ do |msg,match|
|
15
|
+
# http://refiddle.com/18qr
|
16
|
+
" Rendered " + match["view"].white.bold + " within " + match["layout"].white + " " + format_time( match['time'] )
|
17
|
+
end
|
18
|
+
|
19
|
+
match /\s*Completed\s(?<code>\d+)\s(?<friendly>.*)\sin\s(?<time>\d+[^\s]*)\s(?<details>.*)/ do |msg,match|
|
20
|
+
# http://refiddle.com/18qq
|
21
|
+
parts = [ "Completed" ]
|
22
|
+
parts << case match['code'].to_i
|
23
|
+
when 200..399 then match['code'].green
|
24
|
+
when 400..499 then match['code'].yellow
|
25
|
+
else match['code'].red
|
26
|
+
end
|
27
|
+
parts << match['friendly'].yellow
|
28
|
+
parts << 'in'
|
29
|
+
parts << format_time( match['time'], 250 )
|
30
|
+
parts << match['details']
|
31
|
+
|
32
|
+
parts.join(" ")
|
33
|
+
end
|
34
|
+
|
35
|
+
match /Processing by (?<controller>[^\s]*) as (?<format>.*)/ do |msg,match|
|
36
|
+
# http://refiddle.com/18qs
|
37
|
+
"Processing by #{match['controller'].magenta.bold} as #{match['format'].yellow}"
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/shog/rails.rb
ADDED
data/lib/shog/version.rb
CHANGED
data/shog.gemspec
CHANGED
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Shog::Formatters::Spec
|
4
|
+
module_function
|
5
|
+
def configure( formatter )
|
6
|
+
formatter.configure do
|
7
|
+
severity :test, ->{}
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe Shog::Formatter do
|
13
|
+
let(:formatter){ Shog::Formatter.new }
|
14
|
+
|
15
|
+
describe "#formatted_severity_tag" do
|
16
|
+
it "buffers to the same size" do
|
17
|
+
formatter.severity_tag( :warn ){ |msg| msg }
|
18
|
+
formatter.severity_tag( :longer ){ |msg| msg }
|
19
|
+
|
20
|
+
expect( formatter.formatted_severity_tag( "WARN" ) ).to eq "WARN "
|
21
|
+
expect( formatter.formatted_severity_tag( "LONGER" ) ).to eq "LONGER"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "#match" do
|
26
|
+
it "formats a matched line" do
|
27
|
+
formatter.match /GET/ do |msg,match|
|
28
|
+
"R'DONE"
|
29
|
+
end
|
30
|
+
|
31
|
+
result = formatter.call "INFO", Time.now, nil, "Started GET \"/Home\""
|
32
|
+
expect( result ).to eq "[INFO] R'DONE\n"
|
33
|
+
end
|
34
|
+
|
35
|
+
it "includes the match" do
|
36
|
+
formatter.match /GET/ do |msg,match|
|
37
|
+
expect( match ).to be_present
|
38
|
+
end
|
39
|
+
|
40
|
+
formatter.call "INFO", Time.now, nil, "Started GET \"/Home\""
|
41
|
+
end
|
42
|
+
|
43
|
+
it "doesn't match" do
|
44
|
+
result = formatter.call "INFO", Time.now, nil, "Started GET \"/Home\""
|
45
|
+
expect( result ).to eq "[INFO] Started GET \"/Home\"\n"
|
46
|
+
end
|
47
|
+
|
48
|
+
it "uses default severity when no matcher is found" do
|
49
|
+
formatter.severity( :info ){ |msg| "DEFAULT" }
|
50
|
+
|
51
|
+
result = formatter.call "INFO", Time.now, nil, "Started GET \"/Home\""
|
52
|
+
expect( result ).to eq "[INFO] DEFAULT\n"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "#formatters" do
|
57
|
+
it "loads from a symbol" do
|
58
|
+
formatter.should_receive(:severity).with(:test, anything())
|
59
|
+
|
60
|
+
formatter.configure do
|
61
|
+
formatter :spec
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shog
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul Alexander
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: colored
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.2'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.2'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: bundler
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -87,8 +101,14 @@ files:
|
|
87
101
|
- README.md
|
88
102
|
- Rakefile
|
89
103
|
- lib/shog.rb
|
104
|
+
- lib/shog/formatter.rb
|
105
|
+
- lib/shog/formatters.rb
|
106
|
+
- lib/shog/formatters/defaults.rb
|
107
|
+
- lib/shog/formatters/requests.rb
|
108
|
+
- lib/shog/rails.rb
|
90
109
|
- lib/shog/version.rb
|
91
110
|
- shog.gemspec
|
111
|
+
- spec/lib/shog/formatter_spec.rb
|
92
112
|
- spec/spec_helper.rb
|
93
113
|
homepage: https://github.com/phallguy/shog
|
94
114
|
licenses:
|
@@ -105,9 +125,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
105
125
|
version: 1.9.2
|
106
126
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
127
|
requirements:
|
108
|
-
- - "
|
128
|
+
- - ">="
|
109
129
|
- !ruby/object:Gem::Version
|
110
|
-
version:
|
130
|
+
version: '0'
|
111
131
|
requirements: []
|
112
132
|
rubyforge_project:
|
113
133
|
rubygems_version: 2.3.0
|
@@ -115,5 +135,6 @@ signing_key:
|
|
115
135
|
specification_version: 4
|
116
136
|
summary: Make rails 4.0 log details more colorful
|
117
137
|
test_files:
|
138
|
+
- spec/lib/shog/formatter_spec.rb
|
118
139
|
- spec/spec_helper.rb
|
119
140
|
has_rdoc:
|