pull/36/head
kunfei 5 years ago
parent 0104a541e0
commit adc7867306
  1. 2
      app/src/main/java/io/legado/app/ui/config/ConfigFragment.kt
  2. 8
      app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt
  3. 70
      app/src/main/java/io/legado/app/utils/LogUtils.kt

@ -10,6 +10,7 @@ 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.getPrefString
@ -39,6 +40,7 @@ class ConfigFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChange
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
when (key) {
"downloadPath" -> BookHelp.upDownloadPath()
"recordLog" -> LogUtils.upLevel()
}
}

@ -10,6 +10,7 @@ import androidx.preference.PreferenceFragmentCompat
import io.legado.app.App
import io.legado.app.R
import io.legado.app.base.BaseFragment
import io.legado.app.help.BookHelp
import io.legado.app.help.permission.Permissions
import io.legado.app.help.permission.PermissionsCompat
import io.legado.app.help.storage.Backup
@ -21,6 +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.LogUtils
import io.legado.app.utils.startActivity
import kotlinx.android.synthetic.main.view_title_bar.*
import org.jetbrains.anko.startActivity
@ -82,9 +84,9 @@ class MyFragment : BaseFragment(R.layout.fragment_my_config) {
key: String?
) {
when (key) {
"isNightTheme" -> {
App.INSTANCE.applyDayNight()
}
"isNightTheme" -> App.INSTANCE.applyDayNight()
"recordLog" -> LogUtils.upLevel()
"downloadPath" -> BookHelp.upDownloadPath()
}
}

@ -11,66 +11,36 @@ import java.util.logging.Formatter
object LogUtils {
const val TIME_PATTERN = "yyyy-MM-dd HH:mm:ss"
private val logger: Logger by lazy {
val logger: Logger by lazy {
Logger.getGlobal().apply {
addFileHandler(
this,
Level.INFO,
FileHelp.getCachePath()
)
addHandler(fileHandler)
}
}
fun log(msg: String) {
if (App.INSTANCE.getPrefBoolean("recordLog")) {
logger.log(Level.INFO, msg)
}
}
private const val DATE_PATTERN = "yyyy-MM-dd"
const val TIME_PATTERN = "HH:mm:ss"
/**
* 为log添加控制台handler
*
* @param log 要添加handler的log
* @param level 控制台的输出等级
*/
fun addConsoleHandler(log: Logger, level: Level) {
// 控制台输出的handler
val consoleHandler = ConsoleHandler()
// 设置控制台输出的等级(如果ConsoleHandler的等级高于或者等于log的level,则按照FileHandler的level输出到控制台,如果低于,则按照Log等级输出)
consoleHandler.level = level
// 添加控制台的handler
log.addHandler(consoleHandler)
}
/**
* 为log添加文件输出Handler
*
* @param log 要添加文件输出handler的log
* @param level log输出等级
* @param filePath 指定文件全路径
*/
fun addFileHandler(log: Logger, level: Level, filePath: String) {
var fileHandler: FileHandler? = null
try {
fileHandler =
FileHandler(filePath + File.separator + getCurrentDateStr(DATE_PATTERN) + ".log")
// 设置输出文件的等级(如果FileHandler的等级高于或者等于log的level,则按照FileHandler的level输出到文件,如果低于,则按照Log等级输出)
fileHandler.level = level
fileHandler.formatter = object : Formatter() {
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")
return (getCurrentDateStr(TIME_PATTERN) + ": " + record.message + "\n")
}
}
} catch (e: Exception) {
e.printStackTrace()
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
}
// 添加输出文件handler
log.addHandler(fileHandler)
}
/**

Loading…
Cancel
Save