Skip to content

Commit

Permalink
Make log levels configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
dmajda committed Apr 13, 2012
1 parent eef717f commit 4ec3750
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
9 changes: 8 additions & 1 deletion README.md
Expand Up @@ -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`:
Expand Down
30 changes: 20 additions & 10 deletions lib/cheetah.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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]
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions spec/cheetah_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit 4ec3750

Please sign in to comment.