log4ruby 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -48,15 +48,17 @@ module Log4Ruby
48
48
  return if log[:level] < @level
49
49
 
50
50
  # Format the log item and emit it.
51
- emit(@formatter.format(log))
51
+ string = @formatter.format(log)
52
+ emit(string, log)
52
53
  end
53
54
 
54
55
  private
55
56
 
56
57
  # Emits a log message to this appenders target.
57
58
  #
58
- # @param [String] message the message to emit.
59
- def emit(message)
59
+ # @param [String] string the message to emit.
60
+ # @param [Log4Ruby::Log] log the log item.
61
+ def emit(string, log)
60
62
  end
61
63
 
62
64
  end
@@ -17,12 +17,68 @@ module Log4Ruby
17
17
  # @param [Hash] options configuration options. Supported keys are:
18
18
  # :formatter - the formatter this appender should use. Uses the DefaultFormatter if not specified.
19
19
  # :target - the target stream to use (:stdout or :stderr). Default is :stdout
20
+ # :colorize - true to color the content printed to the console. Default is false.
20
21
  def initialize(level, options = {})
21
22
  target = get_option(options, :target, false, :stdout)
22
23
  raise ArgumentError.new("Invalid target '#{target}'. Must be either 'stdout' or 'stderr'.") unless TARGETS.has_key?(target)
23
24
 
24
25
  set_option(options, :stream, TARGETS[target], true)
25
26
  super(level, options)
27
+
28
+ @colorize = get_option(options, :colorize, false, false)
29
+ register_default_colors if @colorize
30
+ end
31
+
32
+ # Registers a color for a particular level. Does nothing if the colorize flag was false.
33
+ #
34
+ # @param [Log4Ruby::Level] level the level to register the color for.
35
+ # @param [Array] color_details the details of the color. An array with the attributes, foreground and background colours.
36
+ def register_color(level, color_details)
37
+ return if @colors.nil?
38
+ @colors[level] = color_details
39
+ end
40
+
41
+ private
42
+
43
+ # Registers the default set of colors.
44
+ def register_default_colors
45
+ @colors = {}
46
+ @colors[Log4Ruby::Level::TRACE] = [2, 30, nil]
47
+ @colors[Log4Ruby::Level::FINE] = [2, 30, nil]
48
+ @colors[Log4Ruby::Level::DEBUG] = [2, 30, nil]
49
+ @colors[Log4Ruby::Level::CONF] = [nil, 30, nil]
50
+ @colors[Log4Ruby::Level::INFO] = [nil, 30, nil]
51
+ @colors[Log4Ruby::Level::WARN] = [1, 30, nil]
52
+ @colors[Log4Ruby::Level::ERROR] = [1, 31, nil]
53
+ @colors[Log4Ruby::Level::FATAL] = [1, 31, nil]
54
+ end
55
+
56
+ # Colors the specified string.
57
+ #
58
+ # @param [String] string
59
+ # @param [Hash] options configuration hash.
60
+ # :attributes - modifiers for the text.
61
+ # :foreground - the color of the text.
62
+ # :background - the color the background.
63
+ def color(string, options = {})
64
+ attributes = get_option(options, :attributes, false, nil)
65
+ foreground = get_option(options, :foreground, false, nil)
66
+ background = get_option(options, :background, false, nil)
67
+ # Color!
68
+ color_string = [attributes, foreground, background].compact.join(";")
69
+ string = "\e[#{color_string}m#{string}\e[0m" unless color_string.empty?
70
+ # Return colored string.
71
+ string
72
+ end
73
+
74
+ # Override to color output if needed.
75
+ def emit(string, log)
76
+ if @colorize
77
+ attributes, foreground, background = @colors[log[:level]]
78
+ string = color(string, :attributes => attributes, :foreground => foreground, :background => background)
79
+ end
80
+
81
+ super(string, log)
26
82
  end
27
83
 
28
84
  end
@@ -107,14 +107,9 @@ module Log4Ruby
107
107
  # @stream is set to the latest file.
108
108
  latest_file
109
109
  # Create the emit method that will roll using the file date as the trigger.
110
- class << self
111
- self
112
-
113
- # Override to roll using the date of the file.
114
- def emit(message)
115
- roll_using_date
116
- super(message)
117
- end
110
+ def self.emit(string, log)
111
+ roll_using_date
112
+ super
118
113
  end
119
114
  end
120
115
 
@@ -126,14 +121,9 @@ module Log4Ruby
126
121
  # @stream is set to the latest file.
127
122
  latest_file
128
123
  # Create the emit method that will roll using the file size as the trigger.
129
- class << self
130
- self
131
-
132
- # Override to roll using the size of the file.
133
- def emit(message)
134
- roll_using_size
135
- super(message)
136
- end
124
+ def self.emit(string, log)
125
+ roll_using_size
126
+ super
137
127
  end
138
128
  end
139
129
 
@@ -54,10 +54,11 @@ module Log4Ruby
54
54
 
55
55
  # Writes the message to the underlying stream.
56
56
  #
57
- # @param [String] message the message to write.
58
- def emit(message)
57
+ # @param [String] string the message to write.
58
+ # @param [Log4Ruby::Log] log the log item.
59
+ def emit(string, log)
59
60
  emit_header if @emit_header
60
- @stream.write(message)
61
+ @stream.write(string)
61
62
  end
62
63
 
63
64
  end
@@ -60,18 +60,46 @@ module Log4Ruby
60
60
  @weight = weight
61
61
  end
62
62
 
63
+ # Check if this level is equal to the specified other log level.
64
+ # Comparison is done using the weight attribute
65
+ #
66
+ # @param [Log4Ruby::Level] other the other level.
67
+ def ==(other)
68
+ self.weight == other.weight
69
+ end
70
+
63
71
  # Check if this level is less than the specified other log level.
64
72
  # Comparison is done using the weight attribute
73
+ #
74
+ # @param [Log4Ruby::Level] other the other level.
65
75
  def <(other)
66
76
  self.weight < other.weight
67
77
  end
68
78
 
69
79
  # Check if this level is less than or equal to the specified other log level.
70
80
  # Comparison is done using the weight attribute
81
+ #
82
+ # @param [Log4Ruby::Level] other the other level.
71
83
  def <=(other)
72
84
  self.weight <= other.weight
73
85
  end
74
86
 
87
+ # Check if this level is greater than the specified other log level.
88
+ # Comparison is done using the weight attribute
89
+ #
90
+ # @param [Log4Ruby::Level] other the other level.
91
+ def >(other)
92
+ self.weight > other.weight
93
+ end
94
+
95
+ # Check if this level is greater than or equal to the specified other log level.
96
+ # Comparison is done using the weight attribute
97
+ #
98
+ # @param [Log4Ruby::Level] other the other level.
99
+ def >=(other)
100
+ self.weight <= other.weight
101
+ end
102
+
75
103
  # Override to return the name of the level.
76
104
  #
77
105
  # @return [String] the level's name.
@@ -4,6 +4,6 @@
4
4
 
5
5
  module Log4Ruby
6
6
 
7
- VERSION = "0.0.4"
7
+ VERSION = "0.0.5"
8
8
 
9
9
  end
@@ -14,7 +14,7 @@ formatter = Log4Ruby::PatternFormatter.new("[%s] %7s - %s : %s (%s)\n", [:timest
14
14
  formatter.add_parameter_formatter(:logger, Log4Ruby::ParameterFormatters.logger_formatter(1))
15
15
  formatter.add_parameter_formatter(:timestamp, Log4Ruby::ParameterFormatters.timestamp_formatter("%Y-%m-%d %H:%M:%S.%L"))
16
16
  # Console appender for printing to stdout.
17
- console_appender = Log4Ruby::ConsoleAppender.new(Log4Ruby::Level::ALL, :target => :stdout)
17
+ console_appender = Log4Ruby::ConsoleAppender.new(Log4Ruby::Level::ALL, :target => :stdout, :colorize => true)
18
18
  # Rolling file appender
19
19
  file_appender = Log4Ruby::FileAppender.new(Log4Ruby::Level::ALL, :roll_type => :dont_roll,
20
20
  :directory => "../logs", :prefix => "TestLogger")
metadata CHANGED
@@ -1,34 +1,48 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: log4ruby
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.4
3
+ version: !ruby/object:Gem::Version
4
+ hash: 21
5
5
  prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 5
10
+ version: 0.0.5
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - Suraj Vijayakumar
9
14
  autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
- date: 2012-07-26 00:00:00.000000000Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
17
+
18
+ date: 2012-07-27 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
15
21
  name: options-arg
16
- requirement: &7797456 !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
17
24
  none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 29
29
+ segments:
30
+ - 0
31
+ - 0
32
+ - 1
21
33
  version: 0.0.1
22
34
  type: :runtime
23
- prerelease: false
24
- version_requirements: *7797456
35
+ version_requirements: *id001
25
36
  description: A logging system for Ruby applications inspired by Java's logging API.
26
- email:
37
+ email:
27
38
  - vijayakumar.suraj@gmail.com
28
39
  executables: []
40
+
29
41
  extensions: []
42
+
30
43
  extra_rdoc_files: []
31
- files:
44
+
45
+ files:
32
46
  - lib/log4ruby/appender.rb
33
47
  - lib/log4ruby/appenders/console_appender.rb
34
48
  - lib/log4ruby/appenders/file_appender.rb
@@ -46,26 +60,38 @@ files:
46
60
  - lib/sample.rb
47
61
  homepage:
48
62
  licenses: []
63
+
49
64
  post_install_message:
50
65
  rdoc_options: []
51
- require_paths:
66
+
67
+ require_paths:
52
68
  - lib
53
- required_ruby_version: !ruby/object:Gem::Requirement
69
+ required_ruby_version: !ruby/object:Gem::Requirement
54
70
  none: false
55
- requirements:
56
- - - ! '>='
57
- - !ruby/object:Gem::Version
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 51
75
+ segments:
76
+ - 1
77
+ - 9
78
+ - 0
58
79
  version: 1.9.0
59
- required_rubygems_version: !ruby/object:Gem::Requirement
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
81
  none: false
61
- requirements:
62
- - - ! '>='
63
- - !ruby/object:Gem::Version
64
- version: '0'
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ hash: 3
86
+ segments:
87
+ - 0
88
+ version: "0"
65
89
  requirements: []
90
+
66
91
  rubyforge_project:
67
- rubygems_version: 1.7.2
92
+ rubygems_version: 1.8.24
68
93
  signing_key:
69
94
  specification_version: 3
70
95
  summary: A simple and customizable logging system inspired by Java logging API.
71
96
  test_files: []
97
+