|
|
|
@ -17,7 +17,7 @@ object LogUtils { |
|
|
|
|
addFileHandler( |
|
|
|
|
this, |
|
|
|
|
Level.INFO, |
|
|
|
|
FileHelp.getCachePath() + File.separator + getCurrentDateStr(DATE_PATTERN) + ".log" |
|
|
|
|
FileHelp.getCachePath() |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -28,23 +28,20 @@ object LogUtils { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const val DATE_PATTERN_FULL = "yyyy-MM-dd HH:mm:ss" |
|
|
|
|
private const val DATE_PATTERN = "yyyy-MM-dd" |
|
|
|
|
const val TIME_PATTERN = "HH:mm:ss" |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 为log添加控制台handler |
|
|
|
|
* |
|
|
|
|
* @param log |
|
|
|
|
* 要添加handler的log |
|
|
|
|
* @param level |
|
|
|
|
* 控制台的输出等级 |
|
|
|
|
* @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) |
|
|
|
|
} |
|
|
|
@ -52,23 +49,21 @@ object LogUtils { |
|
|
|
|
/** |
|
|
|
|
* 为log添加文件输出Handler |
|
|
|
|
* |
|
|
|
|
* @param log |
|
|
|
|
* 要添加文件输出handler的log |
|
|
|
|
* @param level |
|
|
|
|
* log输出等级 |
|
|
|
|
* @param filePath |
|
|
|
|
* 指定文件全路径 |
|
|
|
|
* @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) |
|
|
|
|
fileHandler = |
|
|
|
|
FileHandler(filePath + File.separator + getCurrentDateStr(DATE_PATTERN) + ".log") |
|
|
|
|
// 设置输出文件的等级(如果FileHandler的等级高于或者等于log的level,则按照FileHandler的level输出到文件,如果低于,则按照Log等级输出) |
|
|
|
|
fileHandler.level = level |
|
|
|
|
fileHandler.formatter = object : Formatter() { |
|
|
|
|
override fun format(record: LogRecord): String { |
|
|
|
|
// 设置文件输出格式 |
|
|
|
|
return (getCurrentDateStr(DATE_PATTERN_FULL) + ":" + record.message + "\n") |
|
|
|
|
return (getCurrentDateStr(TIME_PATTERN) + ":" + record.message + "\n") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} catch (e: Exception) { |
|
|
|
@ -80,8 +75,6 @@ object LogUtils { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 获取当前时间 |
|
|
|
|
* |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
@SuppressLint("SimpleDateFormat") |
|
|
|
|
fun getCurrentDateStr(pattern: String): String { |
|
|
|
|