From 1ce32423a90367c3a9f50f2f4dda1f2545264762 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 29 Sep 2019 18:45:43 +0800 Subject: [PATCH] up --- app/build.gradle | 3 - app/src/main/java/io/legado/app/App.kt | 10 --- .../app/receiver/MediaButtonReceiver.kt | 2 +- .../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 | 63 +++++++++++++++++++ 6 files changed, 68 insertions(+), 20 deletions(-) create mode 100644 app/src/main/java/io/legado/app/utils/LogUtils.kt diff --git a/app/build.gradle b/app/build.gradle index 97a5a9dc0..22922cbbe 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -148,7 +148,4 @@ 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 ea8a30f66..54238a6ad 100644 --- a/app/src/main/java/io/legado/app/App.kt +++ b/app/src/main/java/io/legado/app/App.kt @@ -9,8 +9,6 @@ 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 @@ -18,15 +16,12 @@ 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() { @@ -48,11 +43,6 @@ 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 45565c31c..ce3908021 100644 --- a/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt +++ b/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt @@ -4,10 +4,10 @@ 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 import io.legado.app.utils.postEvent 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 c111b9401..fd957e433 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,12 +6,11 @@ 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.getPrefBoolean +import io.legado.app.utils.LogUtils import io.legado.app.utils.getPrefString @@ -41,7 +40,7 @@ class ConfigFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChange override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) { when (key) { "downloadPath" -> BookHelp.upDownloadPath() - "recordLog" -> LogUtils.getLog2FileConfig().configLog2FileEnable(getPrefBoolean("recordLog")) + "recordLog" -> LogUtils.upLevel() } } 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 350737281..af839204e 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,7 +7,6 @@ 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 @@ -23,7 +22,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.getPrefBoolean +import io.legado.app.utils.LogUtils import io.legado.app.utils.startActivity import kotlinx.android.synthetic.main.view_title_bar.* import org.jetbrains.anko.startActivity @@ -86,7 +85,7 @@ class MyFragment : BaseFragment(R.layout.fragment_my_config) { ) { when (key) { "isNightTheme" -> App.INSTANCE.applyDayNight() - "recordLog" -> LogUtils.getLog2FileConfig().configLog2FileEnable(getPrefBoolean("recordLog")) + "recordLog" -> LogUtils.upLevel() "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 new file mode 100644 index 000000000..27c3015fe --- /dev/null +++ b/app/src/main/java/io/legado/app/utils/LogUtils.kt @@ -0,0 +1,63 @@ +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" + + fun d(tag: String, msg: String) { + logger.log(Level.INFO, "$tag $msg") + } + + fun e(tag: String, msg: String) { + logger.log(Level.WARNING, "$tag $msg") + } + + private val logger: 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