- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1.7k
Add org.apache.logging.log4j.core.appender.FileManager.getPath() #3855
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 2.x
Are you sure you want to change the base?
Conversation
| @garydgregory, would you mind sharing the use case, please? What problem is this change set addressing? | 
| The use case is that we (proprietary code base) have custom code that interacts with configurations, file Appender and Managers, we are moving from IO to NIO, and Strings shouldn't be used as an abstraction for where to find a file. Java has a nice abstraction for that: Path. You can also see that internally the file manager also needs a Path. HTH | 
| I'm a strong advocate for using  Given that this is clearly important for your employer, would it be possible to ask them to let you work on #2117 during work hours? 🙏 I recall you volunteered for that on Slack, though it's totally understandable that tackling something of that scope in your spare time isn’t feasible, especially with all the Commons tasks already on your plate. There are clear benefits to transitioning Log4j’s file handling from IO to NIO: 
 | 
| Hi all, TLDR: Let's bring this PR in and then... Let's not confuse file Appenders and file Managers ;-) I took a glance at the rolling file manager and it's a much larger code base. This PR is small and focused, which is a good thing. I am willing to volunteer to migrate the other manager code ((rolling) from IO to NIO in a separate PR over the weekend probably, or at the very least initially from presenting  I want to think about the migration from IO to NIO in two steps: 
 WDYT? For 3.0, all bets are off and breaking BC is OK IMO in this area. | 
| Hi @garydgregory, 
 I believe it's actually too narrowly scoped, to the point where it loses sight of the broader context. To be candid: would anyone upgrade Log4j just because the changelog says that  Tackling the rolling file appender is definitely hard (otherwise I wouldn’t have asked you in the first place!). I’ve actually got a local branch where I started migrating the  What would you say about: Step 1: Fix  
 This change would provide real value worth highlighting in release notes. Would you be open to expand your PR to cover this step? Step 2: Migrate rolling file appenders to NIO 
 | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @garydgregory,
I am willing to volunteer to migrate the other manager code ((rolling) from IO to NIO in a separate PR over the weekend probably, or at the very least initially from presenting
Pathinstead/in addition toStringin the API.
I appreciate the willingness to take this on, but realistically this migration is larger than a "weekend task", especially given your already significant OSS workload. It’s important that we set expectations we can actually deliver on, both for ourselves and for the users waiting on this work. Let’s plan this in achievable steps so it doesn’t stall again.
As proposed on dev@logging (see this thread), I can take care of migrating the internal handling of the current log file to NIO. That part is already in progress and a draft exists. We can address the rolling-related logic separately in a later release since that’s a larger task.
What would really help in this PR is expanding it so that the public API is consistent across all file-based managers, not just FileManager. Right now, this PR introduces getPath() only in one class, which would result in a partial and uneven API. Users often rely on multiple appenders, and staggered API availability makes adoption confusing.
Therefore I propose to modify this PR to:
- 
Add the equivalent API method ( getPath()orgetFilePath()) to:- RandomAccessFileManager
- MemoryMappedFileManager
 
- 
Think about adding such methods to FileAppender,FileAppender.Builderand similar.
That way:
- We avoid shipping a release with a half-finished API.
- The change is meaningful enough to highlight in release notes.
- Users can start adopting the new API consistently.
| * @return The name of the file being managed. | ||
| * @since 2.26.0 | ||
| */ | ||
| public Path getPath() { | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A small naming suggestion: since we already have a getFileName() method, it might be clearer to name this new method getFilePath() instead of getPath().
It would also help to document the expected characteristics of the returned Path, for example:
- Whether the returned path is absolute and normalized. Since multiple appenders (even across different logger contexts) may share a single FileManager, using an absolute, normalizedPathavoids ambiguity and makes comparisons safer.
- Whether the value may change over time. In particular, RollingFileManagercan update its active file during rollover (especially with the DirectWriteRolloverStrategy), so it would be useful to document that the returned path may not be stable across invocations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A small naming suggestion: since we already have a getFileName() method, it might be clearer to name this new method getFilePath() instead of getPath().
Names are important and I'm glad we're discussing them.
IMO, "getFilePath()" is bad because it implies this is a Path to a file:// only, and since a Path is an interface for locating a file in a file system, we should not set expectations that the implementation of the Path is only for file-schemed Paths.
It doesn't matter that there's already an API called "getFileName()", a file name is very different from a Path, where the expectation (from a NIO Path POV) is that the file name is the last segment, the "bar.txt" in "/foo/bar.txt". Having a common prefix between these two methods has no benefit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was interpreting filePath as “path of the file” as opposed to the path to its parent directory or similar.
I agree that “path with file: scheme” would be the wrong interpretation, even if currently that is the only scheme we support.
Add
org.apache.logging.log4j.core.appender.FileManager.getPath()Important
Base your changes on
2.xbranch if you are targeting Log4j 2; usemainotherwise.Checklist
Before we can review and merge your changes, please go through the checklist below. If you're still working on some items, feel free to submit your pull request as a draft—our CI will help guide you through the remaining steps.
✅ Required checks
License: I confirm that my changes are submitted under the Apache License, Version 2.0.
Commit signatures: All commits are signed and verifiable. (See GitHub Docs on Commit Signature Verification).
Code formatting: The code is formatted according to the project’s style guide.
How to check and fix formatting
./mvnw spotless:check./mvnw spotless:applySee the build instructions for details.
Build & Test: I verified that the project builds and all unit tests pass.
How to build the project
Run:
./mvnw verifySee the build instructions for details.
Note that there are still problems on Windows:
🧪 Tests (select one)
📝 Changelog (select one)
src/changelog/.2.x.x. (See Changelog Entry File Guide).