yell 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -7,6 +7,6 @@ gemspec
7
7
  group :development do
8
8
  gem "rake"
9
9
 
10
- gem "rspec"
10
+ gem "rspec", "~> 2"
11
11
  gem "timecop"
12
- end
12
+ end
data/README.md CHANGED
@@ -23,29 +23,11 @@ message as follows:
23
23
  logger = Yell.new STDOUT
24
24
 
25
25
  logger.info "Hello World"
26
- "2012-02-29T09:30:00+01:00 [ INFO] 65784 : Hello World"
27
- #^ ^ ^ ^
28
- #ISO8601 Timestamp Level Pid message
26
+ #=> "2012-02-29T09:30:00+01:00 [ INFO] 65784 : Hello World"
27
+ # ^ ^ ^ ^
28
+ # ISO8601 Timestamp Level Pid Message
29
29
  ```
30
30
 
31
- You can alternate the formatting, or completely disable it altogether. See the
32
- next examples for reference:
33
-
34
- ```ruby
35
- # No formatting
36
- logger = Yell.new STDOUT, :format => false
37
- logger.info "No formatting"
38
- "No formatting"
39
-
40
- # Alternate formatting
41
- logger = Yell.new STDOUT, :format => "'%m' at %d"
42
- logger.info "Alternate formatting"
43
- "'Alternate format' at 2012-02-29T09:30:00+01:00"
44
- ```
45
-
46
- As you can see, it basically is just string interpolation with a few reserved
47
- captures. You can see the list at #Yell::Formatter.
48
-
49
31
  When no arguments are given, Yell will check for `ENV['RACK_ENV']` and
50
32
  determine the filename from that.
51
33
 
@@ -57,8 +39,11 @@ a filename explicitly.
57
39
  Naturally, if you pass a `:filename` to Yell:
58
40
 
59
41
  ```ruby
60
- logger = Yell.new 'custom.log'
42
+ logger = Yell.new 'yell.log'
61
43
  ```
62
44
 
45
+ Please refer to the wiki for further usage examples:
46
+ https://github.com/rudionrails/yell/wiki/
47
+
63
48
  Copyright © 2011-2012 Rudolf Schmidt, released under the MIT license
64
49
 
data/lib/yell.rb CHANGED
@@ -23,35 +23,35 @@
23
23
 
24
24
  require 'time'
25
25
 
26
- $: << File.dirname(__FILE__)
26
+ begin
27
+ require 'yell/event'
28
+ rescue LoadError
29
+ $: << File.dirname(__FILE__)
30
+ require 'yell/event'
31
+ end
27
32
 
33
+ require 'yell/level'
28
34
  require 'yell/formatter'
29
35
  require 'yell/adapters'
30
- require 'yell/level'
31
36
  require 'yell/logger'
32
37
 
33
38
  module Yell #:nodoc:
34
39
  # The possible log levels
35
40
  Severities = [ 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL', 'UNKNOWN' ]
36
41
 
37
- extend self
38
-
39
- # Creates a new logger instance.
40
- #
41
- # Refer to #Yell::Loggger for usage.
42
- #
43
- # @returns [Yell::Logger] The logger instance
44
- def new( *args, &block )
45
- Yell::Logger.new( *args, &block )
46
- end
47
-
48
-
49
- def env #:nodoc:
50
- ENV['YELL_ENV'] || ENV['RACK_ENV'] || 'development'
51
- end
52
-
53
- def level( val = nil ) #:nodoc:
54
- Yell::Level.new( val )
42
+ class << self
43
+ # Creates a new logger instance.
44
+ #
45
+ # Refer to #Yell::Loggger for usage.
46
+ #
47
+ # @return [Yell::Logger] The logger instance
48
+ def new( *args, &block )
49
+ Yell::Logger.new( *args, &block )
50
+ end
51
+
52
+ def env #:nodoc:
53
+ ENV['YELL_ENV'] || ENV['RACK_ENV'] || 'development'
54
+ end
55
55
  end
56
56
 
57
57
  end
@@ -14,7 +14,7 @@ module Yell #:nodoc:
14
14
  #
15
15
  # Adapter classes should provide their own implementation
16
16
  # of this method.
17
- def write( level, message )
17
+ def write( event )
18
18
  nil
19
19
  end
20
20
 
@@ -21,13 +21,14 @@ module Yell #:nodoc:
21
21
  super( options, &block )
22
22
  end
23
23
 
24
- def write( level, message )
24
+ # @overload Close the file if date is expired
25
+ def write( event )
25
26
  close if close?
26
27
 
27
- super( level, message )
28
+ super( event )
28
29
  end
29
30
 
30
- # @override Reset the file handle
31
+ # @overload Reset the file handle
31
32
  def close
32
33
  @filename = new_filename
33
34
 
@@ -13,7 +13,7 @@ module Yell #:nodoc:
13
13
  @filename = options.fetch(:filename, default_filename)
14
14
  end
15
15
 
16
- # @override Lazily open the file handle
16
+ # @overload Lazily open the file handle
17
17
  def stream
18
18
  @stream ||= ::File.open( @filename, ::File::WRONLY|::File::APPEND|::File::CREAT )
19
19
  end
@@ -21,7 +21,7 @@ module Yell #:nodoc:
21
21
 
22
22
  private
23
23
 
24
- def default_filename
24
+ def default_filename #:nodoc:
25
25
  ::File.directory?("log") ? "log/#{Yell.env}.log" : "#{Yell.env}.log"
26
26
  end
27
27
 
@@ -36,17 +36,17 @@ module Yell
36
36
  #
37
37
  # @example
38
38
  # write( 'info', 'Hello World' )
39
- def write( level, message )
40
- msg = @formatter.format( level, message )
39
+ def write( event )
40
+ message = @formatter.format( event )
41
41
 
42
42
  # colorize if applicable
43
43
  if colorize? and color = Colors[level]
44
- msg = color + msg + Colors['DEFAULT']
44
+ message = color + message + Colors['DEFAULT']
45
45
  end
46
46
 
47
- msg << "\n" unless msg[-1] == ?\n # add new line if there is none
47
+ message << "\n" unless message[-1] == ?\n # add new line if there is none
48
48
 
49
- write!( msg )
49
+ write!( message )
50
50
  end
51
51
 
52
52
  # Set the format for your message.
data/lib/yell/event.rb ADDED
@@ -0,0 +1,40 @@
1
+ module Yell #:nodoc:
2
+
3
+ class Event
4
+ CallerRegexp = /^(.+?):(\d+)(?::in `(.*)')?/ #:nodoc:
5
+
6
+ # Accessor to the log level
7
+ attr_reader :level
8
+
9
+ # Accessor to the log message
10
+ attr_reader :message
11
+
12
+ # Accessor to the time the log event occured
13
+ attr_reader :time
14
+
15
+ # Accessor to filename the log event occured
16
+ attr_reader :file
17
+
18
+ # Accessor to the line the log event occured
19
+ attr_reader :line
20
+
21
+ # Accessor to the method the log event occured
22
+ attr_reader :method
23
+
24
+
25
+ # Initialize a new log event
26
+ def initialize( level, message = nil, &block )
27
+ @time = Time.now
28
+ @level = level
29
+ @message = block ? block.call : message
30
+
31
+ if m = CallerRegexp.match( caller(4).first )
32
+ @file, @line, @method = m[1..-1]
33
+ else
34
+ @file, @line, @method = ['', '', '']
35
+ end
36
+ end
37
+
38
+ end
39
+ end
40
+
@@ -2,23 +2,74 @@
2
2
 
3
3
  module Yell #:nodoc:
4
4
 
5
+ # No format on the log message
6
+ #
7
+ # @example
8
+ # logger = Yell.new STDOUT, :format => false
9
+ # logger.info "Hello World!"
10
+ # #=> "Hello World!"
11
+ NoFormat = "%m"
12
+
13
+ # Default Format
14
+ #
15
+ # @example
16
+ # logger = Yell.new STDOUT, :format => Yell::DefaultFormat
17
+ # logger.info "Hello World!"
18
+ # #=> "2012-02-29T09:30:00+01:00 [ INFO] 65784 : Hello World!"
19
+ # # ^ ^ ^ ^
20
+ # # ISO8601 Timestamp Level Pid Message
21
+ DefaultFormat = "%d [%5L] %p : %m"
22
+
23
+ # Basic Format
24
+ #
25
+ # @example
26
+ # logger = Yell.new STDOUT, :format => Yell::BasicFormat
27
+ # logger.info "Hello World!"
28
+ # #=> "I, 2012-02-29T09:30:00+01:00 : Hello World!"
29
+ # # ^ ^ ^
30
+ # # ^ ISO8601 Timestamp Message
31
+ # # Level (short)
32
+ BasicFormat = "%l, %d : %m"
33
+
34
+ # Extended Format
35
+ #
36
+ # @example
37
+ # logger = Yell.new STDOUT, :format => Yell::ExtendedFormat
38
+ # logger.info "Hello World!"
39
+ # #=> "2012-02-29T09:30:00+01:00 [ INFO] 65784 localhost : Hello World!"
40
+ # # ^ ^ ^ ^ ^
41
+ # # ISO8601 Timestamp Level Pid Hostname Message
42
+ ExtendedFormat = "%d [%5L] %p %h : %m"
43
+
44
+
45
+ def format( pattern, date_pattern ) #:nodoc:
46
+ Yell::Formatter.new( pattern, date_pattern )
47
+ end
48
+
49
+
5
50
  # The +Formatter+ provides a handle to configure your log message style.
6
51
  class Formatter
7
52
 
53
+ #:nodoc:
8
54
  PatternTable = {
9
- "m" => "message",
10
- "d" => "date",
11
- "l" => "level[0]",
12
- "L" => "level.upcase",
13
- "p" => "$$",
14
- "h" => "hostname"
55
+ "m" => "event.message", # Message
56
+ "l" => "event.level[0]", # Level (short), e.g.'I', 'W'
57
+ "L" => "event.level", # Level, e.g. 'INFO', 'WARN'
58
+ "d" => "date(event)", # ISO8601 Timestamp
59
+ "p" => "Process.pid", # PID
60
+ "h" => "Socket.gethostname rescue nil", # Hostname
61
+ "F" => "event.file", # Path with filename where the logger was called
62
+ "f" => "File.basename(event.file)", # Filename where the loger was called
63
+ "M" => "event.method", # Method name where the logger was called
64
+ "n" => "event.line" # Line where the logger was called
15
65
  }
16
- PatternRegexp = /([^%]*)(%\d*)?([dlLphm])?(.*)/
17
- DefaultPattern = "%d [%5L] %p %h: %m"
66
+
67
+ #:nodoc:
68
+ PatternRegexp = /([^%]*)(%\d*)?([#{PatternTable.keys.join}])?(.*)/
18
69
 
19
70
 
20
71
  def initialize( pattern = nil, date_pattern = nil )
21
- @pattern = pattern || DefaultPattern
72
+ @pattern = pattern || Yell::DefaultFormat
22
73
  @date_pattern = date_pattern
23
74
 
24
75
  define!
@@ -27,6 +78,7 @@ module Yell #:nodoc:
27
78
 
28
79
  private
29
80
 
81
+ # defines the format method
30
82
  def define!
31
83
  buff, args, _pattern = "", [], @pattern.dup
32
84
 
@@ -43,19 +95,14 @@ module Yell #:nodoc:
43
95
  end
44
96
 
45
97
  instance_eval %-
46
- def format( level, message )
98
+ def format( event )
47
99
  sprintf( "#{buff}", #{args.join(',')} )
48
100
  end
49
101
  -
50
102
  end
51
103
 
52
- def date
53
- @date_pattern ? Time.now.strftime( @date_pattern ) : Time.now.iso8601
54
- end
55
-
56
- def hostname
57
- return @hostname if defined?( @hostname )
58
- @hostname = Socket.gethostname rescue nil
104
+ def date( event )
105
+ @date_pattern ? event.time.strftime( @date_pattern ) : event.time.iso8601
59
106
  end
60
107
 
61
108
  end
data/lib/yell/level.rb CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  module Yell #:nodoc:
4
4
 
5
+ def level( val = nil ) #:nodoc:
6
+ Yell::Level.new( val )
7
+ end
8
+
5
9
  # The +Level+ class handles the severities for you in order to determine
6
10
  # if an adapter should log or not.
7
11
  #
@@ -12,7 +16,6 @@ module Yell #:nodoc:
12
16
  # lt :warn # Will set from :info level an below
13
17
  # lte :warn # Will set from :warn level and below
14
18
  #
15
- #
16
19
  # You are able to combine those modifiers to your convenience.
17
20
  #
18
21
  # @example Set from :info to :error (including)
@@ -89,7 +92,7 @@ module Yell #:nodoc:
89
92
 
90
93
  private
91
94
 
92
- def calculate!( modifier, severity )
95
+ def calculate!( modifier, severity ) #:nodoc:
93
96
  index = index_from( severity )
94
97
  return if index.nil?
95
98
 
@@ -104,7 +107,7 @@ module Yell #:nodoc:
104
107
  taint unless tainted?
105
108
  end
106
109
 
107
- def index_from( severity )
110
+ def index_from( severity ) #:nodoc:
108
111
  case severity
109
112
  when Integer then severity
110
113
  when String, Symbol then Yell::Severities.index( severity.to_s.upcase )
@@ -112,7 +115,7 @@ module Yell #:nodoc:
112
115
  end
113
116
  end
114
117
 
115
- def ascending!( index )
118
+ def ascending!( index ) #:nodoc:
116
119
  @severities.each_with_index do |s, i|
117
120
  next if s == false # skip
118
121
 
@@ -120,7 +123,7 @@ module Yell #:nodoc:
120
123
  end
121
124
  end
122
125
 
123
- def descending!( index )
126
+ def descending!( index ) #:nodoc:
124
127
  @severities.each_with_index do |s, i|
125
128
  next if s == false # skip
126
129
 
@@ -128,7 +131,7 @@ module Yell #:nodoc:
128
131
  end
129
132
  end
130
133
 
131
- def set!( index )
134
+ def set!( index ) #:nodoc:
132
135
  @severities.map! { false } unless tainted?
133
136
 
134
137
  @severities[index] = true
data/lib/yell/logger.rb CHANGED
@@ -117,27 +117,26 @@ module Yell #:nodoc:
117
117
  # Creates instance methods for every defined log level (debug, info, ...) depending
118
118
  # on whether anything should be logged upon, for instance, #info.
119
119
  def define_log_methods!
120
- Yell::Severities.each do |l|
121
- name = l.downcase
120
+ Yell::Severities.each do |s|
121
+ name = s.downcase
122
122
 
123
123
  instance_eval %-
124
- def #{name}?; #{@level.at?(name)}; end # def info?; true; end
125
- #
126
- def #{name}( message = nil ) # def info( message = nil )
127
- return unless #{name}? # return unless info?
128
- #
129
- message = yield if block_given? # message = yield if block_given?
130
- write( "#{l}", message ) # write( "INFO", message )
131
- #
132
- true # true
133
- end # end
124
+ def #{name}?; #{@level.at?(name)}; end # def info?; true; end
125
+ #
126
+ def #{name}( m = nil, &b ) # def info( m = nil, &b )
127
+ return unless #{name}? # return unless info?
128
+ #
129
+ write Yell::Event.new( '#{s}', m, &b ) # write Yell::Event.new( "INFO", m, &b )
130
+ #
131
+ true # true
132
+ end # end
134
133
  -
135
134
  end
136
135
  end
137
136
 
138
137
  # Cycles all the adapters and writes the message
139
- def write( level, message )
140
- @adapters.each { |a| a.write(level, message) if a.write?(level) }
138
+ def write( event )
139
+ @adapters.each { |a| a.write(event) if a.write?(event.level) }
141
140
  end
142
141
 
143
142
  end
data/lib/yell/version.rb CHANGED
@@ -1,4 +1,4 @@
1
- module Yell
2
- VERSION = "0.2.0"
1
+ module Yell #:nodoc:
2
+ VERSION = "0.3.0" #:nodoc:
3
3
 
4
4
  end
data/spec/spec_helper.rb CHANGED
@@ -7,5 +7,14 @@ require 'rspec'
7
7
  require 'timecop'
8
8
 
9
9
  RSpec.configure do |config|
10
+
11
+ config.before do
12
+ Yell.config.stub!( :yaml_file ).and_return( File.dirname(__FILE__) + '/config/yell.yml' )
13
+ end
14
+
15
+ config.after do
16
+ Yell.config.reload! # to not run into caching problems during tests
17
+ end
18
+
10
19
  end
11
20
 
data/spec/yell_spec.rb CHANGED
@@ -15,4 +15,3 @@ describe Yell do
15
15
  end
16
16
 
17
17
  end
18
-
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yell
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-17 00:00:00.000000000 Z
12
+ date: 2012-03-22 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: An easy to use logging library to log into files and any other self-defined
15
15
  adapters
@@ -23,7 +23,6 @@ files:
23
23
  - LICENSE.txt
24
24
  - README.md
25
25
  - Rakefile
26
- - init.rb
27
26
  - lib/yell.rb
28
27
  - lib/yell/adapters.rb
29
28
  - lib/yell/adapters/.file.rb.swp
@@ -31,6 +30,7 @@ files:
31
30
  - lib/yell/adapters/datefile.rb
32
31
  - lib/yell/adapters/file.rb
33
32
  - lib/yell/adapters/io.rb
33
+ - lib/yell/event.rb
34
34
  - lib/yell/formatter.rb
35
35
  - lib/yell/level.rb
36
36
  - lib/yell/logger.rb
@@ -67,3 +67,4 @@ test_files:
67
67
  - spec/spec.opts
68
68
  - spec/spec_helper.rb
69
69
  - spec/yell_spec.rb
70
+ has_rdoc:
data/init.rb DELETED
@@ -1 +0,0 @@
1
- require 'yell'