log4ruby 0.0.2 → 0.0.3

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.
@@ -17,6 +17,21 @@ module Log4Ruby
17
17
  def initialize(level, formatter)
18
18
  @level = level
19
19
  @formatter = formatter
20
+ @closed = false
21
+ end
22
+
23
+ # Closes this appender. Logs will not be emitted by this appender any longer.
24
+ #
25
+ # @return [TrueClass] Always true.
26
+ def close
27
+ @closed = true
28
+ end
29
+
30
+ # Check if this appender is closed.
31
+ #
32
+ # @return [TrueClass, FalseClass] true if the appender is closed.
33
+ def closed?
34
+ @closed
20
35
  end
21
36
 
22
37
  # Process the log item.
@@ -0,0 +1,24 @@
1
+ #
2
+ # 24 Jul 2012
3
+ #
4
+
5
+ require "log4ruby/appenders/stream_appender"
6
+
7
+ module Log4Ruby
8
+
9
+ # Sends console messages to either stdout or stderr.
10
+ class ConsoleAppender < Log4Ruby::StreamAppender
11
+
12
+ TARGETS = {:stdout => $stdout, :stderr => $stderr}
13
+
14
+ # New console appender.
15
+ #
16
+ # @param [Symbol] target either stdout or stderr.
17
+ def initialize(level, formatter, target = :stdout)
18
+ raise ArgumentError.new("Invalid target '#{target}'. Must be either 'stdout' or 'stderr'.") unless TARGETS.has_key?(target)
19
+ super(level, formatter, TARGETS[target])
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,164 @@
1
+ #
2
+ # 24 Jul 2012
3
+ #
4
+
5
+ require "date"
6
+
7
+ require "log4ruby/appenders/stream_appender"
8
+
9
+ module Log4Ruby
10
+
11
+ class DateRollers
12
+
13
+ HOURLY = Proc.new { |file_time| Time.now.hour != file_time.hour }
14
+
15
+ DAILY = Proc.new { |file_time| Time.now.day != file_time.day }
16
+
17
+ WEEKLY = Proc.new { |file_time| DateTime.now.cweek != DateTime.parse(file_time.to_s).cweek }
18
+
19
+ MONTHLY = Proc.new { |file_time| Time.now.month > file_time.month }
20
+
21
+ YEARLY = Proc.new { |file_time| Time.now.year > file_time.year }
22
+
23
+ end
24
+
25
+ # Customizable file appender to log to a file.
26
+ class FileAppender < Log4Ruby::StreamAppender
27
+
28
+ DATE_ROLLERS = {:hourly => DateRollers::HOURLY, :daily => DateRollers::DAILY,
29
+ :weekly => DateRollers::WEEKLY, :monthly => DateRollers::MONTHLY,
30
+ :yearly => DateRollers::YEARLY}
31
+
32
+ # New stream appender.
33
+ #
34
+ # @param [Hash] options configuration hash. Supports the following parameters.
35
+ # :directory - the directory into which the logs should be saved. Defaults to the current directory.
36
+ # :prefix - the file name prefix for the log files. *required*
37
+ # :suffix - the file name suffix for the log files. Defaults to 'log'
38
+ # :max_files - the maximum number of files to keep. Defaults to 5.
39
+ # :roll_type - the type of rolling required. Supports the following
40
+ # :dont_roll - the default type. Only one file is created.
41
+ # :roll_using_size - rolls files using the file's size.
42
+ # :roll_using_date - rolls files using the file's create date.
43
+ #
44
+ # Configuration parameters specific to rollers.
45
+ # dont_roll:
46
+ # :truncate - if true (the default), contents of the file are deleted before logging begins.
47
+ #
48
+ # roll_using_size:
49
+ # :max_size - the maximum allowed size for a log file (in bytes - default is 102400 bytes).
50
+ #
51
+ # roll_using_date:
52
+ # :frequency - the frequency with which to roll files (:hourly, :daily, :weekly, :monthly, :yearly).
53
+ # Default is :daily.
54
+ def initialize(level, formatter, options = {})
55
+ super(level, formatter, nil)
56
+
57
+ @directory = get_option(options, :directory, false, "./")
58
+ @prefix = get_option(options, :prefix, true)
59
+ @suffix = get_option(options, :suffix, false, "log")
60
+ @max_files = get_option(options, :max_files, false, 5)
61
+ @type = get_option(options, :roll_type, false, :dont_roll)
62
+
63
+ type_method = "initialize_#@type"
64
+ raise ArgumentError.new("Unrecognized type '#@type'!") unless respond_to?(type_method, true)
65
+ send(type_method, options)
66
+ end
67
+
68
+ private
69
+
70
+ # Creates a new file for logging into.
71
+ def create_file
72
+ @counter = @counter < @max_files ? @counter + 1 : 1
73
+
74
+ # The create timestamp is fussy.
75
+ # So first, delete any existing files.
76
+ # Then create a file with a temporary name and rename (i.e. move) it to the required file name.
77
+ # Finally open this file for use.
78
+ # This way the create date time is correct (i.e. the actual time the file was created).
79
+ @stream.close unless (@stream.nil? or @stream.closed?)
80
+ file_path = File.expand_path("#@prefix.#@counter.#@suffix", @directory)
81
+ file_path_temp = File.expand_path("#@prefix.#@counter.#@suffix.temp", @directory)
82
+ FileUtils.rm(file_path) if File.exist?(file_path)
83
+ File.new(file_path_temp, "w").close
84
+ FileUtils.mv(file_path_temp, file_path)
85
+ @stream = File.new(file_path, "w")
86
+ end
87
+
88
+ # Initialises this appender to not roll.
89
+ #
90
+ # @param [Hash] options configuration options.
91
+ def initialize_dont_roll(options)
92
+ mode = get_option(options, :truncate, false, true) ? "w" : "a"
93
+ FileUtils.mkdir(@directory) unless File.exist?(@directory)
94
+ @stream = File.open(File.expand_path("#@prefix.#@suffix", @directory), mode)
95
+ end
96
+
97
+ # Initialises this appender to roll using the date of the file.
98
+ #
99
+ # @param [Hash] options configuration options.
100
+ def initialize_roll_using_date(options)
101
+ frequency = get_option(options, :frequency, false, :daily)
102
+ raise ArgumentError.new("Unrecognized frequency '#{frequency}'!") unless DATE_ROLLERS.has_key?(frequency)
103
+ @roller = DATE_ROLLERS[frequency]
104
+ # @stream is set to the latest file.
105
+ latest_file
106
+ # Create the emit method that will roll using the file date as the trigger.
107
+ class << self
108
+ self
109
+ end.send(:define_method, :emit) do |message|
110
+ roll_using_date
111
+ super(message)
112
+ end
113
+ end
114
+
115
+ # Initialises this appender to roll using the size of the file.
116
+ #
117
+ # @param [Hash] options configuration options.
118
+ def initialize_roll_using_size(options)
119
+ @max_size = get_option(options, :max_size, false, 102400)
120
+ # @stream is set to the latest file.
121
+ latest_file
122
+ # Create the emit method that will roll using the file size as the trigger.
123
+ class << self
124
+ self
125
+ end.send(:define_method, :emit) do |message|
126
+ roll_using_size
127
+ super(message)
128
+ end
129
+ end
130
+
131
+ # Sets @stream to the most recent file (if it exists). Otherwise creates a new file.
132
+ # Only required if @stream is nil. Otherwise it returns immediately.
133
+ def latest_file
134
+ FileUtils.mkdir(@directory) unless File.exist?(@directory)
135
+ pattern = File.expand_path("#@prefix.*.#@suffix", @directory)
136
+ files = Dir.glob(pattern).sort { |f1, f2| File.mtime(f2) <=> File.mtime(f1) }
137
+ latest_file = files[0]
138
+
139
+ if latest_file.nil?
140
+ @counter = 0
141
+ create_file
142
+ else
143
+ @stream = File.open(latest_file, "a")
144
+ @counter = Integer(latest_file.match(/#@prefix\.(\d+)\.#@suffix/)[1])
145
+ end
146
+ end
147
+
148
+ # Rolls the current file if the create date time is old enough.
149
+ def roll_using_date
150
+ return unless @roller.call(@stream.ctime)
151
+
152
+ create_file
153
+ end
154
+
155
+ # Rolls the current file if the size is greater than the maximum size.
156
+ def roll_using_size
157
+ return if @stream.stat.size < @max_size
158
+
159
+ create_file
160
+ end
161
+
162
+ end
163
+
164
+ end
@@ -23,6 +23,7 @@ module Log4Ruby
23
23
  const_set(name, level)
24
24
  # Create the logging method.
25
25
  Logger.send(:define_method, method_name) do |message, parameters = {}|
26
+ return if self.closed?
26
27
  return if level < self.effective_level
27
28
  # Construct the log and dispatch it.
28
29
  parameters[:message] = message
@@ -34,6 +34,7 @@ module Log4Ruby
34
34
  @level = level
35
35
  @appenders = appenders
36
36
  @children = {}
37
+ @closed = false
37
38
 
38
39
  @use_parent_appenders = true
39
40
  @full_name = @parent.kind_of?(RootLogger) ? @name : @parent.full_name + "." + @name
@@ -47,6 +48,20 @@ module Log4Ruby
47
48
  @appenders << appender
48
49
  end
49
50
 
51
+ # Shuts down this logger. It will no longer dispatch logs to its appenders, not will it forward logs to its parent.
52
+ #
53
+ # @return [TrueClass] Always true.
54
+ def close
55
+ @closed = true
56
+ end
57
+
58
+ # Check if this logger is closed.
59
+ #
60
+ # @return [TrueClass, FalseClass] true if the logger is closed.
61
+ def closed?
62
+ @closed
63
+ end
64
+
50
65
  # Gets a child logger with the specified name. If the create flag is true, it will create a new logger
51
66
  # with this name.
52
67
  #
@@ -97,7 +112,8 @@ module Log4Ruby
97
112
  # @param [Log4Ruby::Log] log the log item to process. Does nothing if the log does not meet this logger's
98
113
  # threshold level.
99
114
  def process_log(log)
100
- return if log[:level] < @effective_level
115
+ return if self.closed?
116
+ return if log[:level] < self.effective_level
101
117
  # Forward to each appender. Once done, send to the parent.
102
118
  @appenders.each { |appender| appender.process_log(log) }
103
119
  @parent.process_log(log) if @use_parent_appenders
@@ -4,6 +4,6 @@
4
4
 
5
5
  module Log4Ruby
6
6
 
7
- VERSION = "0.0.2"
7
+ VERSION = "0.0.3"
8
8
 
9
9
  end
data/lib/tests.rb CHANGED
@@ -5,7 +5,8 @@
5
5
  $LOAD_PATH << "."
6
6
 
7
7
  require "log4ruby"
8
- require "log4ruby/appenders/stream_appender"
8
+ require "log4ruby/appenders/console_appender"
9
+ require "log4ruby/appenders/file_appender"
9
10
  require "log4ruby/formatters/pattern_formatter"
10
11
 
11
12
  # The pattern formatter, with a custom parameter formatter for the timestamp.
@@ -13,7 +14,14 @@ formatter = Log4Ruby::PatternFormatter.new("[%s] %7s - %s : %s\n", [:timestamp,
13
14
  formatter.add_parameter_formatter(:logger, Log4Ruby::ParameterFormatters.logger_formatter(1))
14
15
  formatter.add_parameter_formatter(:timestamp, Log4Ruby::ParameterFormatters.timestamp_formatter("%Y-%m-%d %H:%M:%S.%L"))
15
16
  # Stream appender for printing to stdout.
16
- console_appender = Log4Ruby::StreamAppender.new(Log4Ruby::Level::ALL, formatter, $stdout)
17
+ console_appender = Log4Ruby::ConsoleAppender.new(Log4Ruby::Level::ALL, formatter, :stdout)
18
+ # Rolling file appender
19
+ file_appender = Log4Ruby::FileAppender.new(Log4Ruby::Level::ALL, formatter, :roll_type => :dont_roll,
20
+ :directory => "../logs", :prefix => "TestLogger")
21
+ size_appender = Log4Ruby::FileAppender.new(Log4Ruby::Level::ALL, formatter, :roll_type => :roll_using_size,
22
+ :directory => "../logs", :prefix => "TestLoggerSize", :max_size => 1024)
23
+ date_appender = Log4Ruby::FileAppender.new(Log4Ruby::Level::ALL, formatter, :roll_type => :roll_using_date,
24
+ :directory => "../logs", :prefix => "TestLoggerDate", :frequency => :hourly)
17
25
 
18
26
  # Register a custom level FINER, that is between TRACE and FINE
19
27
  Log4Ruby::Level.register("FINER", 150.0)
@@ -22,6 +30,9 @@ Log4Ruby::Level.register("FINER", 150.0)
22
30
  logger = Log4Ruby.get_logger("Log4Ruby::TestLogger")
23
31
  logger.set_level(Log4Ruby::Level::FINER)
24
32
  logger.add_appender(console_appender)
33
+ logger.add_appender(file_appender)
34
+ logger.add_appender(size_appender)
35
+ logger.add_appender(date_appender)
25
36
 
26
37
  puts "Logger will print TRACE? : #{logger.trace?}"
27
38
  puts "Logger will print FINER? : #{logger.finer?}"
@@ -35,4 +46,4 @@ logger.conf("Test!")
35
46
  logger.info("Test!")
36
47
  logger.warn("Test!")
37
48
  logger.error("Test!")
38
- logger.fatal("Test!")
49
+ logger.fatal("Test!")
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: log4ruby
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Suraj Vijayakumar
@@ -44,6 +44,8 @@ extra_rdoc_files: []
44
44
 
45
45
  files:
46
46
  - lib/log4ruby/appender.rb
47
+ - lib/log4ruby/appenders/console_appender.rb
48
+ - lib/log4ruby/appenders/file_appender.rb
47
49
  - lib/log4ruby/appenders/nil_appender.rb
48
50
  - lib/log4ruby/appenders/stream_appender.rb
49
51
  - lib/log4ruby/exceptions.rb
@@ -52,7 +54,6 @@ files:
52
54
  - lib/log4ruby/level.rb
53
55
  - lib/log4ruby/log.rb
54
56
  - lib/log4ruby/logger.rb
55
- - lib/log4ruby/rolling_file.rb
56
57
  - lib/log4ruby/version.rb
57
58
  - lib/log4ruby.rb
58
59
  - lib/tests.rb
@@ -69,10 +70,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
69
70
  requirements:
70
71
  - - ">="
71
72
  - !ruby/object:Gem::Version
72
- hash: 3
73
+ hash: 51
73
74
  segments:
75
+ - 1
76
+ - 9
74
77
  - 0
75
- version: "0"
78
+ version: 1.9.0
76
79
  required_rubygems_version: !ruby/object:Gem::Requirement
77
80
  none: false
78
81
  requirements:
@@ -1,26 +0,0 @@
1
- #
2
- # 23 Jul 2012
3
- #
4
-
5
- module Log4Ruby
6
-
7
- class RollingFile
8
-
9
- # New rolling file with the specified prefix.
10
- #
11
- # @param [String] prefix the prefix for all the files that will be created.
12
- def initialize(prefix)
13
- @prefix = prefix
14
- end
15
-
16
- end
17
-
18
- class TimedRollingFile < RollingFile
19
-
20
- # New timed rolling file that will roll over to a new file when the specified time expires.
21
- def initialize(prefix, time_to_roll)
22
- end
23
-
24
- end
25
-
26
- end