From 2aa3b9090b914d3f6dd94cd79c08f8be157d318f Mon Sep 17 00:00:00 2001 From: Ionatan Wiznia Date: Fri, 23 Jul 2021 17:38:41 +0200 Subject: [PATCH] More log improvements: log navigations, improve logs, flush on logout and app inactive --- package-lock.json | 4 +-- package.json | 2 +- src/App.js | 48 +++++++++++++++++++++++------ src/libs/Log.js | 7 +++++ src/libs/Navigation/Navigation.js | 2 ++ src/libs/PusherConnectionManager.js | 6 +++- src/libs/actions/Session.js | 2 ++ 7 files changed, 57 insertions(+), 14 deletions(-) diff --git a/package-lock.json b/package-lock.json index e3981ba51254..09d03bf7fe3f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23059,8 +23059,8 @@ } }, "expensify-common": { - "version": "git://github.com/Expensify/expensify-common.git#8e163a9b72c1bd33c83ee5ebef6b7a69ce6f54b4", - "from": "git://github.com/Expensify/expensify-common.git#8e163a9b72c1bd33c83ee5ebef6b7a69ce6f54b4", + "version": "git://github.com/Expensify/expensify-common.git#5ddf00f89e78ab22a50977eb367d4c2805555b93", + "from": "git://github.com/Expensify/expensify-common.git#5ddf00f89e78ab22a50977eb367d4c2805555b93", "requires": { "classnames": "2.3.1", "clipboard": "2.0.4", diff --git a/package.json b/package.json index 5d727c9ceaae..49290e9bc242 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,7 @@ "electron-log": "^4.3.5", "electron-serve": "^1.0.0", "electron-updater": "^4.3.4", - "expensify-common": "git://github.com/Expensify/expensify-common.git#8e163a9b72c1bd33c83ee5ebef6b7a69ce6f54b4", + "expensify-common": "git://github.com/Expensify/expensify-common.git#5ddf00f89e78ab22a50977eb367d4c2805555b93", "expo-haptics": "^10.0.0", "file-loader": "^6.0.0", "html-entities": "^1.3.1", diff --git a/src/App.js b/src/App.js index 0ac8e0ac8e60..2c4b6d331740 100644 --- a/src/App.js +++ b/src/App.js @@ -1,9 +1,10 @@ -import React from 'react'; -import {LogBox} from 'react-native'; +import React, {Component} from 'react'; +import {AppState, LogBox} from 'react-native'; import {SafeAreaProvider} from 'react-native-safe-area-context'; import CustomStatusBar from './components/CustomStatusBar'; import ErrorBoundary from './components/ErrorBoundary'; import Expensify from './Expensify'; +import Log from './libs/Log'; LogBox.ignoreLogs([ // Basically it means that if the app goes in the background and back to foreground on Android, @@ -15,14 +16,41 @@ LogBox.ignoreLogs([ 'Require cycle: node_modules/rn-fetch-blob', ]); -const App = () => ( - - - - - - -); +class App extends Component { + constructor(props) { + super(props); + this.state = { + appState: AppState.currentState, + }; + this.handleAppStateChange = this.handleAppStateChange.bind(this); + } + + componentDidMount() { + AppState.addEventListener('change', this.handleAppStateChange); + } + + componentWillUnmount() { + AppState.removeEventListener('change', this.handleAppStateChange); + } + + handleAppStateChange(nextAppState) { + if (nextAppState.match(/inactive|background/) && this.state.appState === 'active') { + Log.info('Flushing logs as app is going inactive', true); + } + this.setState({appState: nextAppState}); + } + + render() { + return ( + + + + + + + ); + } +} App.displayName = 'App'; diff --git a/src/libs/Log.js b/src/libs/Log.js index f24b68f88ad7..22f7988ae853 100644 --- a/src/libs/Log.js +++ b/src/libs/Log.js @@ -5,6 +5,9 @@ import getPlatform from './getPlatform'; import {version} from '../../package.json'; import NetworkConnection from './NetworkConnection'; +let timeout = null; +let info; + /** * Network interface for logger. * @@ -19,6 +22,8 @@ function serverLoggingCallback(params) { if (requestParams.parameters) { requestParams.parameters = JSON.stringify(params.parameters); } + clearTimeout(timeout); + timeout = setTimeout(() => info('Flushing logs older than 10 minutes', true), 10 * 60 * 1000); return API.Log(requestParams); } @@ -33,6 +38,8 @@ const Log = new Logger({ }, isDebug: !CONFIG.IS_IN_PRODUCTION, }); +info = Log.info; +timeout = setTimeout(() => info('Flushing logs older than 10 minutes', true), 10 * 60 * 1000); NetworkConnection.registerLogInfoCallback(Log.info); export default Log; diff --git a/src/libs/Navigation/Navigation.js b/src/libs/Navigation/Navigation.js index 894974fbc055..f5a048dc97c0 100644 --- a/src/libs/Navigation/Navigation.js +++ b/src/libs/Navigation/Navigation.js @@ -4,6 +4,7 @@ import {StackActions, DrawerActions} from '@react-navigation/native'; import PropTypes from 'prop-types'; import Onyx from 'react-native-onyx'; import linkTo from './linkTo'; +import Log from '../Log'; import ROUTES from '../../ROUTES'; import SCREENS from '../../SCREENS'; import CustomActions from './CustomActions'; @@ -54,6 +55,7 @@ function goBack(shouldOpenDrawer = true) { * @param {String} route */ function navigate(route = ROUTES.HOME) { + Log.info('Navigating to route', false, {route}); if (route === ROUTES.HOME) { if (isLoggedIn) { openDrawer(); diff --git a/src/libs/PusherConnectionManager.js b/src/libs/PusherConnectionManager.js index e5a778632c7c..9873ed33d20c 100644 --- a/src/libs/PusherConnectionManager.js +++ b/src/libs/PusherConnectionManager.js @@ -44,7 +44,11 @@ function init() { return; } - Log.info('[PusherConnectionManager] Pusher authenticated successfully', false, {channelName: channel.name}); + Log.info( + '[PusherConnectionManager] Pusher authenticated successfully', + false, + {channelName: channel.name}, + ); callback(null, data); }) .catch((error) => { diff --git a/src/libs/actions/Session.js b/src/libs/actions/Session.js index 3540939041fd..b1008a39d168 100644 --- a/src/libs/actions/Session.js +++ b/src/libs/actions/Session.js @@ -5,6 +5,7 @@ import ONYXKEYS from '../../ONYXKEYS'; import redirectToSignIn from './SignInRedirect'; import * as API from '../API'; import CONFIG from '../../CONFIG'; +import Log from '../Log'; import PushNotification from '../Notification/PushNotification'; import Timing from './Timing'; import CONST from '../../CONST'; @@ -61,6 +62,7 @@ function createAccount(login) { * Clears the Onyx store and redirects user to the sign in page */ function signOut() { + Log.info('Flushing logs before signing out', true); if (credentials && credentials.autoGeneratedLogin) { // Clean up the login that we created API.DeleteLogin({