diff --git a/README.md b/README.md index 4d25554..88e0a1d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/redacting_logger.rb b/lib/redacting_logger.rb index bccac75..f398918 100644 --- a/lib/redacting_logger.rb +++ b/lib/redacting_logger.rb @@ -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] 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 diff --git a/lib/version.rb b/lib/version.rb index 9d7ae2e..22cb395 100644 --- a/lib/version.rb +++ b/lib/version.rb @@ -2,6 +2,6 @@ module RedactingLogger module Version - VERSION = "0.0.4" + VERSION = "0.1.0" end end diff --git a/spec/lib/redacting_logger_spec.rb b/spec/lib/redacting_logger_spec.rb index b536c11..94f820c 100644 --- a/spec/lib/redacting_logger_spec.rb +++ b/spec/lib/redacting_logger_spec.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + require "logger" require "stringio" require_relative "../spec_helper" @@ -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 @@ -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) @@ -41,14 +42,14 @@ 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 @@ -56,8 +57,8 @@ 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 @@ -65,8 +66,8 @@ 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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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