Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

24 log to stderr #25

Merged
merged 3 commits into from
Oct 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ writing to a file or syslog since logstash can receive the structured data direc
## Features

* Can write directly to logstash over a UDP or TCP/SSL connection.
* Can write to a file, Redis, a unix socket, or stdout.
* Can write to a file, Redis, a unix socket, stdout or stderr.
* Always writes in logstash JSON format.
* Logger can take a string message, a hash, a `LogStash::Event`, an object, or a JSON string as input.
* Events are automatically populated with message, timestamp, host, and severity.
Expand Down Expand Up @@ -45,6 +45,7 @@ file_logger = LogStashLogger.new(type: :file, path: 'log/development.log', sync:
unix_logger = LogStashLogger.new(type: :unix, path: '/tmp/sock')
redis_logger = LogStashLogger.new(type: :redis)
stdout_logger = LogStashLogger.new(type: :stdout)
stderr_logger = LogStashLogger.new(type: :stderr)
io_logger = LogStashLogger.new(type: :io, io: io)

# The following messages are written to UDP port 5228:
Expand Down Expand Up @@ -78,6 +79,7 @@ unix:///tmp/socket
file:///path/to/file
redis://localhost:6379
stdout:/
stderr:/
```

Pass the URI into your logstash logger like so:
Expand Down
2 changes: 2 additions & 0 deletions lib/logstash-logger/device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module Device
autoload :File, 'logstash-logger/device/file'
autoload :IO, 'logstash-logger/device/io'
autoload :Stdout, 'logstash-logger/device/stdout'
autoload :Stderr, 'logstash-logger/device/stderr'

def self.new(opts)
opts = opts.dup
Expand Down Expand Up @@ -43,6 +44,7 @@ def self.device_klass_for(type)
when :redis then Redis
when :io then IO
when :stdout then Stdout
when :stderr then Stderr
else fail ArgumentError, 'Invalid type'
end
end
Expand Down
13 changes: 13 additions & 0 deletions lib/logstash-logger/device/stderr.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module LogStashLogger
module Device
class Stderr < IO
def initialize(opts={})
super({io: $stderr}.merge(opts))
end

def close
# no-op
end
end
end
end
16 changes: 16 additions & 0 deletions spec/device/stderr_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'logstash-logger'

describe LogStashLogger::Device::Stderr do
let(:stderr) { $stderr }

it 'writes to stderr' do
expect(subject.to_io).to eq stderr
expect(stderr).to receive(:write).once
subject.write("test")
end

it 'ignores #close' do
expect(stderr).not_to receive(:close)
subject.close
end
end
5 changes: 5 additions & 0 deletions spec/device_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@
let(:uri_config) { stdout_uri_config }
it { is_expected.to be_a LogStashLogger::Device::Stdout }
end

context 'when URI config is stderr' do
let(:uri_config) { stderr_uri_config }
it { is_expected.to be_a LogStashLogger::Device::Stderr }
end
end

end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def connection_type
let(:file_uri) { "file://#{file.path}" }
let(:redis_uri) { "redis://localhost:6379" }
let(:stdout_uri) { "stdout://localhost" }
let(:stderr_uri) { "stderr://localhost" }

let(:invalid_uri_config) { {uri: "non parsable uri"} }
let(:udp_uri_config) { {uri: udp_uri} }
Expand All @@ -61,4 +62,5 @@ def connection_type
let(:file_uri_config) { {uri: file_uri} }
let(:redis_uri_config) { {uri: redis_uri} }
let(:stdout_uri_config) { {uri: stdout_uri} }
let(:stderr_uri_config) { {uri: stderr_uri} }
end