yell 1.4.0 → 2.2.2
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 +4 -4
- data/.gitignore +6 -2
- data/.ruby-version +1 -0
- data/.travis.yml +16 -13
- data/Gemfile +15 -8
- data/LICENSE.txt +1 -1
- data/README.md +10 -3
- data/Rakefile +1 -3
- data/examples/002.1-log-level-basics.rb +2 -2
- data/examples/002.2-log-level-on-certain-severities-only.rb +5 -4
- data/examples/002.3-log-level-within-range.rb +7 -5
- data/examples/003.1-formatting-DefaultFormat.rb +3 -3
- data/examples/003.2-formatting-BasicFormat.rb +3 -3
- data/examples/003.3-formatting-ExtendedFormat.rb +3 -3
- data/examples/003.4-formatting-on-your-own.rb +3 -3
- data/examples/004.1-colorizing-the-log-output.rb +3 -3
- data/examples/005.1-repository.rb +3 -3
- data/examples/006.1-the-loggable-module.rb +2 -2
- data/examples/006.2-the-loggable-module-with-inheritance.rb +2 -2
- data/lib/core_ext/logger.rb +17 -0
- data/lib/yell/adapters/base.rb +17 -22
- data/lib/yell/adapters/datefile.rb +23 -22
- data/lib/yell/adapters/file.rb +3 -7
- data/lib/yell/adapters/io.rb +9 -15
- data/lib/yell/adapters/streams.rb +0 -5
- data/lib/yell/adapters.rb +41 -10
- data/lib/yell/configuration.rb +2 -4
- data/lib/yell/event.rb +46 -10
- data/lib/yell/formatter.rb +134 -54
- data/lib/yell/helpers/{adapters.rb → adapter.rb} +7 -17
- data/lib/yell/helpers/base.rb +0 -1
- data/lib/yell/helpers/formatter.rb +8 -8
- data/lib/yell/helpers/level.rb +7 -4
- data/lib/yell/helpers/silencer.rb +3 -5
- data/lib/yell/helpers/tracer.rb +6 -7
- data/lib/yell/level.rb +19 -16
- data/lib/yell/loggable.rb +11 -7
- data/lib/yell/logger.rb +47 -68
- data/lib/yell/repository.rb +3 -5
- data/lib/yell/silencer.rb +30 -14
- data/lib/yell/version.rb +1 -4
- data/lib/yell.rb +29 -11
- data/yell.gemspec +22 -15
- metadata +19 -50
- data/spec/fixtures/yell.yml +0 -7
- data/spec/spec_helper.rb +0 -50
- data/spec/threaded/yell_spec.rb +0 -100
- data/spec/yell/adapters/base_spec.rb +0 -43
- data/spec/yell/adapters/datefile_spec.rb +0 -168
- data/spec/yell/adapters/file_spec.rb +0 -75
- data/spec/yell/adapters/io_spec.rb +0 -72
- data/spec/yell/adapters/streams_spec.rb +0 -26
- data/spec/yell/adapters_spec.rb +0 -45
- data/spec/yell/configuration_spec.rb +0 -36
- data/spec/yell/event_spec.rb +0 -97
- data/spec/yell/formatter_spec.rb +0 -136
- data/spec/yell/level_spec.rb +0 -200
- data/spec/yell/loggable_spec.rb +0 -20
- data/spec/yell/logger_spec.rb +0 -263
- data/spec/yell/repository_spec.rb +0 -70
- data/spec/yell/silencer_spec.rb +0 -49
- data/spec/yell_spec.rb +0 -102
data/lib/yell/adapters/file.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
1
|
module Yell #:nodoc:
|
4
2
|
module Adapters #:nodoc:
|
5
3
|
|
@@ -11,14 +9,14 @@ module Yell #:nodoc:
|
|
11
9
|
|
12
10
|
# @overload setup!( options )
|
13
11
|
def setup!( options )
|
14
|
-
@filename = ::File.expand_path
|
12
|
+
@filename = ::File.expand_path(Yell.__fetch__(options, :filename, default: default_filename))
|
15
13
|
|
16
14
|
super
|
17
15
|
end
|
18
16
|
|
19
17
|
# @overload open!
|
20
18
|
def open!
|
21
|
-
@stream = ::File.open(
|
19
|
+
@stream = ::File.open(@filename, ::File::WRONLY|::File::APPEND|::File::CREAT)
|
22
20
|
|
23
21
|
super
|
24
22
|
end
|
@@ -26,13 +24,11 @@ module Yell #:nodoc:
|
|
26
24
|
def default_filename #:nodoc:
|
27
25
|
logdir = ::File.expand_path("log")
|
28
26
|
|
29
|
-
::File.expand_path
|
27
|
+
::File.expand_path(::File.directory?(logdir) ? "#{logdir}/#{Yell.env}.log" : "#{Yell.env}.log")
|
30
28
|
end
|
31
29
|
|
32
30
|
end
|
33
31
|
|
34
|
-
register( :file, Yell::Adapters::File )
|
35
|
-
|
36
32
|
end
|
37
33
|
end
|
38
34
|
|
data/lib/yell/adapters/io.rb
CHANGED
@@ -1,8 +1,5 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
1
|
module Yell #:nodoc:
|
4
2
|
module Adapters #:nodoc:
|
5
|
-
|
6
3
|
class Io < Yell::Adapters::Base
|
7
4
|
include Yell::Helpers::Formatter
|
8
5
|
|
@@ -45,39 +42,38 @@ module Yell #:nodoc:
|
|
45
42
|
def setup!( options )
|
46
43
|
@stream = nil
|
47
44
|
|
48
|
-
self.colors =
|
49
|
-
self.
|
50
|
-
self.sync =
|
45
|
+
self.colors = Yell.__fetch__(options, :colors, default: false)
|
46
|
+
self.formatter = Yell.__fetch__(options, :format, :formatter)
|
47
|
+
self.sync = Yell.__fetch__(options, :sync, default: true)
|
51
48
|
|
52
49
|
super
|
53
50
|
end
|
54
51
|
|
55
52
|
# @overload write!( event )
|
56
53
|
def write!( event )
|
57
|
-
message =
|
54
|
+
message = formatter.call(event)
|
58
55
|
|
59
56
|
# colorize if applicable
|
60
57
|
if colors and color = TTYColors[event.level]
|
61
58
|
message = color + message + TTYColors[-1]
|
62
59
|
end
|
63
60
|
|
64
|
-
message
|
65
|
-
stream.syswrite( message )
|
61
|
+
stream.syswrite(message)
|
66
62
|
|
67
63
|
super
|
68
64
|
end
|
69
65
|
|
70
66
|
# @overload open!
|
71
67
|
def open!
|
72
|
-
@stream.sync = self.sync if @stream.respond_to?
|
73
|
-
@stream.flush
|
68
|
+
@stream.sync = self.sync if @stream.respond_to?(:sync)
|
69
|
+
@stream.flush if @stream.respond_to?(:flush)
|
74
70
|
|
75
71
|
super
|
76
72
|
end
|
77
73
|
|
78
74
|
# @overload close!
|
79
75
|
def close!
|
80
|
-
@stream.close if @stream.respond_to?
|
76
|
+
@stream.close if @stream.respond_to?(:close)
|
81
77
|
@stream = nil
|
82
78
|
|
83
79
|
super
|
@@ -93,11 +89,9 @@ module Yell #:nodoc:
|
|
93
89
|
|
94
90
|
# @overload inspectables
|
95
91
|
def inspectables
|
96
|
-
super.concat [:
|
92
|
+
super.concat [:formatter, :colors, :sync]
|
97
93
|
end
|
98
|
-
|
99
94
|
end
|
100
|
-
|
101
95
|
end
|
102
96
|
end
|
103
97
|
|
data/lib/yell/adapters.rb
CHANGED
@@ -1,6 +1,4 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
module Yell #:nodoc:
|
3
|
-
|
4
2
|
# AdapterNotFound is raised whenever you want to instantiate an
|
5
3
|
# adapter that does not exist.
|
6
4
|
class AdapterNotFound < StandardError; end
|
@@ -9,6 +7,41 @@ module Yell #:nodoc:
|
|
9
7
|
# the logger. You should not have to call the corresponding classes
|
10
8
|
# directly.
|
11
9
|
module Adapters
|
10
|
+
class Collection
|
11
|
+
def initialize( options = {} )
|
12
|
+
@options = options
|
13
|
+
@collection = []
|
14
|
+
end
|
15
|
+
|
16
|
+
def add( type = :file, *args, &block )
|
17
|
+
options = [@options, *args].inject(Hash.new) do |h, c|
|
18
|
+
h.merge( [String, Pathname].include?(c.class) ? {:filename => c} : c )
|
19
|
+
end
|
20
|
+
|
21
|
+
# remove possible :null adapters
|
22
|
+
@collection.shift if @collection.first.instance_of?(Yell::Adapters::Base)
|
23
|
+
|
24
|
+
new_adapter = Yell::Adapters.new(type, options, &block)
|
25
|
+
@collection.push(new_adapter)
|
26
|
+
|
27
|
+
new_adapter
|
28
|
+
end
|
29
|
+
|
30
|
+
def empty?
|
31
|
+
@collection.empty?
|
32
|
+
end
|
33
|
+
|
34
|
+
# @private
|
35
|
+
def write( event )
|
36
|
+
@collection.each { |c| c.write(event) }
|
37
|
+
true
|
38
|
+
end
|
39
|
+
|
40
|
+
# @private
|
41
|
+
def close
|
42
|
+
@collection.each { |c| c.close }
|
43
|
+
end
|
44
|
+
end
|
12
45
|
|
13
46
|
# holds the list of known adapters
|
14
47
|
@adapters = {}
|
@@ -18,27 +51,25 @@ module Yell #:nodoc:
|
|
18
51
|
# @example
|
19
52
|
# Yell::Adapters.register( :myadapter, MyAdapter )
|
20
53
|
def self.register( name, klass )
|
21
|
-
@adapters[name] = klass
|
54
|
+
@adapters[name.to_sym] = klass
|
22
55
|
end
|
23
56
|
|
24
57
|
# Returns an instance of the given processor type.
|
25
58
|
#
|
26
59
|
# @example A simple file adapter
|
27
60
|
# Yell::Adapters.new( :file )
|
28
|
-
def self.new(
|
29
|
-
return
|
61
|
+
def self.new( type, options = {}, &block )
|
62
|
+
return type if type.is_a?(Yell::Adapters::Base)
|
30
63
|
|
31
|
-
adapter = case
|
64
|
+
adapter = case type
|
32
65
|
when STDOUT then @adapters[:stdout]
|
33
66
|
when STDERR then @adapters[:stderr]
|
34
|
-
else @adapters[
|
67
|
+
else @adapters[type.to_sym]
|
35
68
|
end
|
36
69
|
|
37
|
-
raise AdapterNotFound.new(
|
38
|
-
|
70
|
+
raise AdapterNotFound.new(type) if adapter.nil?
|
39
71
|
adapter.new(options, &block)
|
40
72
|
end
|
41
|
-
|
42
73
|
end
|
43
74
|
end
|
44
75
|
|
data/lib/yell/configuration.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
1
|
require 'erb'
|
4
2
|
require 'yaml'
|
5
3
|
|
@@ -13,8 +11,8 @@ module Yell #:nodoc:
|
|
13
11
|
yaml = YAML.load( ERB.new(File.read(file)).result )
|
14
12
|
|
15
13
|
# in case we have ActiveSupport
|
16
|
-
if
|
17
|
-
yaml = yaml
|
14
|
+
if defined?(ActiveSupport::HashWithIndifferentAccess)
|
15
|
+
yaml = ActiveSupport::HashWithIndifferentAccess.new(yaml)
|
18
16
|
end
|
19
17
|
|
20
18
|
yaml[Yell.env] || {}
|
data/lib/yell/event.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
1
|
require 'time'
|
4
2
|
require 'socket'
|
5
3
|
|
@@ -14,6 +12,26 @@ module Yell #:nodoc:
|
|
14
12
|
# jruby and rubinius seem to have a different caller
|
15
13
|
CallerIndex = defined?(RUBY_ENGINE) && ["rbx", "jruby"].include?(RUBY_ENGINE) ? 1 : 2
|
16
14
|
|
15
|
+
|
16
|
+
class Options
|
17
|
+
include Comparable
|
18
|
+
|
19
|
+
attr_reader :severity
|
20
|
+
attr_reader :caller_offset
|
21
|
+
|
22
|
+
def initialize( severity, caller_offset )
|
23
|
+
@severity = severity
|
24
|
+
@caller_offset = caller_offset
|
25
|
+
end
|
26
|
+
|
27
|
+
def <=>( other )
|
28
|
+
@severity <=> other
|
29
|
+
end
|
30
|
+
|
31
|
+
alias :to_i :severity
|
32
|
+
alias :to_int :severity
|
33
|
+
end
|
34
|
+
|
17
35
|
# Prefetch those values (no need to do that on every new instance)
|
18
36
|
@@hostname = Socket.gethostname rescue nil
|
19
37
|
@@progname = $0
|
@@ -21,23 +39,27 @@ module Yell #:nodoc:
|
|
21
39
|
# Accessor to the log level
|
22
40
|
attr_reader :level
|
23
41
|
|
24
|
-
# Accessor to the log
|
42
|
+
# Accessor to the log message
|
25
43
|
attr_reader :messages
|
26
44
|
|
27
45
|
# Accessor to the time the log event occured
|
28
46
|
attr_reader :time
|
29
47
|
|
48
|
+
# Accessor to the logger's name
|
49
|
+
attr_reader :name
|
50
|
+
|
51
|
+
|
52
|
+
def initialize( logger, options, *messages)
|
53
|
+
@time = Time.now
|
54
|
+
@name = logger.name
|
30
55
|
|
31
|
-
|
32
|
-
@time = Time.now
|
33
|
-
@level = level
|
56
|
+
extract!(options)
|
34
57
|
|
35
58
|
@messages = messages
|
36
|
-
@messages << block.call if block
|
37
59
|
|
38
|
-
@caller = logger.trace.at?(level) ? caller[
|
39
|
-
@file
|
40
|
-
@line
|
60
|
+
@caller = logger.trace.at?(level) ? caller[caller_index].to_s : ''
|
61
|
+
@file = nil
|
62
|
+
@line = nil
|
41
63
|
@method = nil
|
42
64
|
|
43
65
|
@pid = nil
|
@@ -81,6 +103,20 @@ module Yell #:nodoc:
|
|
81
103
|
|
82
104
|
private
|
83
105
|
|
106
|
+
def extract!( options )
|
107
|
+
if options.is_a?(Yell::Event::Options)
|
108
|
+
@level = options.severity
|
109
|
+
@caller_offset = options.caller_offset
|
110
|
+
else
|
111
|
+
@level = options
|
112
|
+
@caller_offset = 0
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def caller_index
|
117
|
+
CallerIndex + @caller_offset
|
118
|
+
end
|
119
|
+
|
84
120
|
def backtrace!
|
85
121
|
if m = CallerRegexp.match(@caller)
|
86
122
|
@file, @line, @method = m[1..-1]
|
data/lib/yell/formatter.rb
CHANGED
@@ -1,12 +1,19 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
require 'time'
|
3
2
|
|
3
|
+
# TODO: Register custom formats
|
4
|
+
#
|
5
|
+
# @example The Yell default fomat
|
6
|
+
# Yell::Formatter.register(:default)
|
7
|
+
#
|
8
|
+
# @example The Ruby standard logger format
|
9
|
+
# Yell::Formatter.register(:stdlogger, "%l, [%d #%p] %5L -- : %m", "%Y-%m-%dT%H:%M:%S.%6N")
|
10
|
+
#
|
4
11
|
module Yell #:nodoc:
|
5
12
|
|
6
13
|
# No format on the log message
|
7
14
|
#
|
8
15
|
# @example
|
9
|
-
# logger = Yell.new STDOUT, :
|
16
|
+
# logger = Yell.new STDOUT, format: false
|
10
17
|
# logger.info "Hello World!"
|
11
18
|
# #=> "Hello World!"
|
12
19
|
NoFormat = "%m"
|
@@ -14,7 +21,7 @@ module Yell #:nodoc:
|
|
14
21
|
# Default Format
|
15
22
|
#
|
16
23
|
# @example
|
17
|
-
# logger = Yell.new STDOUT, :
|
24
|
+
# logger = Yell.new STDOUT, format: Yell::DefaultFormat
|
18
25
|
# logger.info "Hello World!"
|
19
26
|
# #=> "2012-02-29T09:30:00+01:00 [ INFO] 65784 : Hello World!"
|
20
27
|
# # ^ ^ ^ ^
|
@@ -24,7 +31,7 @@ module Yell #:nodoc:
|
|
24
31
|
# Basic Format
|
25
32
|
#
|
26
33
|
# @example
|
27
|
-
# logger = Yell.new STDOUT, :
|
34
|
+
# logger = Yell.new STDOUT, format: Yell::BasicFormat
|
28
35
|
# logger.info "Hello World!"
|
29
36
|
# #=> "I, 2012-02-29T09:30:00+01:00 : Hello World!"
|
30
37
|
# # ^ ^ ^
|
@@ -35,7 +42,7 @@ module Yell #:nodoc:
|
|
35
42
|
# Extended Format
|
36
43
|
#
|
37
44
|
# @example
|
38
|
-
# logger = Yell.new STDOUT, :
|
45
|
+
# logger = Yell.new STDOUT, format: Yell::ExtendedFormat
|
39
46
|
# logger.info "Hello World!"
|
40
47
|
# #=> "2012-02-29T09:30:00+01:00 [ INFO] 65784 localhost : Hello World!"
|
41
48
|
# # ^ ^ ^ ^ ^
|
@@ -47,8 +54,8 @@ module Yell #:nodoc:
|
|
47
54
|
class Formatter
|
48
55
|
|
49
56
|
Table = {
|
50
|
-
"m" => "message(
|
51
|
-
"l" => "level(event.level
|
57
|
+
"m" => "message(event.messages)", # Message
|
58
|
+
"l" => "level(event.level, 1)", # Level (short), e.g.'I', 'W'
|
52
59
|
"L" => "level(event.level)", # Level, e.g. 'INFO', 'WARN'
|
53
60
|
"d" => "date(event.time)", # ISO8601 Timestamp
|
54
61
|
"h" => "event.hostname", # Hostname
|
@@ -58,23 +65,54 @@ module Yell #:nodoc:
|
|
58
65
|
"F" => "event.file", # Path with filename where the logger was called
|
59
66
|
"f" => "File.basename(event.file)", # Filename where the loger was called
|
60
67
|
"M" => "event.method", # Method name where the logger was called
|
61
|
-
"n" => "event.line"
|
68
|
+
"n" => "event.line", # Line where the logger was called
|
69
|
+
"N" => "event.name" # Name of the logger
|
62
70
|
}
|
63
71
|
|
64
|
-
|
72
|
+
# For standard formatted backwards compatibility
|
73
|
+
LegacyTable = Hash[ Table.keys.map { |k| [k, 'noop'] } ].merge(
|
74
|
+
'm' => 'message(msg)',
|
75
|
+
'l' => 'level(event, 1)',
|
76
|
+
'L' => 'level(event)',
|
77
|
+
'd' => 'date(time)',
|
78
|
+
"p" => "$$",
|
79
|
+
'P' => 'progname'
|
80
|
+
)
|
81
|
+
|
82
|
+
PatternMatcher = /([^%]*)(%\d*)?(#{Table.keys.join('|')})?(.*)/m
|
83
|
+
|
84
|
+
|
85
|
+
attr_reader :pattern, :date_pattern
|
65
86
|
|
66
87
|
|
67
88
|
# Initializes a new +Yell::Formatter+.
|
68
89
|
#
|
69
|
-
# Upon initialization it defines a format method. `format` takes
|
70
|
-
# a {Yell::Event} instance as agument in order to apply for desired log
|
90
|
+
# Upon initialization it defines a format method. `format` takes
|
91
|
+
# a {Yell::Event} instance as agument in order to apply for desired log
|
71
92
|
# message formatting.
|
72
|
-
|
73
|
-
|
74
|
-
|
93
|
+
#
|
94
|
+
# @example Blank formatter
|
95
|
+
# Formatter.new
|
96
|
+
#
|
97
|
+
# @example Formatter with a message pattern
|
98
|
+
# Formatter.new("%d [%5L] %p : %m")
|
99
|
+
#
|
100
|
+
# @example Formatter with a message and date pattern
|
101
|
+
# Formatter.new("%d [%5L] %p : %m", "%D %H:%M:%S.%L")
|
102
|
+
#
|
103
|
+
# @example Formatter with a message modifier
|
104
|
+
# Formatter.new do |f|
|
105
|
+
# f.modify(Hash) { |h| "Hash: #{h.inspect}" }
|
106
|
+
# end
|
107
|
+
def initialize( *args, &block )
|
108
|
+
builder = Builder.new(*args, &block)
|
109
|
+
|
110
|
+
@pattern = builder.pattern
|
111
|
+
@date_pattern = builder.date_pattern
|
112
|
+
@modifier = builder.modifier
|
75
113
|
|
76
114
|
define_date_method!
|
77
|
-
|
115
|
+
define_call_method!
|
78
116
|
end
|
79
117
|
|
80
118
|
# Get a pretty string
|
@@ -85,75 +123,117 @@ module Yell #:nodoc:
|
|
85
123
|
|
86
124
|
private
|
87
125
|
|
126
|
+
# Message modifier class to allow different modifiers for different requirements.
|
127
|
+
class Modifier
|
128
|
+
def initialize
|
129
|
+
@repository = {}
|
130
|
+
end
|
131
|
+
|
132
|
+
def set( key, &block )
|
133
|
+
@repository.merge!(key => block)
|
134
|
+
end
|
135
|
+
|
136
|
+
def call( message )
|
137
|
+
case
|
138
|
+
when mod = @repository[message.class] || @repository[message.class.to_s]
|
139
|
+
mod.call(message)
|
140
|
+
when message.is_a?(Array)
|
141
|
+
message.map { |m| call(m) }.join(" ")
|
142
|
+
when message.is_a?(Hash)
|
143
|
+
message.map { |k, v| "#{k}: #{v}" }.join(", ")
|
144
|
+
when message.is_a?(Exception)
|
145
|
+
backtrace = message.backtrace ? "\n\t#{message.backtrace.join("\n\t")}" : ""
|
146
|
+
sprintf("%s: %s%s", message.class, message.message, backtrace)
|
147
|
+
else
|
148
|
+
message
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
# Builder class to allow setters that won't be accessible once
|
154
|
+
# transferred to the Formatter
|
155
|
+
class Builder
|
156
|
+
attr_accessor :pattern, :date_pattern
|
157
|
+
attr_reader :modifier
|
158
|
+
|
159
|
+
def initialize( pattern = nil, date_pattern = nil, &block )
|
160
|
+
@modifier = Modifier.new
|
161
|
+
|
162
|
+
@pattern = case pattern
|
163
|
+
when false then Yell::NoFormat
|
164
|
+
when nil then Yell::DefaultFormat
|
165
|
+
else pattern
|
166
|
+
end.dup
|
167
|
+
|
168
|
+
@pattern << "\n" unless @pattern[-1] == ?\n # add newline if not present
|
169
|
+
@date_pattern = date_pattern || :iso8601
|
170
|
+
|
171
|
+
block.call(self) if block
|
172
|
+
end
|
173
|
+
|
174
|
+
def modify( key, &block )
|
175
|
+
modifier.set(key, &block)
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
88
179
|
def define_date_method!
|
89
180
|
buf = case @date_pattern
|
90
181
|
when String then "t.strftime(@date_pattern)"
|
91
182
|
when Symbol then respond_to?(@date_pattern, true) ? "#{@date_pattern}(t)" : "t.#{@date_pattern}"
|
92
|
-
else
|
183
|
+
else t.iso8601
|
93
184
|
end
|
94
185
|
|
95
|
-
|
96
|
-
|
186
|
+
# define the method
|
187
|
+
instance_eval <<-METHOD, __FILE__, __LINE__
|
188
|
+
def date(t = Time.now)
|
97
189
|
#{buf}
|
98
190
|
end
|
99
|
-
|
191
|
+
METHOD
|
192
|
+
end
|
193
|
+
|
194
|
+
# define a standard +Logger+ backwards compatible #call method for the formatter
|
195
|
+
def define_call_method!
|
196
|
+
instance_eval <<-METHOD, __FILE__, __LINE__
|
197
|
+
def call(event, time = nil, progname = nil, msg = nil)
|
198
|
+
event.is_a?(Yell::Event) ? #{to_sprintf(Table)} : #{to_sprintf(LegacyTable)}
|
199
|
+
end
|
200
|
+
METHOD
|
100
201
|
end
|
101
202
|
|
102
|
-
def
|
203
|
+
def to_sprintf( table )
|
103
204
|
buff, args, _pattern = "", [], @pattern.dup
|
104
205
|
|
105
206
|
while true
|
106
|
-
match =
|
207
|
+
match = PatternMatcher.match(_pattern)
|
107
208
|
|
108
209
|
buff << match[1] unless match[1].empty?
|
109
210
|
break if match[2].nil?
|
110
211
|
|
111
212
|
buff << match[2] + 's'
|
112
|
-
args <<
|
213
|
+
args << table[ match[3] ]
|
113
214
|
|
114
215
|
_pattern = match[4]
|
115
216
|
end
|
116
217
|
|
117
|
-
|
118
|
-
def format( event )
|
119
|
-
sprintf("#{buff}", #{args.join(',')})
|
120
|
-
end
|
121
|
-
EOS
|
218
|
+
%Q{sprintf("#{buff.gsub(/"/, '\"')}", #{args.join(', ')})}
|
122
219
|
end
|
123
220
|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
zone = if t.utc?
|
129
|
-
"-00:00"
|
130
|
-
else
|
131
|
-
offset = t.utc_offset
|
132
|
-
sign = offset < 0 ? '-' : '+'
|
133
|
-
sprintf('%s%02d:%02d', sign, *(offset.abs/60).divmod(60))
|
221
|
+
def level( sev, length = nil )
|
222
|
+
severity = case sev
|
223
|
+
when Integer then Yell::Severities[sev] || 'ANY'
|
224
|
+
else sev
|
134
225
|
end
|
135
226
|
|
136
|
-
|
137
|
-
end
|
138
|
-
|
139
|
-
def level( l )
|
140
|
-
Yell::Severities[ l ]
|
227
|
+
length.nil? ? severity : severity[0, length]
|
141
228
|
end
|
142
229
|
|
143
|
-
def message(
|
144
|
-
messages.
|
230
|
+
def message( messages )
|
231
|
+
@modifier.call(messages.is_a?(Array) && messages.size == 1 ? messages.first : messages)
|
145
232
|
end
|
146
233
|
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
m.map { |k,v| "#{k}: #{v}" }.join( ", " )
|
151
|
-
when Exception
|
152
|
-
backtrace = m.backtrace ? "\n\t#{m.backtrace.join("\n\t")}" : ""
|
153
|
-
sprintf("%s: %s%s", m.class, m.message, backtrace)
|
154
|
-
else
|
155
|
-
m
|
156
|
-
end
|
234
|
+
# do nothing
|
235
|
+
def noop
|
236
|
+
''
|
157
237
|
end
|
158
238
|
|
159
239
|
end
|
@@ -1,8 +1,6 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
module Yell #:nodoc:
|
3
2
|
module Helpers #:nodoc:
|
4
|
-
module
|
5
|
-
|
3
|
+
module Adapter #:nodoc:
|
6
4
|
# Define an adapter to be used for logging.
|
7
5
|
#
|
8
6
|
# @example Standard adapter
|
@@ -12,40 +10,32 @@ module Yell #:nodoc:
|
|
12
10
|
# adapter :file, 'development.log'
|
13
11
|
#
|
14
12
|
# # Alternative notation for filename in options
|
15
|
-
# adapter :file, :
|
13
|
+
# adapter :file, filename: 'developent.log'
|
16
14
|
#
|
17
15
|
# @example Standard adapter with filename and additional options
|
18
|
-
# adapter :file, 'development.log', :
|
16
|
+
# adapter :file, 'development.log', level: :warn
|
19
17
|
#
|
20
18
|
# @example Set the adapter directly from an adapter instance
|
21
|
-
# adapter
|
19
|
+
# adapter Yell::Adapter::File.new
|
22
20
|
#
|
23
21
|
# @param [Symbol] type The type of the adapter, may be `:file` or `:datefile` (default `:file`)
|
24
22
|
# @return [Yell::Adapter] The instance
|
25
23
|
# @raise [Yell::NoSuchAdapter] Will be thrown when the adapter is not defined
|
26
24
|
def adapter( type = :file, *args, &block )
|
27
|
-
|
28
|
-
h.merge( [String, Pathname].include?(c.class) ? {:filename => c} : c )
|
29
|
-
end
|
30
|
-
|
31
|
-
@adapters << Yell::Adapters.new(type, options, &block)
|
25
|
+
adapters.add(type, *args, &block)
|
32
26
|
end
|
33
27
|
|
34
|
-
# @private
|
35
28
|
def adapters
|
36
|
-
@
|
29
|
+
@__adapters__
|
37
30
|
end
|
38
31
|
|
39
|
-
|
40
32
|
private
|
41
33
|
|
42
34
|
def reset!
|
43
|
-
@
|
35
|
+
@__adapters__ = Yell::Adapters::Collection.new(@options)
|
44
36
|
|
45
37
|
super
|
46
38
|
end
|
47
|
-
|
48
39
|
end
|
49
40
|
end
|
50
41
|
end
|
51
|
-
|
data/lib/yell/helpers/base.rb
CHANGED
@@ -1,26 +1,26 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
module Yell #:nodoc:
|
3
2
|
module Helpers #:nodoc:
|
4
3
|
module Formatter #:nodoc:
|
5
4
|
|
6
5
|
# Set the format for your message.
|
7
|
-
def
|
8
|
-
@
|
6
|
+
def formatter=( pattern )
|
7
|
+
@__formatter__ = case pattern
|
9
8
|
when Yell::Formatter then pattern
|
10
|
-
when false then Yell::Formatter.new(Yell::NoFormat)
|
11
9
|
else Yell::Formatter.new(*pattern)
|
12
10
|
end
|
13
11
|
end
|
12
|
+
alias :format= :formatter=
|
14
13
|
|
15
|
-
|
16
|
-
|
17
|
-
@formatter
|
14
|
+
def formatter
|
15
|
+
@__formatter__
|
18
16
|
end
|
17
|
+
alias :format :formatter
|
18
|
+
|
19
19
|
|
20
20
|
private
|
21
21
|
|
22
22
|
def reset!
|
23
|
-
@
|
23
|
+
@__formatter__ = Yell::Formatter.new
|
24
24
|
|
25
25
|
super
|
26
26
|
end
|