Android Logger
最近看了公司的Log工具类,觉得有点简单。决定自己实现一个,Logger, 一个可以控制Log输出不同场景的工具类
package com.android.chocolate.util
import android.util.Log
import java.lang.NullPointerException
object Logger {
private const val TAG = "Logger"
private const val isOpen = false
private var onlyLogCategory: Int? = null
private var isExpand = false
@Synchronized
fun setOnlyLogCategory(style: Style) {
onlyLogCategory = style.getMark()
}
@Synchronized
fun getOnlyLogCategory() = onlyLogCategory
@Synchronized
fun setExpand(status: Boolean) {
isExpand = status
}
@Synchronized
fun getExpand() = isExpand
fun i(tag: String, msg: String) {
if (!isOpen) {
return
}
if (onlyLogCategory != null) {
if (Style.INFO.getMark() > onlyLogCategory!!) {
if (!isExpand) {
return
}
} else if (Style.INFO.getMark() != onlyLogCategory) {
return
}
}
if (msg.isEmpty() || tag.isEmpty()) {
throw NullPointerException("i(tag: String, msg: String) Received Illegal arguments")
}
Log.i(tag, msg)
}
fun i(msg: String) {
i(TAG, msg)
}
fun d(tag: String, msg: String) {
if (!isOpen) {
return
}
if (onlyLogCategory != null) {
if (Style.DEBUG.getMark() > onlyLogCategory!!) {
if (!isExpand) {
return
}
} else if (Style.DEBUG.getMark() != onlyLogCategory) {
return
}
}
if (msg.isEmpty() || tag.isEmpty()) {
throw NullPointerException("d(tag: String, msg: String) Received Illegal arguments")
}
Log.d(tag, msg)
}
fun d(msg: String) {
d(TAG, msg)
}
fun e(tag: String, msg: String) {
if (!isOpen) {
return
}
if (onlyLogCategory != null) {
if (Style.ERROR.getMark() > onlyLogCategory!!) {
if (!isExpand) {
return
}
} else if (Style.ERROR.getMark() != onlyLogCategory) {
return
}
}
if (msg.isEmpty() || tag.isEmpty()) {
throw NullPointerException("e(tag: String, msg: String) Received Illegal arguments")
}
Log.e(tag, msg)
}
fun e(msg: String) {
e(TAG, msg)
}
enum class Style(private val mark: Int) {
VERBOSE(2),
DEBUG(3),
INFO(4),
WARN(5),
ERROR(6),
ASSERT(7);
fun getMark() = mark
}
}
|