format_logs 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,4 @@
1
+ lib/**/*.rb
2
+ README.rdoc
3
+ ChangeLog.rdoc
4
+ LICENSE.txt
data/.loadpath ADDED
@@ -0,0 +1,5 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <loadpath>
3
+ <pathentry path="" type="src"/>
4
+ <pathentry path="org.rubypeople.rdt.launching.RUBY_CONTAINER" type="con"/>
5
+ </loadpath>
data/.project ADDED
@@ -0,0 +1,17 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <projectDescription>
3
+ <name>format_logs</name>
4
+ <comment></comment>
5
+ <projects>
6
+ </projects>
7
+ <buildSpec>
8
+ <buildCommand>
9
+ <name>org.rubypeople.rdt.core.rubybuilder</name>
10
+ <arguments>
11
+ </arguments>
12
+ </buildCommand>
13
+ </buildSpec>
14
+ <natures>
15
+ <nature>org.rubypeople.rdt.core.rubynature</nature>
16
+ </natures>
17
+ </projectDescription>
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour --format documentation
data/ChangeLog.rdoc ADDED
@@ -0,0 +1,4 @@
1
+ === 0.1.0 / 2011-06-01
2
+
3
+ * Initial release:
4
+
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Erik Gustavson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,51 @@
1
+ = format_logs - basic log formatting
2
+
3
+ * {Homepage}[http://github.com/goosetav/format_logs]
4
+ * {Documentation}[http://rubydoc.info/gems/format_logs/frames]
5
+
6
+ == Description
7
+
8
+ Adds some simple formatting to the standard Rails BufferedLogger class.
9
+
10
+ This solution aims to be something between the default logging which is missing information such as
11
+ timestamps, process IDs and severity levels and a full logging solution like {log4r}[http://log4r.rubyforge.org/]
12
+
13
+ Gem inspired by Paul Dowman's {better_logging}[https://github.com/pauldowman/better_logging]
14
+
15
+ == Features
16
+
17
+ Configurable settings - accepts a hash of options to adjust the output format.
18
+
19
+ == Examples
20
+
21
+ These are the defaults
22
+
23
+ FormatLogs.settings = {
24
+ :time_format => '%Y-%m-%d %H:%M:%S %z',
25
+ :show_pid => true,
26
+ :show_host => true,
27
+ :show_time => true
28
+ }
29
+
30
+ you can pass any combination of options you want:
31
+ FormatLogs.setting = {:show_pid => false}
32
+
33
+ == Requirements
34
+
35
+ Rails 3.0+
36
+
37
+ == Install
38
+
39
+ add:
40
+
41
+ gem require 'format_logs'
42
+
43
+ to your Gemfile and then run:
44
+
45
+ bundle update format_logs
46
+
47
+ == Copyright
48
+
49
+ Copyright (c) 2011 Erik Gustavson
50
+
51
+ See LICENSE.txt for details.
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ gem 'ore-tasks', '~> 0.4'
6
+ require 'ore/tasks'
7
+
8
+ Ore::Tasks.new
9
+ rescue LoadError => e
10
+ STDERR.puts e.message
11
+ STDERR.puts "Run `gem install ore-tasks` to install 'ore/tasks'."
12
+ end
13
+
14
+ require 'rake/rdoctask'
15
+ Rake::RDocTask.new do |rdoc|
16
+ rdoc.title = "format_logs"
17
+ end
18
+ task :doc => :rdoc
19
+
20
+ begin
21
+ gem 'rspec', '~> 2.4'
22
+ require 'rspec/core/rake_task'
23
+
24
+ RSpec::Core::RakeTask.new
25
+ rescue LoadError => e
26
+ task :spec do
27
+ abort "Please run `gem install rspec` to install RSpec."
28
+ end
29
+ end
30
+
31
+ task :test => :spec
32
+ task :default => :spec
@@ -0,0 +1,15 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ begin
4
+ Ore::Specification.new do |gemspec|
5
+ # custom logic here
6
+ end
7
+ rescue NameError
8
+ begin
9
+ require 'ore/specification'
10
+ retry
11
+ rescue LoadError
12
+ STDERR.puts "The '#{__FILE__}' file requires Ore."
13
+ STDERR.puts "Run `gem install ore-core` to install Ore."
14
+ end
15
+ end
data/gemspec.yml ADDED
@@ -0,0 +1,33 @@
1
+ name: format_logs
2
+ version: 0.1.0
3
+ summary: "Adds some basic formatting to the default rails logs"
4
+ description: |
5
+ Adds some simple formatting to the standard Rails BufferedLogger class.
6
+
7
+ This solution aims to be something between the default logging which is missing information such as
8
+ timestamps, process IDs and severity levels and a full logging solution like log4r
9
+
10
+ license: MIT
11
+ authors: goosetav
12
+ homepage: http://github.com/goosetav/format_logs
13
+ post_install_message: |
14
+ *********************************************
15
+
16
+ Add the following to your Gemfile to activate
17
+
18
+ gem "format_logs"
19
+
20
+ Configuration options can be set by:
21
+
22
+ FormatLogs.settings = {...}
23
+
24
+ see README.rdoc for more details
25
+
26
+ *********************************************
27
+
28
+ dependencies:
29
+ rails: >= 3.0
30
+
31
+ development_dependencies:
32
+ ore-tasks: ~> 0.4
33
+ rspec: ~> 2.4
@@ -0,0 +1,4 @@
1
+ module FormatLogs
2
+ # format_logs version
3
+ VERSION = "0.1.0"
4
+ end
@@ -0,0 +1,136 @@
1
+ require 'format_logs/version'
2
+
3
+ module FormatLogs
4
+
5
+ def self.included(base)
6
+ puts "Adding formatting to #{base.name}"
7
+ base.send :extend, ClassMethods
8
+
9
+ base.send :include, InstanceMethods
10
+ base.alias_method_chain :add, :formatting # if base.respond_to? :add
11
+ end
12
+
13
+ module ClassMethods
14
+
15
+ def settings
16
+ @settings ||= default_settings
17
+ end
18
+
19
+ def settings=(new_settings)
20
+ @settings = default_settings + new_settings
21
+ flush_attributes!
22
+ end
23
+
24
+ def flush_cached_settings!
25
+ [@time_format, @show_time, @show_pid, @show_host, @trim_leading_whitespace].each {|a| a = nil}
26
+ end
27
+
28
+ def default_settings
29
+ {
30
+ :time_format => '%Y-%m-%d %H:%M:%S %z',
31
+ :show_pid => true,
32
+ :show_host => true,
33
+ :show_time => true,
34
+ :trim_leading_whitespace => true
35
+ }
36
+ end
37
+
38
+ def time_format
39
+ @time_format ||= settings[:time_format]
40
+ end
41
+
42
+ def show_time?
43
+ @show_time ||= settings[:show_time]
44
+ end
45
+
46
+ def use_color?
47
+ @use_color ||= (Rails.configuration.colorize_logging != false)
48
+ end
49
+
50
+ def show_pid?
51
+ @show_pid ||= settings[:show_pid]
52
+ end
53
+
54
+ def trim_leading_whitespace?
55
+ @trim_leading_whitespace ||= settings[:trim_leading_whitespace]
56
+ end
57
+
58
+ def show_host?
59
+ @show_host ||= settings[:show_host]
60
+ end
61
+
62
+ def timestamp
63
+ "#{Time.now.utc.strftime(time_format)} - " if show_time?
64
+ end
65
+
66
+ def pid
67
+ "#{$$} - " if show_pid?
68
+ end
69
+
70
+ def hostname
71
+ @hostname ||= "#{`hostname -s`.strip rescue ''} - " if show_host?
72
+ end
73
+
74
+ def to_color(severity)
75
+ return case severity
76
+ when ActiveSupport::BufferedLogger::INFO then info_s
77
+ when ActiveSupport::BufferedLogger::WARN then warn_s
78
+ when ActiveSupport::BufferedLogger::ERROR then error_s
79
+ when ActiveSupport::BufferedLogger::FATAL then fatal_s
80
+ when ActiveSupport::BufferedLogger::DEBUG then debug_s
81
+ else unknown_s
82
+ end
83
+ end
84
+
85
+ def debug_s
86
+ "DEBUG "
87
+ end
88
+
89
+ def unknown_S
90
+ "UNKNOWN"
91
+ end
92
+
93
+ def info_s
94
+ @info_s ||= color("INFO ", '32m')
95
+ end
96
+
97
+ def warn_s
98
+ @warn_s ||= color("WARN ", '33m')
99
+ end
100
+
101
+ def error_s
102
+ @error_s ||= color("ERROR ", '31m')
103
+ end
104
+
105
+ def fatal_s
106
+ @fatal_s ||= color("FATAL ", '1;30;41m')
107
+ end
108
+
109
+ def color(s, color)
110
+ use_color? ? "\033[#{color}#{s}\033[0m" : s
111
+ end
112
+
113
+ end
114
+
115
+ module InstanceMethods
116
+
117
+ def add_with_formatting(severity, message = nil, progname = nill, &block)
118
+ return if @level > severity
119
+ message = format(severity, (message || (block && block.call) || progname).to_s)
120
+ add_without_formatting(severity, message, progname)
121
+ end
122
+
123
+ def format(severity, message)
124
+ "#{self.class.pid}#{self.class.hostname}#{self.class.timestamp}#{self.class.to_color(severity)} - #{trim_leading_whitespace(message)}"
125
+ end
126
+
127
+ def trim_leading_whitespace(m)
128
+ self.class.trim_leading_whitespace? ? m.gsub(/^\s*/,'') : m
129
+ end
130
+
131
+ end
132
+
133
+
134
+ end
135
+
136
+ ActiveSupport::BufferedLogger.send :include, FormatLogs
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+ require 'format_logs'
3
+
4
+ describe FormatLogs do
5
+ it "should have a VERSION constant" do
6
+ subject.const_get('VERSION').should_not be_empty
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ gem 'rspec', '~> 2.4'
2
+ require 'rspec'
3
+ require 'format_logs/version'
4
+
5
+ include FormatLogs
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: format_logs
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - goosetav
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-06-03 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rails
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 7
29
+ segments:
30
+ - 3
31
+ - 0
32
+ version: "3.0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: ore-tasks
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ - 4
47
+ version: "0.4"
48
+ type: :development
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: rspec
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ hash: 11
59
+ segments:
60
+ - 2
61
+ - 4
62
+ version: "2.4"
63
+ type: :development
64
+ version_requirements: *id003
65
+ description: |
66
+ Adds some simple formatting to the standard Rails BufferedLogger class.
67
+
68
+ This solution aims to be something between the default logging which is missing information such as
69
+ timestamps, process IDs and severity levels and a full logging solution like log4r
70
+
71
+ email: []
72
+
73
+ executables: []
74
+
75
+ extensions: []
76
+
77
+ extra_rdoc_files:
78
+ - README.rdoc
79
+ files:
80
+ - spec/spec_helper.rb
81
+ - gemspec.yml
82
+ - ChangeLog.rdoc
83
+ - .rspec
84
+ - lib/format_logs/version.rb
85
+ - .loadpath
86
+ - .project
87
+ - Rakefile
88
+ - LICENSE.txt
89
+ - README.rdoc
90
+ - spec/format_logs_spec.rb
91
+ - .document
92
+ - lib/format_logs.rb
93
+ - format_logs.gemspec
94
+ homepage: http://github.com/goosetav/format_logs
95
+ licenses:
96
+ - MIT
97
+ post_install_message: |
98
+ *********************************************
99
+
100
+ Add the following to your Gemfile to activate
101
+
102
+ gem "format_logs"
103
+
104
+ Configuration options can be set by:
105
+
106
+ FormatLogs.settings = {...}
107
+
108
+ see README.rdoc for more details
109
+
110
+ *********************************************
111
+
112
+ rdoc_options: []
113
+
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ hash: 3
122
+ segments:
123
+ - 0
124
+ version: "0"
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ hash: 3
131
+ segments:
132
+ - 0
133
+ version: "0"
134
+ requirements: []
135
+
136
+ rubyforge_project: format_logs
137
+ rubygems_version: 1.8.5
138
+ signing_key:
139
+ specification_version: 3
140
+ summary: Adds some basic formatting to the default rails logs
141
+ test_files:
142
+ - spec/format_logs_spec.rb