Skip to content

Commit

Permalink
Merge pull request #2 from GrantBirki/follow-logger-inputs
Browse files Browse the repository at this point in the history
Follow "Logger" Inputs
  • Loading branch information
GrantBirki authored Nov 14, 2023
2 parents fc7298c + da20c3a commit 19bb347
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ require "redacting_logger"

# Create a new logger
logger = RedactingLogger.new(
$stdout, # The device to log to (defaults to $stdout if not provided)
redact_patterns: [/REDACTED_PATTERN1/, /REDACTED_PATTERN2/], # An array of Regexp patterns to redact from the logs
log_device: $stdout, # The device to log to
level: Logger::INFO, # The log level to use
redacted_msg: "[REDACTED]", # The message to replace the redacted patterns with
use_default_patterns: true # Whether to use the default built-in patterns or not
Expand Down
12 changes: 9 additions & 3 deletions lib/redacting_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,25 @@
class RedactingLogger < Logger
# Initializes a new instance of the RedactingLogger class.
#
# @param logdev [Object] The log device. Defaults to $stdout.
# @param shift_age [Integer] The number of old log files to keep.
# @param shift_size [Integer] The maximum logfile size.
# @param redact_patterns [Array<String>] The patterns to redact from the log messages. Defaults to [].
# @param log_device [Object] The log device (file, STDOUT, etc.) to write to. Defaults to STDOUT.
# @param redacted_msg [String] The message to replace the redacted patterns with.
# @param use_default_patterns [Boolean] Whether to use the default patterns or not.
# @param kwargs [Hash] Additional options to pass to the Logger class.
#
# logdev, shift_age, and shift_size are all using the defaults from the standard Logger class. -> https://github.com/ruby/logger/blob/0996f90650fd95718f0ffe835b965de18654b71c/lib/logger.rb#L578-L580
def initialize(
logdev = $stdout,
shift_age = 0,
shift_size = 1048576,
redact_patterns: [],
log_device: $stdout,
redacted_msg: "[REDACTED]",
use_default_patterns: true,
**kwargs
)
super(log_device, **kwargs)
super(logdev, **kwargs)
@redact_patterns = redact_patterns
@redacted_msg = redacted_msg
@redact_patterns += Patterns::DEFAULT if use_default_patterns
Expand Down
2 changes: 1 addition & 1 deletion lib/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module RedactingLogger
module Version
VERSION = "0.0.4"
VERSION = "0.1.0"
end
end
45 changes: 23 additions & 22 deletions spec/lib/redacting_logger_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

require "logger"
require "stringio"
require_relative "../spec_helper"
Expand All @@ -10,8 +11,8 @@
redact_patterns = ["secret", "password"]
level = Logger::INFO
logger = RedactingLogger.new(
$stdout,
redact_patterns:,
log_device: $stdout,
level:,
redacted_msg: "!!!REDACTED!!!",
use_default_patterns: false
Expand All @@ -32,7 +33,7 @@
end

it "ensures the class is initialized properly with default values and uses built-in patterns" do
logger = RedactingLogger.new(use_default_patterns: true)
logger = RedactingLogger.new($stdout, use_default_patterns: true)
expect(logger.level).to eq(Logger::DEBUG)
expect(logger.instance_variable_get(:@redact_patterns).length).to be > 0
expect(logger.instance_variable_get(:@logdev).dev).to eq($stdout)
Expand All @@ -41,32 +42,32 @@
end

context "#add" do
let(:log_device) { StringIO.new }
let(:logger) { RedactingLogger.new(redact_patterns: [/secret/, /password/, /token_[A-Z]{5}/], log_device:) }
let(:logdev) { StringIO.new }
let(:logger) { RedactingLogger.new(logdev, redact_patterns: [/secret/, /password/, /token_[A-Z]{5}/]) }

it "ensures the message is redacted" do
logger.info { ["This is a secret password", nil] }

log_device.rewind
log_output = log_device.read
logdev.rewind
log_output = logdev.read

expect(log_output).to match(/This is a \[REDACTED\] \[REDACTED\]/)
end

it "ensures the progname is redacted" do
logger.info { ["This is a message", "secret"] }

log_device.rewind
log_output = log_device.read
logdev.rewind
log_output = logdev.read

expect(log_output).to match(/\[REDACTED\]: This is a message/)
end

it "redacts the message when it is a substring of the redact pattern" do
logger.info("This is a supersecretmessage")

log_device.rewind
log_output = log_device.read
logdev.rewind
log_output = logdev.read
expect(log_output).to match(/This is a super\[REDACTED\]message/)
end

Expand All @@ -75,8 +76,8 @@

logger.info("logging in with token #{token} ...")

log_device.rewind
log_output = log_device.read
logdev.rewind
log_output = logdev.read
expect(log_output).to match(/logging in with token \[REDACTED\] .../)
end

Expand All @@ -85,8 +86,8 @@

logger.info("logging in with token #{token} ...")

log_device.rewind
log_output = log_device.read
logdev.rewind
log_output = logdev.read
expect(log_output).to match(/logging in with token \[REDACTED\] .../)
end

Expand All @@ -96,8 +97,8 @@

logger.warn("oh no, I failed to login with that token: #{token}, try again")

log_device.rewind
log_output = log_device.read
logdev.rewind
log_output = logdev.read
expect(log_output).to match(/oh no, I failed to login with that token: \[REDACTED\], try again/)
end

Expand All @@ -106,8 +107,8 @@

logger.debug("GitHub Actions token: #{token}")

log_device.rewind
log_output = log_device.read
logdev.rewind
log_output = logdev.read
expect(log_output).to match(/GitHub Actions token: \[REDACTED\]/)
end

Expand All @@ -116,8 +117,8 @@

logger.fatal("Custom token: #{token}")

log_device.rewind
log_output = log_device.read
logdev.rewind
log_output = logdev.read
expect(log_output).to match(/Custom token: \[REDACTED\]/)
end

Expand All @@ -126,8 +127,8 @@

logger.fatal("Custom token: #{token}")

log_device.rewind
log_output = log_device.read
logdev.rewind
log_output = logdev.read

expect(log_output).to match(/Custom token: token_ABCD/)
end
Expand Down

0 comments on commit 19bb347

Please sign in to comment.