itslog 0.3.2 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +29 -18
- data/Rakefile +9 -0
- data/lib/itslog/configure.rb +16 -1
- data/lib/itslog/extension.rb +26 -4
- data/lib/itslog/railtie.rb +13 -5
- data/lib/itslog/version.rb +1 -1
- data/test/fixtures/article.rb +3 -0
- data/test/fixtures/itslog.sqlite3 +0 -0
- data/test/fixtures/log.txt +2 -0
- data/test/fixtures/schema.rb +6 -0
- data/test/helper.rb +28 -0
- data/test/itslog_test.rb +60 -0
- metadata +11 -5
data/README.md
CHANGED
@@ -3,11 +3,15 @@ itslog
|
|
3
3
|
|
4
4
|
`itslog` is a log formatter designed to aid rails development.
|
5
5
|
|
6
|
-
The formatting will prepend all log statements with a colored header and additional information about the statement.
|
6
|
+
The formatting will prepend all log statements with a colored header and additional information about the statement. The information and structure of the new statements is customizable through configuration.
|
7
7
|
|
8
8
|
[timestamp] [rails namespace] [log message], example:
|
9
9
|
|
10
|
-
|
10
|
+
![](http://cdn.lts.cr/files/bf23b77669a77fb444b6/itslog_example1.jpg)
|
11
|
+
|
12
|
+
Errors and warnings will also stand out, example:
|
13
|
+
|
14
|
+
![](http://cdn.lts.cr/files/bf23b77669a77fb444b6/itslog_example2.jpg)
|
11
15
|
|
12
16
|
In addition to more readable logs, you can tail and grep specific parts of your application. example:
|
13
17
|
|
@@ -20,9 +24,11 @@ Install
|
|
20
24
|
|
21
25
|
Add to your Gemfile in Rails:
|
22
26
|
|
23
|
-
|
24
|
-
|
25
|
-
|
27
|
+
``` ruby
|
28
|
+
group :development, :test do
|
29
|
+
gem 'itslog'
|
30
|
+
end
|
31
|
+
```
|
26
32
|
|
27
33
|
Configure
|
28
34
|
-----------
|
@@ -31,14 +37,17 @@ Itslog does not need to be configured unless you want to customize the output st
|
|
31
37
|
|
32
38
|
Example:
|
33
39
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
40
|
+
``` ruby
|
41
|
+
Itslog.configure do |config|
|
42
|
+
config.namespace_colors = {
|
43
|
+
'action_controller' => "\e[32m",
|
44
|
+
'active_record' => "\e[94m",
|
45
|
+
'mongo' => "\e[94m",
|
46
|
+
'action_view' => "\e[36m"
|
47
|
+
}
|
48
|
+
config.format = "%t [%n]: %m"
|
49
|
+
end
|
50
|
+
```
|
42
51
|
|
43
52
|
Configure format by building a string anyway you'd like and using the following variables:
|
44
53
|
|
@@ -48,11 +57,13 @@ Configure format by building a string anyway you'd like and using the following
|
|
48
57
|
|
49
58
|
I don't recommend coloring by severity because it's not very useful. To color by severity instead of the default of namespace:
|
50
59
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
60
|
+
``` ruby
|
61
|
+
Itslog.configure do |config|
|
62
|
+
config.color_by :severity
|
63
|
+
# debug, info, warning, error, fatal, unknown
|
64
|
+
config.severity_colors = ["\e[36m","\e[32m","\e[33m","\e[31m","\e[31m","\e[37m"]
|
65
|
+
end
|
66
|
+
```
|
56
67
|
|
57
68
|
Place the configuration in an initializer:
|
58
69
|
|
data/Rakefile
CHANGED
data/lib/itslog/configure.rb
CHANGED
@@ -8,13 +8,14 @@ module Itslog
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def format
|
11
|
-
@format ||= '%t %
|
11
|
+
@format ||= '%t %n_%m'
|
12
12
|
end
|
13
13
|
|
14
14
|
def namespace_colors
|
15
15
|
@namespace_colors ||= {
|
16
16
|
'action_controller' => "\e[32m",
|
17
17
|
'active_record' => "\e[94m",
|
18
|
+
'mongo' => "\e[94m",
|
18
19
|
'action_view' => "\e[36m"}
|
19
20
|
end
|
20
21
|
|
@@ -35,5 +36,19 @@ module Itslog
|
|
35
36
|
def configure
|
36
37
|
yield self
|
37
38
|
end
|
39
|
+
|
40
|
+
def reset
|
41
|
+
configure do |config|
|
42
|
+
config.color_by = :namespace
|
43
|
+
config.format = '%t %n_%m'
|
44
|
+
config.namespace_colors = {
|
45
|
+
'action_controller' => "\e[32m",
|
46
|
+
'active_record' => "\e[94m",
|
47
|
+
'mongo' => "\e[94m",
|
48
|
+
'action_view' => "\e[36m"}
|
49
|
+
config.severity_colors = [
|
50
|
+
"\e[36m","\e[32m","\e[33m","\e[31m","\e[31m","\e[37m"]
|
51
|
+
end
|
52
|
+
end
|
38
53
|
end
|
39
54
|
end
|
data/lib/itslog/extension.rb
CHANGED
@@ -9,17 +9,24 @@ module Itslog
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def add_with_format(severity, message = nil, progname = nil, &block)
|
12
|
-
return if @level > severity
|
12
|
+
return if @level > severity || message.nil?
|
13
13
|
|
14
14
|
time = Time.now.to_s(:db).split.last
|
15
15
|
message = "\e[37m" + message.to_s.strip
|
16
|
-
msg = ''
|
16
|
+
msg = namespace.present? ? '' : "\n"
|
17
17
|
msg << Itslog::Configure.color(namespace, severity)
|
18
18
|
msg << Itslog::Configure.format.dup
|
19
|
-
|
20
|
-
|
19
|
+
|
20
|
+
{ '%t' => time,
|
21
|
+
'%n_' => namespace.present? ? namespace + ' ' : namespace,
|
22
|
+
'%n' => namespace,
|
23
|
+
'%m' => message
|
24
|
+
}.each do |k,v|
|
25
|
+
msg.gsub! k,v
|
21
26
|
end
|
22
27
|
|
28
|
+
@namespace = ''
|
29
|
+
|
23
30
|
add_without_format severity, msg, progname, &block
|
24
31
|
end
|
25
32
|
|
@@ -44,3 +51,18 @@ module Itslog
|
|
44
51
|
end
|
45
52
|
end
|
46
53
|
end
|
54
|
+
|
55
|
+
if defined? Mongoid::Logger
|
56
|
+
class Mongoid::Logger
|
57
|
+
delegate :namespace=, :to => :logger, :allow_nil => true
|
58
|
+
end
|
59
|
+
|
60
|
+
class Mongo::Connection
|
61
|
+
def log_operation_with_namespace(name, payload)
|
62
|
+
@logger.namespace = 'mongo' if @logger
|
63
|
+
log_operation_without_namespace(name, payload)
|
64
|
+
end
|
65
|
+
|
66
|
+
alias_method_chain :log_operation, :namespace
|
67
|
+
end
|
68
|
+
end
|
data/lib/itslog/railtie.rb
CHANGED
@@ -1,7 +1,15 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
module Itslog
|
2
|
+
class Railtie < Rails::Railtie
|
3
|
+
config.before_initialize do
|
4
|
+
Itslog::Railtie.insert
|
5
|
+
ActiveSupport::LogSubscriber.colorize_logging = false
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class Railtie
|
10
|
+
def self.insert
|
11
|
+
ActiveSupport::BufferedLogger.send(:include, Itslog::BufferedLoggerExtension)
|
12
|
+
ActiveSupport::LogSubscriber.send(:include, Itslog::LogSubscriberExtension)
|
13
|
+
end
|
6
14
|
end
|
7
15
|
end
|
data/lib/itslog/version.rb
CHANGED
Binary file
|
data/test/helper.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'active_support'
|
3
|
+
require 'test/unit'
|
4
|
+
require 'timecop'
|
5
|
+
require 'redgreen'
|
6
|
+
require 'itslog'
|
7
|
+
|
8
|
+
ActiveRecord::Base.establish_connection \
|
9
|
+
:adapter => 'sqlite3',
|
10
|
+
:database => File.dirname(__FILE__) + '/fixtures/itslog.sqlite3'
|
11
|
+
load File.dirname(__FILE__) + '/fixtures/schema.rb'
|
12
|
+
load File.dirname(__FILE__) + '/fixtures/article.rb'
|
13
|
+
|
14
|
+
Itslog::Railtie.insert
|
15
|
+
|
16
|
+
def assert_log(log, msg, severity=0, namespace=nil)
|
17
|
+
level = [:debug, :info, :warn, :error, :fatal, :unknown]
|
18
|
+
Timecop.freeze(Time.parse('01:01:01'))
|
19
|
+
file_name = File.dirname(__FILE__) + '/fixtures/log.txt'
|
20
|
+
File.delete(file_name)
|
21
|
+
Rails.logger = ActiveSupport::BufferedLogger.new(file_name)
|
22
|
+
Rails.logger.namespace = namespace if namespace.present?
|
23
|
+
Rails.logger.send(level[severity], msg)
|
24
|
+
assert_equal log, File.read(file_name)
|
25
|
+
Timecop.return
|
26
|
+
end
|
27
|
+
|
28
|
+
class Test::Unit::TestCase; end
|
data/test/itslog_test.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
$LOAD_PATH.unshift File.dirname(__FILE__)
|
2
|
+
require 'helper'
|
3
|
+
|
4
|
+
class ItslogTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_default_severity_color
|
7
|
+
Itslog::Configure.reset
|
8
|
+
assert_log "\n\e[37m01:01:01 \e[37mtest\n", 'test', 0
|
9
|
+
assert_log "\n\e[37m01:01:01 \e[37mtest\n", 'test', 1
|
10
|
+
assert_log "\n\e[33m01:01:01 \e[37mtest\n", 'test', 2
|
11
|
+
assert_log "\n\e[31m01:01:01 \e[37mtest\n", 'test', 3
|
12
|
+
assert_log "\n\e[31m01:01:01 \e[37mtest\n", 'test', 4
|
13
|
+
assert_log "\n\e[37m01:01:01 \e[37mtest\n", 'test', 5
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_custom_severity_color
|
17
|
+
Itslog::Configure.reset
|
18
|
+
Itslog.configure do |config|
|
19
|
+
config.severity_colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo']
|
20
|
+
config.color_by = :severity
|
21
|
+
end
|
22
|
+
assert_log "\nred01:01:01 \e[37mtest\n" , 'test', 0
|
23
|
+
assert_log "\norange01:01:01 \e[37mtest\n", 'test', 1
|
24
|
+
assert_log "\nyellow01:01:01 \e[37mtest\n", 'test', 2
|
25
|
+
assert_log "\ngreen01:01:01 \e[37mtest\n" , 'test', 3
|
26
|
+
assert_log "\nblue01:01:01 \e[37mtest\n" , 'test', 4
|
27
|
+
assert_log "\nindigo01:01:01 \e[37mtest\n", 'test', 5
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_default_namespaces
|
31
|
+
Itslog::Configure.reset
|
32
|
+
assert_log "\e[32m01:01:01 action_controller \e[37mtest\n", 'test', 0, 'action_controller'
|
33
|
+
assert_log "\e[36m01:01:01 action_view \e[37mtest\n" , 'test', 0, 'action_view'
|
34
|
+
assert_log "\e[94m01:01:01 active_record \e[37mtest\n" , 'test', 0, 'active_record'
|
35
|
+
assert_log "\e[94m01:01:01 mongo \e[37mtest\n" , 'test', 0, 'mongo'
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_custom_namespace_color
|
39
|
+
Itslog::Configure.reset
|
40
|
+
Itslog.configure do |config|
|
41
|
+
config.format = '%n %m'
|
42
|
+
config.namespace_colors = {
|
43
|
+
'action_controller' => 'red',
|
44
|
+
'action_view' => 'orange',
|
45
|
+
'active_record' => 'yellow',
|
46
|
+
'mongo' => 'green'}
|
47
|
+
end
|
48
|
+
assert_log "redaction_controller \e[37mtest\n", 'test', 0, 'action_controller'
|
49
|
+
assert_log "orangeaction_view \e[37mtest\n" , 'test', 0, 'action_view'
|
50
|
+
assert_log "yellowactive_record \e[37mtest\n" , 'test', 0, 'active_record'
|
51
|
+
assert_log "greenmongo \e[37mtest\n" , 'test', 0, 'mongo'
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_custom_format
|
55
|
+
Itslog::Configure.reset
|
56
|
+
Itslog.configure { |config| config.format = '[%n] %m' }
|
57
|
+
assert_log "\n\e[37m[] \e[37mtest\n", 'test', 0
|
58
|
+
assert_log "\e[32m[action_controller] \e[37mtest\n", 'test', 0, 'action_controller'
|
59
|
+
end
|
60
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: itslog
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 11
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 5
|
9
|
+
- 0
|
10
|
+
version: 0.5.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- John Thomas Marino
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-06-28 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -36,6 +36,12 @@ files:
|
|
36
36
|
- lib/itslog/railtie.rb
|
37
37
|
- lib/itslog/version.rb
|
38
38
|
- lib/itslog.rb
|
39
|
+
- test/fixtures/article.rb
|
40
|
+
- test/fixtures/itslog.sqlite3
|
41
|
+
- test/fixtures/log.txt
|
42
|
+
- test/fixtures/schema.rb
|
43
|
+
- test/helper.rb
|
44
|
+
- test/itslog_test.rb
|
39
45
|
has_rdoc: true
|
40
46
|
homepage: http://github.com/johmas/itslog
|
41
47
|
licenses: []
|