expose DEFAULT_LOGGER and remove nil/null support

This commit is contained in:
Colin Surprenant 2015-12-09 18:14:43 -05:00
parent 216055cbd6
commit 264c23811d
5 changed files with 22 additions and 8 deletions

View file

@ -149,7 +149,7 @@ describe LogStash::Event do
context "logger" do
let(:logger) { double("Logger") }
after(:each) { LogStash::Event.logger = nil }
after(:each) { LogStash::Event.logger = LogStash::Event::DEFAULT_LOGGER }
# the following 2 specs are using both a real module (DummyLogger)
# and a mock. both tests are needed to make sure the implementation
@ -163,7 +163,7 @@ describe LogStash::Event do
it "should set logger using a mock" do
LogStash::Event.logger = logger
expect(logger).to receive(:warn)
expect(logger).to receive(:warn).once
LogStash::Event.new(TIMESTAMP => "invalid timestamp")
end
@ -174,7 +174,7 @@ describe LogStash::Event do
LogStash::Event.new(TIMESTAMP => "invalid timestamp")
# then unset
LogStash::Event.logger = nil
LogStash::Event.logger = LogStash::Event::DEFAULT_LOGGER
expect(logger).to receive(:warn).never
# this will produce a log line in stdout by the Java Event
LogStash::Event.new(TIMESTAMP => "ignore this log")

View file

@ -30,6 +30,9 @@ public class Event implements Cloneable, Serializable {
private static final Logger DEFAULT_LOGGER = new StdioLogger();
private transient final ObjectMapper mapper = new ObjectMapper();
// logger is static since once set there is no point in changing it at runtime
// for other reasons than in tests/specs.
private transient static Logger logger = DEFAULT_LOGGER;
public Event()
@ -249,7 +252,9 @@ public class Event implements Cloneable, Serializable {
}
}
// Event.logger is static since once set there is no point in changing it at runtime
// for other reasons than in tests/specs.
public static void setLogger(Logger logger) {
Event.logger = (logger == null) ? DEFAULT_LOGGER : logger;
Event.logger = logger;
}
}

View file

@ -30,6 +30,7 @@ public class JrubyEventExtLibrary implements Library {
clazz.setConstant("TIMESTAMP", runtime.newString(Event.TIMESTAMP));
clazz.setConstant("TIMESTAMP_FAILURE_TAG", runtime.newString(Event.TIMESTAMP_FAILURE_TAG));
clazz.setConstant("TIMESTAMP_FAILURE_FIELD", runtime.newString(Event.TIMESTAMP_FAILURE_FIELD));
clazz.setConstant("DEFAULT_LOGGER", runtime.getModule("Cabin").getClass("Channel").callMethod("get", runtime.getModule("LogStash")));
clazz.defineAnnotatedMethods(RubyEvent.class);
clazz.defineAnnotatedConstants(RubyEvent.class);
}
@ -287,10 +288,12 @@ public class JrubyEventExtLibrary implements Library {
return new JrubyTimestampExtLibrary.RubyTimestamp(context.getRuntime(), this.event.getTimestamp());
}
// set a new logger for all Event instances
// there is no point in changing it at runtime for other reasons than in tests/specs.
@JRubyMethod(name = "logger=", required = 1, meta = true)
public static IRubyObject ruby_set_logger(ThreadContext context, IRubyObject recv, IRubyObject value)
{
Event.setLogger(value.isNil() ? null : new ProxyLogger((RubyObject)value));
Event.setLogger(new ProxyLogger((RubyObject)value));
return value;
}
}

View file

@ -63,6 +63,9 @@ class LogStash::Event
MIN_FLOAT_BEFORE_SCI_NOT = 0.0001
MAX_FLOAT_BEFORE_SCI_NOT = 1000000000000000.0
DEFAULT_LOGGER = Cabin::Channel.get(LogStash)
@@logger = DEFAULT_LOGGER
def initialize(data = {})
@cancelled = false
@data = data
@ -243,6 +246,9 @@ class LogStash::Event
raise DeprecatedMethod
end
# set a new logger for all Event instances
# there is no point in changing it at runtime for other reasons than in tests/specs.
# @param logger [Cabin::Channel] logger instance that will be used by all Event instances
def self.logger=(logger)
@@logger = logger
end
@ -250,7 +256,7 @@ class LogStash::Event
private
def logger
@@logger ||= Cabin::Channel.get(LogStash)
@@logger
end
def init_timestamp(o)

View file

@ -348,7 +348,7 @@ describe LogStash::Event do
LogStash::Event.new("@timestamp" => 666)
# reset logger to default
LogStash::Event.logger = nil
LogStash::Event.logger = LogStash::Event::DEFAULT_LOGGER
end
it "should tag for invalid string format" do
@ -365,7 +365,7 @@ describe LogStash::Event do
LogStash::Event.new("@timestamp" => "foo")
# reset logger to default
LogStash::Event.logger = nil
LogStash::Event.logger = LogStash::Event::DEFAULT_LOGGER
end
end