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

Filename/FilePath for rnFsFileAsync #4

Closed
creambyemute opened this issue Apr 8, 2020 · 4 comments
Closed

Filename/FilePath for rnFsFileAsync #4

creambyemute opened this issue Apr 8, 2020 · 4 comments

Comments

@creambyemute
Copy link

Hey there,

What would you think about slightly changing the rnFsFileAsync so that the filename/filepath is providable and not hardcoded?

Example usage:
An App where you can login and want to log per user and not per app.

@alessandro-bottamedi
Copy link
Collaborator

Hi, i've just added the possibility to set transports options, as you can see in the docs now you can change the file name for the rnFsFileAsync (the transportOptions.loggerName property).
Abaout the path, in order to maintain the highest compatibility between android and ios for now I have left RNFS.DocumentDirectoryPath as I'm sure it works on all devices.
Did you have any advice to handle the path?

@creambyemute
Copy link
Author

creambyemute commented Apr 8, 2020

@alessandro-bottamedi I'd say make the path optional, if provided use that path, otherwise default to RNFS.DocumentDirectoryPath?

For the meantime I created my own customFsLog function which is called by my customLogger

private customLogger: transportFunctionType = async (msg: any, level: any) => {
        if (this.logLevel === "error") {
            if (this.isString(msg)) {
                Analytics.trackEvent(this.logTag, { msg: msg as string });
            } else if (this.isErrorObject(msg)) {
                Analytics.trackEvent(this.logTag, msg);
            } else if (this.isObject(msg)) {
                Analytics.trackEvent(this.logTag, msg);
            }
        }

        const logMessage = `${this.logTag}: ${JSON.stringify(msg)}`;
        this.fsLog(logMessage, level);

        if (__DEV__) {
            switch (this.logLevel) {
                case "info":
                    console.log(logMessage);
                    break;
                case "warn":
                case "error":
                    console.warn(logMessage);
                    break;
                default:
                    break;
            }
        }
    };
private fsLog: transportFunctionType = async (msg: any, level: any) => {
        const output = `${new Date().toLocaleString()} | ${level.text.toUpperCase()} | ${msg}\n`;
        const filePath = LowDBAdapter.path
            ? `${stores.rootStore.authStore.userDirectoryPath}rnfslogs.txt`
            : `${Constants.DatabaseDir}/rnfslogs.txt`;
        RNFS.appendFile(filePath, output, "utf8")
            .then(() => {})
            .catch((err: any) => {
                console.warn(err);
            });
    };

We're using DocumentDirectoryPath as well

const DatabaseDir = Platform.select({
    ios: `${RNFS.DocumentDirectoryPath}`,
    android: `${RNFS.DocumentDirectoryPath}/../databases`
});

const DownloadDir = Platform.select({
    ios: `${RNFS.DocumentDirectoryPath}`,
    android: `${RNFS.DownloadDirectoryPath}`
});

I'm fine if you don't add the path as it's pretty easy/straightforward to setup a custom fs log already as shown above.
I think this is even the better solution as like this, the library does not have to care about directory locations/permissions.

@alessandro-bottamedi
Copy link
Collaborator

alessandro-bottamedi commented Apr 9, 2020

I added the option to set the path for rnFsFileAsync.
To set the path and filename after "RNFS.DocumentDirectoryPath" we can use the fileName option.
if someone wants to change the path "RNFS.DocumentDirectoryPath", they can use the filePath option.
All TransportOptions are not required to keep the library easy to use.

@vietmobilefolk
Copy link

vietmobilefolk commented Jul 29, 2022

Two options to customize file and folder are: fileName and filePath
not loggerPath

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

No branches or pull requests

3 participants