rack-colorized_logger 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/rack-colorized_logger.rb +67 -0
  2. metadata +63 -0
@@ -0,0 +1,67 @@
1
+ require 'term/ansicolor'
2
+ class ::String; include ::Term::ANSIColor; end
3
+
4
+ module Rack
5
+ class ColorizedLogger
6
+
7
+ DEFAULT_COLORS = {
8
+ :params => [:blue, :red],
9
+ :session => [:cyan, :yellow],
10
+ :cookies => [:green, :magenta]
11
+ }
12
+
13
+ DEFAULT_OUTPUT = ::STDOUT
14
+
15
+ attr_writer :public, :colors, :out
16
+
17
+ def initialize(app)
18
+ yield self if block_given?
19
+ @colors ||= (defined? Rack::ColorizedLogger::COLORS) ? Rack::ColorizedLogger::COLORS : DEFAULT_COLORS
20
+ @out ||= DEFAULT_OUTPUT
21
+ @app = app
22
+ @public_map = Dir[::File.join(@public, '**', '*')].map {|f| ::File.basename f} if @public and ::File.directory? @public
23
+ end
24
+
25
+ def call env
26
+ @request = Rack::Request.new(env)
27
+ selected_paths = (@public_map.nil? or @public_map.empty?) ? nil : @public_map.select {|p| @request.path.index("\/#{p}") == 0}
28
+ if selected_paths.nil? or selected_paths.empty?
29
+ @out.puts "path:".bold + " " + @request.path
30
+ @colors.each do |thing, color_a|
31
+ if thing.respond_to? :call
32
+ _thing = thing.call(@request)
33
+ @out.puts "#{_thing[0]}:".send(color_a[0]).bold
34
+ pretty_colors_h _thing[1], *color_a
35
+ else
36
+ @out.puts "#{thing.to_s}:".send(color_a[0]).bold
37
+ pretty_colors_h @request.send(thing), *color_a
38
+ end
39
+ end
40
+ end
41
+ @app.call env
42
+ end
43
+
44
+ private
45
+
46
+ def pretty_colors_h(hash, k_color, v_color = nil, padding = 1, start = true)
47
+ v_color ||= k_color
48
+ indent = padding + 1
49
+ @out.puts sprintf("%1$*2$s", "{", padding).blue if start
50
+ hash.each do |k,v|
51
+ if v.is_a?(Hash)
52
+ @out.puts sprintf("%1$*2$s","",indent) + "#{k.to_s.bold.send(k_color)} => " + "{".blue
53
+ pretty_colors_h(v, k_color, v_color, padding+2, false)
54
+ elsif v.is_a?(Array)
55
+ @out.puts sprintf("%1$*2$s","",indent) + "#{k.to_s.bold.send(k_color)} => " + pretty_colors_a(v, v_color)
56
+ else
57
+ @out.puts sprintf("%1$*2$s","",indent) + "#{k.to_s.bold.send(k_color)} => #{v.to_s.bold.send(v_color)}"
58
+ end
59
+ end
60
+ @out.puts sprintf("%1$*2$s", "}", padding).blue
61
+ end
62
+
63
+ def pretty_colors_a(array, color)
64
+ "[" + array.map {|e| e.is_a?(Hash) ? e.inspect.bold.send(color) : e.bold.send(color)}.join(",") + "]"
65
+ end
66
+ end
67
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-colorized_logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kenichi Nakamura
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: term-ansicolor
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description:
31
+ email:
32
+ - kenichi.nakamura@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/rack-colorized_logger.rb
38
+ homepage: https://github.com/kenichi/rack-colorized_logger
39
+ licenses: []
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: 1.4.2
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 1.8.24
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: simple logger that outputs params, session, and cookies in fancy colors.
62
+ test_files: []
63
+ has_rdoc: