Skip to content
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

Appdirs using logger loads log configuration before being able to set the log directory via appdirs #40

Closed
marvk opened this issue Jan 25, 2021 · 3 comments
Assignees
Milestone

Comments

@marvk
Copy link

marvk commented Jan 25, 2021

This very much relates to #4

I'm trying to use AppDirs to set my log file directory, but AppDirs using a logger itself makes this very difficult as it loads my logger before I am able to set my logging directory system property to a path obtained by AppDirs.

If you want to keep logging in AppDirs (Which at this time is only used in ShellFolderResolver and AppDirsFactory), how about some method of obtaining an AppDirsFactory with a disabled logger?

public class AppDirsFactory {
    private static Logger logger;

    private AppDirsFactory() {
        super();
    }

    public static AppDirs getInstanceWithoutLogger() {
        logger = NOPLogger.NOP_LOGGER;
        return createInstance(false);
    }

    public static AppDirs getInstance() {
        logger = LoggerFactory.getLogger(AppDirsFactory.class);
        return createInstance(true);
    }

    private static AppDirs createInstance(final boolean logging) {
        String os = System.getProperty("os.name").toLowerCase();
        if (os.startsWith("mac os x")) {
            // ...
        } else if (os.startsWith("windows")) {
            logger.debug("os.name {} is resolved to Windows", os);
            // do the same in ShellFolderResolver
            WindowsFolderResolver folderResolver = new ShellFolderResolver(logging);
            return new WindowsAppDirs(folderResolver);
        } else {
            // ...
        }
    }
}

I realize this isn't super pretty, but I feel like something like this is much needed. Not creating a logger while trying to find out which directory to log to would improve this already excellent little library, and I feel like this must be a pretty common use case.

@harawata
Copy link
Owner

Thank you @marvk for the report!
I'll just remove logging.
Please give me a day or two.

@harawata harawata added this to the 1.3.0 milestone Jan 25, 2021
@harawata harawata self-assigned this Jan 25, 2021
harawata added a commit that referenced this issue Jan 26, 2021
@harawata
Copy link
Owner

I have just released 1.2.1 .
Please let me know if there is any issue!

@marvk
Copy link
Author

marvk commented Jan 26, 2021

Thanks for the quick response, looks good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants