|
| 1 | +import React, {useEffect, useState} from 'react'; |
| 2 | +import {Alert, Button, StyleSheet, Switch, Text, View} from 'react-native'; |
| 3 | +import {FileLogger, LogLevel} from 'react-native-file-logger'; |
| 4 | + |
| 5 | +export const App = () => { |
| 6 | + const [logLevel, setLogLevel] = useState(LogLevel.Debug); |
| 7 | + const [enabled, setEnabled] = useState(true); |
| 8 | + |
| 9 | + useEffect(() => { |
| 10 | + FileLogger.configure({logLevel: LogLevel.Debug}).then(() => |
| 11 | + console.log('File-logger configured'), |
| 12 | + ); |
| 13 | + }, []); |
| 14 | + |
| 15 | + const showLogFilePaths = async () => { |
| 16 | + Alert.alert('File paths', (await FileLogger.getLogFilePaths()).join('\n')); |
| 17 | + }; |
| 18 | + |
| 19 | + const changeEnabled = (value: boolean) => { |
| 20 | + if (value) { |
| 21 | + FileLogger.enableConsoleCapture(); |
| 22 | + } else { |
| 23 | + FileLogger.disableConsoleCapture(); |
| 24 | + } |
| 25 | + setEnabled(value); |
| 26 | + }; |
| 27 | + |
| 28 | + const changeLogLevel = () => { |
| 29 | + const nextLogLevel = (logLevel + 1) % 4; |
| 30 | + FileLogger.setLogLevel(nextLogLevel); |
| 31 | + setLogLevel(nextLogLevel); |
| 32 | + }; |
| 33 | + |
| 34 | + const sendLogFilesByEmail = () => { |
| 35 | + FileLogger.sendLogFilesByEmail({ |
| 36 | + |
| 37 | + subject: 'Log files', |
| 38 | + body: 'Please find attached the log files from your app', |
| 39 | + }); |
| 40 | + }; |
| 41 | + |
| 42 | + const deleteLogFiles = async () => { |
| 43 | + FileLogger.deleteLogFiles(); |
| 44 | + Alert.alert('Log files deleted'); |
| 45 | + }; |
| 46 | + |
| 47 | + return ( |
| 48 | + <View style={styles.container}> |
| 49 | + <View style={styles.buttonContainer}> |
| 50 | + <View style={styles.button}> |
| 51 | + <Button |
| 52 | + title="Log info" |
| 53 | + onPress={() => console.log('Log info', {nested: {data: 123}})} |
| 54 | + /> |
| 55 | + </View> |
| 56 | + <View style={styles.button}> |
| 57 | + <Button |
| 58 | + title="Log warning" |
| 59 | + onPress={() => console.warn('Log warning', {nested: {data: 456}})} |
| 60 | + /> |
| 61 | + </View> |
| 62 | + <View style={styles.button}> |
| 63 | + <Button title="Change log level" onPress={changeLogLevel} /> |
| 64 | + </View> |
| 65 | + <View style={styles.button}> |
| 66 | + <Button title="Show file paths" onPress={showLogFilePaths} /> |
| 67 | + </View> |
| 68 | + <View style={styles.button}> |
| 69 | + <Button title="Send files by email" onPress={sendLogFilesByEmail} /> |
| 70 | + </View> |
| 71 | + <View style={styles.button}> |
| 72 | + <Button title="Delete files" onPress={deleteLogFiles} /> |
| 73 | + </View> |
| 74 | + </View> |
| 75 | + <View style={styles.settingsRow}> |
| 76 | + <Text style={styles.settingsLabel}>Enabled</Text> |
| 77 | + <Switch value={enabled} onValueChange={changeEnabled} /> |
| 78 | + </View> |
| 79 | + <View style={styles.settingsRow}> |
| 80 | + <Text style={styles.settingsLabel}>Log Level</Text> |
| 81 | + <Text style={styles.settingsValue}>{LogLevel[logLevel]}</Text> |
| 82 | + </View> |
| 83 | + </View> |
| 84 | + ); |
| 85 | +}; |
| 86 | + |
| 87 | +const styles = StyleSheet.create({ |
| 88 | + container: { |
| 89 | + flex: 1, |
| 90 | + padding: 25, |
| 91 | + backgroundColor: '#f5fcff', |
| 92 | + }, |
| 93 | + buttonContainer: { |
| 94 | + flex: 1, |
| 95 | + justifyContent: 'center', |
| 96 | + }, |
| 97 | + button: { |
| 98 | + marginVertical: 6, |
| 99 | + }, |
| 100 | + settingsRow: { |
| 101 | + paddingHorizontal: 16, |
| 102 | + paddingVertical: 10, |
| 103 | + marginBottom: 20, |
| 104 | + minHeight: 50, |
| 105 | + flexDirection: 'row', |
| 106 | + alignItems: 'center', |
| 107 | + backgroundColor: '#dff4ff', |
| 108 | + borderRadius: 6, |
| 109 | + }, |
| 110 | + settingsLabel: { |
| 111 | + flex: 1, |
| 112 | + fontWeight: '500', |
| 113 | + color: 'black', |
| 114 | + }, |
| 115 | + settingsValue: { |
| 116 | + color: 'black', |
| 117 | + }, |
| 118 | +}); |
0 commit comments