Merge pull request #538 from vector-im/feature/log_mgmt

Reduce release build log level
This commit is contained in:
Benoit Marty 2019-09-05 15:24:04 +02:00 committed by GitHub
commit 9cedb18921
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 75 additions and 40 deletions

View File

@ -5,7 +5,7 @@ Features:
-

Improvements:
-
- Reduce default release build log level, and lab option to enable more logs.

Other changes:
-

View File

@ -86,9 +86,12 @@ class VectorApplication : Application(), HasVectorInjector, MatrixConfiguration.
vectorComponent = DaggerVectorComponent.factory().create(this)
vectorComponent.inject(this)
vectorUncaughtExceptionHandler.activate(this)
// Log
VectorFileLogger.init(this)
Timber.plant(Timber.DebugTree(), VectorFileLogger)

if (BuildConfig.DEBUG) {
Timber.plant(Timber.DebugTree())
}
Timber.plant(vectorComponent.vectorFileLogger())

if (BuildConfig.DEBUG) {
Stetho.initializeWithDefaults(this)
}

View File

@ -41,6 +41,7 @@ import im.vector.riotx.features.notifications.NotificationBroadcastReceiver
import im.vector.riotx.features.notifications.NotificationDrawerManager
import im.vector.riotx.features.notifications.PushRuleTriggerListener
import im.vector.riotx.features.rageshake.BugReporter
import im.vector.riotx.features.rageshake.VectorFileLogger
import im.vector.riotx.features.rageshake.VectorUncaughtExceptionHandler
import im.vector.riotx.features.settings.VectorPreferences
import javax.inject.Singleton
@ -101,6 +102,8 @@ interface VectorComponent {

fun vectorPreferences(): VectorPreferences

fun vectorFileLogger(): VectorFileLogger

@Component.Factory
interface Factory {
fun create(@BindsInstance context: Context): VectorComponent

View File

@ -52,7 +52,8 @@ import javax.inject.Singleton
*/
@Singleton
class BugReporter @Inject constructor(private val activeSessionHolder: ActiveSessionHolder,
private val versionProvider: VersionProvider) {
private val versionProvider: VersionProvider,
private val vectorFileLogger : VectorFileLogger) {
var inMultiWindowMode = false

companion object {
@ -162,7 +163,7 @@ class BugReporter @Inject constructor(private val activeSessionHolder: ActiveSes
val gzippedFiles = ArrayList<File>()

if (withDevicesLogs) {
val files = VectorFileLogger.getLogFiles()
val files = vectorFileLogger.getLogFiles()

for (f in files) {
if (!mIsCancelled) {

View File

@ -17,43 +17,74 @@
package im.vector.riotx.features.rageshake

import android.content.Context
import android.text.TextUtils
import android.util.Log
import im.vector.riotx.features.settings.VectorPreferences
import timber.log.Timber
import java.io.File
import java.io.IOException
import java.io.PrintWriter
import java.io.StringWriter
import java.text.SimpleDateFormat
import java.util.*
import java.util.logging.*
import java.util.logging.Formatter
import javax.inject.Inject
import javax.inject.Singleton
import kotlin.collections.ArrayList

object VectorFileLogger : Timber.DebugTree() {
private const val LOG_SIZE_BYTES = 20 * 1024 * 1024 // 20MB

private const val LOG_SIZE_BYTES = 50 * 1024 * 1024 // 50MB
private const val LOG_ROTATION_COUNT = 3


@Singleton
class VectorFileLogger @Inject constructor(val context: Context, private val vectorPreferences: VectorPreferences) : Timber.DebugTree() {

// relatively large rotation count because closing > opening the app rotates the log (!)
private const val LOG_ROTATION_COUNT = 15

private val sLogger = Logger.getLogger("im.vector.riotx")
private lateinit var sFileHandler: FileHandler
private lateinit var sCacheDirectory: File
private var sFileName = "riotx"
private var sFileHandler: FileHandler? = null
private var sCacheDirectory: File? = null
private var sFileName = "riotxlogs"

fun init(context: Context) {
private val prioPrefixes = mapOf(
Log.VERBOSE to "V/ ",
Log.DEBUG to "D/ ",
Log.INFO to "I/ ",
Log.WARN to "W/ ",
Log.ERROR to "E/ ",
Log.ASSERT to "WTF/ "
)

init {
val logsDirectoryFile = context.cacheDir.absolutePath + "/logs"

setLogDirectory(File(logsDirectoryFile))
init("RiotXLog")
try {
if (sCacheDirectory != null) {
sFileHandler = FileHandler(sCacheDirectory!!.absolutePath + "/" + sFileName + ".%g.txt", LOG_SIZE_BYTES, LOG_ROTATION_COUNT)
sFileHandler?.formatter = LogFormatter()
sLogger.useParentHandlers = false
sLogger.level = Level.ALL
sLogger.addHandler(sFileHandler)
}
} catch (e: Throwable) {
Timber.e(e, "Failed to initialize FileLogger")
}
}

override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
if (sFileHandler == null) return
if (skipLog(priority)) return
if (t != null) {
logToFile(t)
}
logToFile(prioPrefixes[priority] ?: "$priority ", tag ?: "Tag", message)
}

logToFile("$priority ", tag ?: "Tag", message)
private fun skipLog(priority: Int): Boolean {
return if (vectorPreferences.labAllowedExtendedLogging()) {
false
} else {
priority < Log.ERROR
}
}

/**
@ -68,24 +99,6 @@ object VectorFileLogger : Timber.DebugTree() {
sCacheDirectory = cacheDir
}

/**
* Initialises the logger. Should be called AFTER [Log.setLogDirectory].
*
* @param fileName the base file name
*/
private fun init(fileName: String) {
try {
if (!TextUtils.isEmpty(fileName)) {
sFileName = fileName
}
sFileHandler = FileHandler(sCacheDirectory.absolutePath + "/" + sFileName + ".%g.txt", LOG_SIZE_BYTES, LOG_ROTATION_COUNT)
sFileHandler.formatter = LogFormatter()
sLogger.useParentHandlers = false
sLogger.level = Level.ALL
sLogger.addHandler(sFileHandler)
} catch (e: IOException) {
}
}

/**
* Adds our own log files to the provided list of files.
@ -99,8 +112,8 @@ object VectorFileLogger : Timber.DebugTree() {
try {
// reported by GA
if (null != sFileHandler) {
sFileHandler.flush()
val absPath = sCacheDirectory.absolutePath
sFileHandler!!.flush()
val absPath = sCacheDirectory?.absolutePath ?: return emptyList()

for (i in 0..LOG_ROTATION_COUNT) {
val filepath = "$absPath/$sFileName.$i.txt"
@ -111,7 +124,7 @@ object VectorFileLogger : Timber.DebugTree() {
}
}
} catch (e: Exception) {
Timber.e(e, "## addLogFiles() failed : " + e.message)
Timber.e(e, "## addLogFiles() failed : %s", e.message)
}

return files

View File

@ -149,6 +149,8 @@ class VectorPreferences @Inject constructor(private val context: Context) {
private const val SETTINGS_USE_NATIVE_CAMERA_PREFERENCE_KEY = "SETTINGS_USE_NATIVE_CAMERA_PREFERENCE_KEY"
private const val SETTINGS_ENABLE_SEND_VOICE_FEATURE_PREFERENCE_KEY = "SETTINGS_ENABLE_SEND_VOICE_FEATURE_PREFERENCE_KEY"

const val SETTINGS_LABS_ALLOW_EXTENDED_LOGS = "SETTINGS_LABS_ALLOW_EXTENDED_LOGS"

private const val SETTINGS_LABS_SHOW_HIDDEN_EVENTS_PREFERENCE_KEY = "SETTINGS_LABS_SHOW_HIDDEN_EVENTS_PREFERENCE_KEY"
private const val SETTINGS_LABS_ENABLE_SWIPE_TO_REPLY = "SETTINGS_LABS_ENABLE_SWIPE_TO_REPLY"

@ -257,6 +259,10 @@ class VectorPreferences @Inject constructor(private val context: Context) {
return defaultPrefs.getBoolean(SETTINGS_LABS_ENABLE_SWIPE_TO_REPLY, true)
}

fun labAllowedExtendedLogging(): Boolean {
return defaultPrefs.getBoolean(SETTINGS_LABS_ALLOW_EXTENDED_LOGS, false)
}

/**
* Tells if we have already asked the user to disable battery optimisations on android >= M devices.
*

View File

@ -2,5 +2,7 @@
<resources>

<!-- Strings not defined in Riot -->
<string name="labs_allow_extended_logging">Enable verbose logs.</string>
<string name="labs_allow_extended_logging_summary">Verbose logs will help developers by providing more logs when you send a RageShake. Even when enabled, the application does not log message contents or any other private data.</string>

</resources>

View File

@ -45,6 +45,13 @@
android:key="SETTINGS_LABS_ENABLE_SWIPE_TO_REPLY"
android:title="@string/labs_swipe_to_reply_in_timeline" />


<im.vector.riotx.core.preference.VectorSwitchPreference
android:defaultValue="false"
android:key="SETTINGS_LABS_ALLOW_EXTENDED_LOGS"
android:summary="@string/labs_allow_extended_logging_summary"
android:title="@string/labs_allow_extended_logging" />

<!--</im.vector.riotx.core.preference.VectorPreferenceCategory>-->

</androidx.preference.PreferenceScreen>