From 016e98f4855ca322c38090c5d6002cc311f66622 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 29 Sep 2019 18:02:33 +0800 Subject: [PATCH] up --- app/build.gradle | 3 + app/src/main/java/io/legado/app/App.kt | 10 ++++ .../app/receiver/MediaButtonReceiver.kt | 5 +- .../io/legado/app/ui/config/ConfigFragment.kt | 5 +- .../io/legado/app/ui/main/my/MyFragment.kt | 5 +- .../main/java/io/legado/app/utils/LogUtils.kt | 55 ------------------- 6 files changed, 21 insertions(+), 62 deletions(-) delete mode 100644 app/src/main/java/io/legado/app/utils/LogUtils.kt diff --git a/app/build.gradle b/app/build.gradle index 22922cbbe..97a5a9dc0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -148,4 +148,7 @@ dependencies { //颜色选择 implementation 'com.jaredrummler:colorpicker:1.1.0' + //日志记录 + implementation 'com.apkfuns.logutils:library:1.7.5' + implementation 'com.apkfuns.log2file:log2file:1.3.1' } diff --git a/app/src/main/java/io/legado/app/App.kt b/app/src/main/java/io/legado/app/App.kt index 54238a6ad..ea8a30f66 100644 --- a/app/src/main/java/io/legado/app/App.kt +++ b/app/src/main/java/io/legado/app/App.kt @@ -9,6 +9,8 @@ import android.os.Build import android.os.Bundle import androidx.annotation.RequiresApi import androidx.appcompat.app.AppCompatDelegate +import com.apkfuns.log2file.LogFileEngineFactory +import com.apkfuns.logutils.LogUtils import com.jeremyliao.liveeventbus.LiveEventBus import io.legado.app.constant.AppConst.channelIdDownload import io.legado.app.constant.AppConst.channelIdReadAloud @@ -16,12 +18,15 @@ import io.legado.app.constant.AppConst.channelIdWeb import io.legado.app.data.AppDatabase import io.legado.app.help.ActivityHelp import io.legado.app.help.CrashHandler +import io.legado.app.help.FileHelp import io.legado.app.help.ReadBookConfig import io.legado.app.lib.theme.ThemeStore import io.legado.app.ui.widget.page.ChapterProvider import io.legado.app.utils.getCompatColor +import io.legado.app.utils.getPrefBoolean import io.legado.app.utils.getPrefInt import io.legado.app.utils.isNightTheme +import java.io.File @Suppress("DEPRECATION") class App : Application() { @@ -43,6 +48,11 @@ class App : Application() { super.onCreate() INSTANCE = this CrashHandler().init(this) + LogUtils.getLog2FileConfig() + .configLog2FileEnable(getPrefBoolean("recordLog")) + .configLog2FilePath(FileHelp.getCachePath() + File.separator + "logs" + File.separator) + .configLog2FileNameFormat("%d{yyyyMMdd}.log") + .configLogFileEngine(LogFileEngineFactory(this)) db = AppDatabase.createDatabase(INSTANCE) packageManager.getPackageInfo(packageName, 0)?.let { versionCode = it.versionCode diff --git a/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt b/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt index 1dfa59fcc..45565c31c 100644 --- a/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt +++ b/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt @@ -4,12 +4,11 @@ import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.view.KeyEvent +import com.apkfuns.logutils.LogUtils import io.legado.app.constant.Bus import io.legado.app.help.ActivityHelp import io.legado.app.ui.book.read.ReadBookActivity -import io.legado.app.utils.LogUtils.MyLogger import io.legado.app.utils.postEvent -import java.util.logging.Level /** @@ -24,7 +23,7 @@ class MediaButtonReceiver : BroadcastReceiver() { fun handleIntent(context: Context, intent: Intent): Boolean { val intentAction = intent.action val keyEventAction = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT)?.action - MyLogger.log(Level.INFO, "耳机按键: $intentAction $keyEventAction") + LogUtils.d("耳机按键", "$intentAction $keyEventAction") if (Intent.ACTION_MEDIA_BUTTON == intentAction) { if (keyEventAction == KeyEvent.ACTION_DOWN) { readAloud(context) diff --git a/app/src/main/java/io/legado/app/ui/config/ConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/ConfigFragment.kt index fd957e433..c111b9401 100644 --- a/app/src/main/java/io/legado/app/ui/config/ConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/ConfigFragment.kt @@ -6,11 +6,12 @@ import android.view.View import androidx.preference.ListPreference import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat +import com.apkfuns.logutils.LogUtils import io.legado.app.App import io.legado.app.R import io.legado.app.help.BookHelp import io.legado.app.lib.theme.ATH -import io.legado.app.utils.LogUtils +import io.legado.app.utils.getPrefBoolean import io.legado.app.utils.getPrefString @@ -40,7 +41,7 @@ class ConfigFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChange override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) { when (key) { "downloadPath" -> BookHelp.upDownloadPath() - "recordLog" -> LogUtils.upLevel() + "recordLog" -> LogUtils.getLog2FileConfig().configLog2FileEnable(getPrefBoolean("recordLog")) } } diff --git a/app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt b/app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt index af839204e..350737281 100644 --- a/app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt @@ -7,6 +7,7 @@ import android.view.MenuItem import android.view.View import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat +import com.apkfuns.logutils.LogUtils import io.legado.app.App import io.legado.app.R import io.legado.app.base.BaseFragment @@ -22,7 +23,7 @@ import io.legado.app.ui.book.source.manage.BookSourceActivity import io.legado.app.ui.config.ConfigActivity import io.legado.app.ui.config.ConfigViewModel import io.legado.app.ui.replacerule.ReplaceRuleActivity -import io.legado.app.utils.LogUtils +import io.legado.app.utils.getPrefBoolean import io.legado.app.utils.startActivity import kotlinx.android.synthetic.main.view_title_bar.* import org.jetbrains.anko.startActivity @@ -85,7 +86,7 @@ class MyFragment : BaseFragment(R.layout.fragment_my_config) { ) { when (key) { "isNightTheme" -> App.INSTANCE.applyDayNight() - "recordLog" -> LogUtils.upLevel() + "recordLog" -> LogUtils.getLog2FileConfig().configLog2FileEnable(getPrefBoolean("recordLog")) "downloadPath" -> BookHelp.upDownloadPath() } } diff --git a/app/src/main/java/io/legado/app/utils/LogUtils.kt b/app/src/main/java/io/legado/app/utils/LogUtils.kt deleted file mode 100644 index 82b2c8cc4..000000000 --- a/app/src/main/java/io/legado/app/utils/LogUtils.kt +++ /dev/null @@ -1,55 +0,0 @@ -package io.legado.app.utils - -import android.annotation.SuppressLint -import io.legado.app.App -import io.legado.app.help.FileHelp -import java.io.File -import java.text.SimpleDateFormat -import java.util.* -import java.util.logging.* -import java.util.logging.Formatter - - -object LogUtils { - const val TIME_PATTERN = "yyyy-MM-dd HH:mm:ss" - - val MyLogger: Logger by lazy { - Logger.getGlobal().apply { - addHandler(fileHandler) - } - } - - private val fileHandler by lazy { - FileHandler(FileHelp.getCachePath() + File.separator + "app.log", 1024, 2).apply { - formatter = object : Formatter() { - override fun format(record: LogRecord): String { - // 设置文件输出格式 - return (getCurrentDateStr(TIME_PATTERN) + ": " + record.message + "\n") - } - } - level = if (App.INSTANCE.getPrefBoolean("recordLog")) { - Level.INFO - } else { - Level.OFF - } - } - } - - fun upLevel() { - fileHandler.level = if (App.INSTANCE.getPrefBoolean("recordLog")) { - Level.INFO - } else { - Level.OFF - } - } - - /** - * 获取当前时间 - */ - @SuppressLint("SimpleDateFormat") - fun getCurrentDateStr(pattern: String): String { - val date = Date() - val sdf = SimpleDateFormat(pattern) - return sdf.format(date) - } -} \ No newline at end of file