Add on-device logger
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
import * as FileSystem from 'expo-file-system';
|
||||
|
||||
const logFileUri = FileSystem.documentDirectory + 'app_logs.txt';
|
||||
|
||||
export const logger = {
|
||||
log: async (message: string) => {
|
||||
try {
|
||||
const timestamp = new Date().toISOString();
|
||||
const logLine = `[INFO] ${timestamp}: ${message}\n`;
|
||||
console.log(logLine.trim());
|
||||
|
||||
const fileInfo = await FileSystem.getInfoAsync(logFileUri);
|
||||
if (!fileInfo.exists) {
|
||||
await FileSystem.writeAsStringAsync(logFileUri, logLine, { encoding: FileSystem.EncodingType.UTF8 });
|
||||
} else {
|
||||
// Read existing and append (simplistic approach for small logs)
|
||||
// expo-file-system doesn't have an append method out of the box in older versions,
|
||||
// but wait, we can just read, then write. To avoid OOM we should truncate if too big.
|
||||
const current = await FileSystem.readAsStringAsync(logFileUri, { encoding: FileSystem.EncodingType.UTF8 });
|
||||
// Keep last 100000 chars
|
||||
let newContent = current + logLine;
|
||||
if (newContent.length > 100000) {
|
||||
newContent = newContent.slice(-100000);
|
||||
}
|
||||
await FileSystem.writeAsStringAsync(logFileUri, newContent, { encoding: FileSystem.EncodingType.UTF8 });
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('Failed to write log:', e);
|
||||
}
|
||||
},
|
||||
|
||||
error: async (message: string, error?: any) => {
|
||||
try {
|
||||
const timestamp = new Date().toISOString();
|
||||
let errorString = '';
|
||||
if (error) {
|
||||
if (error instanceof Error) {
|
||||
errorString = `\n Name: ${error.name}\n Message: ${error.message}\n Stack: ${error.stack}`;
|
||||
} else if (typeof error === 'object') {
|
||||
errorString = `\n Object: ${JSON.stringify(error)}`;
|
||||
} else {
|
||||
errorString = `\n Value: ${String(error)}`;
|
||||
}
|
||||
}
|
||||
|
||||
const logLine = `[ERROR] ${timestamp}: ${message}${errorString}\n`;
|
||||
console.error(logLine.trim());
|
||||
|
||||
const fileInfo = await FileSystem.getInfoAsync(logFileUri);
|
||||
if (!fileInfo.exists) {
|
||||
await FileSystem.writeAsStringAsync(logFileUri, logLine, { encoding: FileSystem.EncodingType.UTF8 });
|
||||
} else {
|
||||
const current = await FileSystem.readAsStringAsync(logFileUri, { encoding: FileSystem.EncodingType.UTF8 });
|
||||
let newContent = current + logLine;
|
||||
if (newContent.length > 100000) {
|
||||
newContent = newContent.slice(-100000);
|
||||
}
|
||||
await FileSystem.writeAsStringAsync(logFileUri, newContent, { encoding: FileSystem.EncodingType.UTF8 });
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('Failed to write error log:', e);
|
||||
}
|
||||
},
|
||||
|
||||
readLogs: async (): Promise<string> => {
|
||||
try {
|
||||
const fileInfo = await FileSystem.getInfoAsync(logFileUri);
|
||||
if (!fileInfo.exists) return 'Keine Logs vorhanden.';
|
||||
return await FileSystem.readAsStringAsync(logFileUri, { encoding: FileSystem.EncodingType.UTF8 });
|
||||
} catch (e) {
|
||||
return `Fehler beim Lesen der Logs: ${e}`;
|
||||
}
|
||||
},
|
||||
|
||||
clearLogs: async () => {
|
||||
try {
|
||||
await FileSystem.deleteAsync(logFileUri, { idempotent: true });
|
||||
} catch (e) {
|
||||
console.log('Failed to clear logs:', e);
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user