react-native-logs
react-native-logs copied to clipboard
Suggested improvements to fileAsyncTransport.
Hi there @alessandro-bottamedi, thanks for making react-native-logs.
I'd like to offer a few suggestions for future improvements. They all apply to fileAsyncTransport.
- Option to auto-create log directories, if missing. e.g. if app wants to log to
${documentsDirectory}/logs}, that "logs" subdirectory might be missing when app is first installed. Perhaps acreateDirectoriesfield in the config? - Option to roll over log files if they exceed a max file size on disk. Perhaps a
maxFileSizefield in the config? - Option to clean up old log files on launch, retaining just the N most recent log files. Perhaps a
maxLogFileCountfield in the config?
Cheers!
It might be nice to add a means to flush logs to disk.
I believe that it's better to perform all these tasks before the initialization of react-native-logs, also for performance reasons. It's better to avoid putting them in the fileAsyncTransport, which is executed every time an attempt is made to write a log. For example, we could compress the last log file upon opening the app:
import { zip } from 'react-native-zip-archive'
import RNFS from 'react-native-fs'
import { logger, fileAsyncTransport } from "react-native-logs"
let date = new Date();
date.setDate(date.getDate() - 1);
let d = date.getDate();
let m = date.getMonth() + 1;
let y = date.getFullYear();
const targetPath = `${DocumentDirectoryPath}/logs_${d}-${m}-${y}.zip`
const sourcePath = `${DocumentDirectoryPath}/logs_${d}-${m}-${y}`
RNFS.exists(sourcePath)
.then((exists) => {
if (exists) {
zip(sourcePath, targetPath)
.then((path) => {
RNFS.unlink(filePath)
})
.catch((error) => {
console.error(error)
})
})
.catch((error) => {
console.error(error);
});
const config = {
severity: "debug",
transport: fileAsyncTransport,
transportOptions: {
FS: RNFS,
fileName: `logs_{date-today}`, // Create a new file every day
},
};
I haven't tested this code; there might be errors...