Skip to content
Browse files

Make log levels configurable

  • Loading branch information...
1 parent eef717f commit 4ec3750580e9ec24bf9ef1e3ee9bce7a612463cf @dmajda dmajda committed Apr 13, 2012
Showing with 56 additions and 11 deletions.
  1. +8 −1 README.md
  2. +20 −10 lib/cheetah.rb
  3. +28 −0 spec/cheetah_spec.rb
View
9 README.md
@@ -78,11 +78,18 @@ rescue Cheetah::ExecutionFailed => e
end
```
-For debugging purposes, you can also use a logger. Cheetah will log the command, its status, input and both outputs to it. The `:info` level will be used for normal messages, the `:error` level for messages about errors (non-zero exit status or non-empty error output):
+For debugging purposes, you can also use a logger. Cheetah will log the command, its status, input and both outputs to it. By default, the `Logger::INFO` level will be used for normal messages and the `Logger::ERROR` level for messages about errors (non-zero exit status or non-empty error output), but this can be changed if needed:
```ruby
# Log the execution
Cheetah.run("ls -l", :logger => logger)
+
+# Change levels of logged messages
+Cheetah.run("ls -l",
+ :logger => logger,
+ :logger_level_info => Logger::DEBUG,
+ :logger_level_error => Logger::WARN
+)
```
To avoid repetition, you can set global default value of any option passed too `Cheetah.run`:
View
30 lib/cheetah.rb
@@ -96,9 +96,10 @@ class << self
#
# The command execution can be logged using a logger passed in the `:logger`
# option. If a logger is set, the method will log the command, its status,
- # input and both outputs to it. The `:info` level will be used for normal
- # messages, the `:error` level for messages about errors (non-zero exit
- # status or non-empty error output).
+ # input and both outputs to it. By default, the `Logger::INFO` level will be
+ # used for normal messages and the `Logger::ERROR` level for messages about
+ # errors (non-zero exit status or non-empty error output), but this can be
+ # changed using the `:logger_level_info` and `:logger_level_error` options.
#
# Values of options not set using the `options` parameter are taken from
# {Cheetah.default_options}. If a value is not specified there too, the
@@ -119,6 +120,10 @@ class << self
# two-element array of strings
# @option options [Logger, nil] :logger (nil) logger to log the command
# execution
+ # @option options [Integer] :logger_level_info (Logger::INFO) level for
+ # logging normal messages; makes sense only if `:logger` is specified
+ # @option options [Integer] :logger_level_error (Logger::ERROR) level for
+ # logging error messages; makes sense only if `:logger` is specified
#
# @overload run(command_and_args, options = {})
# This variant is useful mainly when building the command and its
@@ -147,8 +152,10 @@ def run(command, *args)
options = args.last.is_a?(Hash) ? args.pop : {}
options = @default_options.merge(options)
- stdin = options[:stdin] || ""
- logger = options[:logger]
+ stdin = options[:stdin] || ""
+ logger = options[:logger]
+ logger_level_info = options[:logger_level_info] || Logger::INFO
+ logger_level_error = options[:logger_level_error] || Logger::ERROR
if command.is_a?(Array)
args = command[1..-1]
@@ -160,8 +167,10 @@ def run(command, *args)
pipe_stderr_read, pipe_stderr_write = IO.pipe
if logger
- logger.info "Executing command #{command.inspect} with #{describe_args(args)}."
- logger.info "Standard input: " + (stdin.empty? ? "(none)" : stdin)
+ logger.add logger_level_info,
+ "Executing command #{command.inspect} with #{describe_args(args)}."
+ logger.add logger_level_info,
+ "Standard input: " + (stdin.empty? ? "(none)" : stdin)
end
pid = fork do
@@ -244,10 +253,11 @@ def run(command, *args)
end
ensure
if logger
- logger.add status.success? ? Logger::INFO : Logger::ERROR,
+ logger.add status.success? ? logger_level_info : logger_level_error,
"Status: #{status.exitstatus}"
- logger.info "Standard output: " + (stdout.empty? ? "(none)" : stdout)
- logger.add stderr.empty? ? Logger::INFO : Logger::ERROR,
+ logger.add logger_level_info,
+ "Standard output: " + (stdout.empty? ? "(none)" : stdout)
+ logger.add stderr.empty? ? logger_level_info : logger_level_error,
"Error output: " + (stderr.empty? ? "(none)" : stderr)
end
end
View
28 spec/cheetah_spec.rb
@@ -255,6 +255,34 @@ def create_command(source, options = {})
INFO Error output: (none)
EOT
end
+
+ it "logs info messages on the level specified by the :logger_level_info option" do
+ lambda { |logger|
+ Cheetah.run("/bin/true", :logger => logger, :logger_level_info => Logger::DEBUG)
+ }.should log(<<-EOT)
+ DEBUG Executing command "/bin/true" with no arguments.
+ DEBUG Standard input: (none)
+ DEBUG Status: 0
+ DEBUG Standard output: (none)
+ DEBUG Error output: (none)
+ EOT
+ end
+
+ it "logs error messages on the level specified by the :logger_level_error option" do
+ lambda { |logger|
+ begin
+ Cheetah.run("/bin/false", :logger => logger, :logger_level_error => Logger::WARN)
+ rescue Cheetah::ExecutionFailed
+ # Eat it.
+ end
+ }.should log(<<-EOT)
+ INFO Executing command "/bin/false" with no arguments.
+ INFO Standard input: (none)
+ WARN Status: 1
+ INFO Standard output: (none)
+ INFO Error output: (none)
+ EOT
+ end
end
describe "options handling" do

0 comments on commit 4ec3750

Please sign in to comment.
Something went wrong with that request. Please try again.