Security News
Opengrep Emerges as Open Source Alternative Amid Semgrep Licensing Controversy
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
lumberjack_capture_device
Advanced tools
This is a plugin device for the lumberjack gem that enables capturing log messages in a test suite so that assertions can be made against them. It provides and easy and stable method of testing that specific log messages are being sent to a logger.
Using mocks and stubs on a logger to test that it receives messages can make for a brittle test suite since there can a wide variety of code writing messages to logs and your test suite may have a higher log level turned on causing it skip messages at a lower level.
For instance, this rspec code can break if any of the code called by the do_something
writes a different info log message:
do_something
expect(Rail.logger).to receive(:info).with("Something happened")
It will also break if the test suite logger has the log level set to warn
or higher since it will then skip all info and debug messages.
You can call the Lumberjack::CaptureDevice.capture
method to override a logger so that it will capture log entries within a block to an in memory buffer. This method will yield the capturing log device as well as return it as the result of the method. The log level will also be temporarily set to debug within the block, so you can capture all log messages without having to change the log level for the entire test suite.
You can use the include?
method on the log device to determine if specific log entries were made. This would be the equivalent code to the above rspec test, but without the brittleness of mocking method calls:
Lumberjack::CaptureDevice.capture(Rails.logger) do |logs|
do_something
expect(logs).to include(level: :info, message: "Something happened")
end
You can also write that same test as:
logs = Lumberjack::CaptureDevice.capture(Rails.logger) { do_something }
expect(logs).to include(level: :info, message: "Something happened")
For MiniTest, you could assert:
logs = Lumberjack::CaptureDevice.capture(Rails.logger) { do_something }
assert(logs.include?(level: :info, message: "Something happened"))
You can filter the logs on level, message, and tags. The level option can take either a label (i.e. :warn
) or a constant (i.e. Logger::WARN
). The message filter can be either an exact string, or a regular expression or any matcher supported by your test library. The tags argument can match tags with a Hash mapping tag names to the matcher.
expect(logs).to include(level: :info, message: /something/i)
expect(logs).to include(level: Logger::INFO, tags: {foo: "bar"})
expect(logs).to include(tags: {foo: anything, count: {one: 1}})
You can also use the Lumberjack::CaptureDevice#extract
method with the same arguments as used by include?
to grab all lines that match the filters. And finally, you can access all the log entries with Lumberjack::CaptureDevice#buffer
.
FAQs
Unknown package
We found that lumberjack_capture_device demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.