Custom Appender documentation possible errors #227
-
super(level: level, &block) I am writing an appender that I can query in unit tests. So far I have this, in case it's useful to you, or you have any suggestions: class LogEventAppender < SemanticLogger::Subscriber
attr_reader :events
def initialize(level = nil, &block)
super(level: level, &block)
@events = []
end
# Display the log struct and the text formatted output
def log(log)
@events << log if level_index > (log.level_index || 0)
end
def clear
@events.clear
end
end Then, during rspec initialization, in a def event_appender
$semantic_logger_event_appender ||= LogEventAppender.new
end
def events
SemanticLogger.flush
event_appender.events
end
def setup_logging(rspec_config)
rspec_config.before(:each) do
SemanticLogger.flush
event_appender.clear
end
...
end Then, in a given test, I can call |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
The appender you are looking for already exists, see the section |
Beta Was this translation helpful? Give feedback.
-
Thank you, Documentation updated. |
Beta Was this translation helpful? Give feedback.
-
The documentation for the new Minitest extension for testing log messages: https://logger.rocketjob.io/testing.html @keithrbennett, since it looks like you use rspec, would you be interested in creating the rspec equivalent of the following minitest extensions just added to SemanticLogger on the master branch? Would be great to include the rspec equivalent when the above minitest helper is published in a gem. It is just in master for now. If you want to try it locally, update Gemfile with: |
Beta Was this translation helpful? Give feedback.
Thank you, Documentation updated.