`datetime_format=` does not apply to custom formatters
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 161
- Forks
- 71
- Avg merge
- 3d 22h
- Merged PRs (30d)
- 2
Description
When a custom formatter that responds to datetime_format= is set on a Logger, calling Logger#datetime_format= applies the format to the internal @default_formatter instead of the active formatter. This means the datetime format has no effect on log output.
Steps to reproduce
require 'logger'
custom_formatter = Logger::Formatter.new
logger = Logger.new($stdout, formatter: custom_formatter, datetime_format: "%d%b%Y@%H:%M:%S")
# Expected: datetime_format is set on custom_formatter
logger.datetime_format # => nil (unexpected)
custom_formatter.datetime_format # => nil (unexpected)
logger.info("hello")
# Outputs default datetime format instead of "%d%b%Y@%H:%M:%S"
The same issue occurs when setting datetime_format after initialization:
logger = Logger.new($stdout, formatter: Logger::Formatter.new)
logger.datetime_format = "%d%b%Y@%H:%M:%S"
logger.info("hello")
# Still uses default datetime format
Root cause
Logger#datetime_format= always delegates to @default_formatter:
def datetime_format=(datetime_format)
@default_formatter.datetime_format = datetime_format
end
But format_message uses @formatter when set:
def format_message(severity, datetime, progname, msg)
(@formatter || @default_formatter).call(severity, datetime, progname, msg)
end
Additionally, in initialize, datetime_format is set before the custom formatter is assigned, so it never reaches the custom formatter even during construction.
Expected behavior
datetime_format= should apply to the active formatter (custom or default) if it responds to datetime_format=.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading Logger#datetime_format=, initialize, and format_message, using the reproduction steps to trace which formatter receives the setting. Add regression coverage for custom formatters during and after initialization, and verify that log output uses the requested datetime format.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ruby
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 72/100