Skip to content

Commit

Permalink
Allow the logger device to be set.
Browse files Browse the repository at this point in the history
  • Loading branch information
netzpirat committed Jan 22, 2013
1 parent c8b8b6b commit 7ebf395
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

### Improvements

- Allow the logger device to be set with the `:device` option.
- Improve `list` and `show` command output. ([@netzpirat][])
- [#386][] Replace Pry's reset command. ([@envygeeks][])

Expand Down Expand Up @@ -803,4 +804,4 @@ The Listen integration has been supervised by [@thibaudgg][] and executed by [@M
[@waldo]: https://github.com/waldo
[@wereHamster]: https://github.com/wereHamster
[@yannlugrin]: https://github.com/yannlugrin
[@zonque]: https://github.com/zonque
[@zonque]: https://github.com/zonque
9 changes: 7 additions & 2 deletions README.md
Expand Up @@ -795,14 +795,16 @@ filter /\.js$/

### logger

The `logger` method allows you to customize the Guard log output to your needs by specifying one or more options like:
The `logger` method allows you to customize the [Lumberjack](https://github.com/bdurand/lumberjack) log output to your
needs by specifying one or more options like:

```ruby
logger :level => :warn,
:template => '[:severity - :time - :progname] :message',
:time_format => 'at %I:%M%p',
:only => [:rspec, :jasmine, 'coffeescript'],
:except => :jammit
:except => :jammit,
:device => 'guard.log'
```

Log `:level` option must be either `:debug`, `:info`, `:warn` or `:error`. If Guard is started in debug mode, the log
Expand All @@ -817,6 +819,9 @@ The `:time_format` option directives are the same as Time#strftime or can be `:m
The `:only` and `:except` are either a string or a symbol, or an array of strings or symbols that matches the name of
the Guard plugin name that sends the log message. They cannot be specified at the same time.

By default the logger uses `$stderr` as device, but you can override this by supplying the `:device` option and set
either an IO stream or a filename.

### Example

```ruby
Expand Down
4 changes: 2 additions & 2 deletions lib/guard/ui.rb
Expand Up @@ -16,15 +16,15 @@ class << self
# Get the Guard::UI logger instance
#
def logger
@logger ||= Lumberjack::Logger.new($stderr, self.options)
@logger ||= Lumberjack::Logger.new(self.options[:device], self.options)
end

# Get the logger options
#
# @return [Hash] the logger options
#
def options
@options ||= { :level => :info, :template => ':time - :severity - :message', :time_format => '%H:%M:%S' }
@options ||= { :device => $stderr, :level => :info, :template => ':time - :severity - :message', :time_format => '%H:%M:%S' }
end

# Set the logger options
Expand Down
6 changes: 5 additions & 1 deletion spec/guard/ui_spec.rb
@@ -1,7 +1,7 @@
require 'spec_helper'

describe Guard::UI do
after { Guard::UI.options = { :level => :info, :template => ':time - :severity - :message', :time_format => '%H:%M:%S' } }
after { Guard::UI.options = { :level => :info, :device => $stderr, :template => ':time - :severity - :message', :time_format => '%H:%M:%S' } }

before do
# The spec helper stubs all UI classes, so other specs doesn't have
Expand Down Expand Up @@ -32,6 +32,10 @@
it 'returns the logger instance' do
Guard::UI.logger.should be_an_instance_of Lumberjack::Logger
end

it 'sets the logger device' do
Guard::UI.logger.device.send(:stream).should be $stderr
end
end

describe '.options=' do
Expand Down

0 comments on commit 7ebf395

Please sign in to comment.