Date, log type, and tag are not complete when logging to FilePrinter
iadcialim opened this issue · 3 comments
iadcialim commented
elvishew commented
@iadcialim The ClassicFlattener
use a pattern of {d} {l}/{t}: {m}
, there is no line seperator between tag and message.
If you hope the border to be more pretty, you can use a PatternFlattener
with a pattern {d} {l}/{t}: \n{m}
instead of using a ClassicFlattener
.
iadcialim commented
hi @elvishew thanks. I went with your suggestion above.
Btw, I have this wrapper class
class MyLogger {
companion object {
fun print(tag: String, message: String) {
XLog.tag(tag)
.printers(AndroidPrinter(false), getFilePrinter())
.disableBorder()
.disableStackTrace()
.disableThreadInfo()
.i(message)
}
private fun getFilePrinter(flattener: PatternFlattener = ClassicFlattener()): FilePrinter {
val logDir =
File(ContextUtil.instance.getContext().filesDir?.absolutePath + File.separator + "logs")
// Log.d("getFilePrinter", "logs=${logDir.absolutePath}")
return FilePrinter.Builder(logDir.absolutePath) // Specify the directory path of log file(s)
.fileNameGenerator(MyLogFileNameGenerator("kaas")) // Default: ChangelessFileNameGenerator("log")
.backupStrategy(NeverBackupStrategy()) // Default: FileSizeBackupStrategy(1024 * 1024)
// .cleanStrategy(FileLastModifiedCleanStrategy(604800000)) // delete logs older than 1 week
.cleanStrategy(FileLastModifiedCleanStrategy(86400000)) // delete logs older than 1 day
.flattener(flattener) // Default: DefaultFlattener
.build()
}
}
}
I use the print
function when Im printing a formatted json string
message = "{"email":"xxx@company.com","password":"Password","companyId":"2123","mobileDeviceId":"234"}"
try {
// use to check if the message is a JSON string
// else it will throw JSONException
JSONObject(message)
val source: Buffer = Buffer().writeUtf8(message)
val reader: JsonReader = JsonReader.of(source)
val value: Any? = reader.readJsonValue()
val adapter: JsonAdapter<Any> =
Moshi.Builder().build().adapter(Any::class.java).indent(" ")
var result: String = adapter.toJson(value)
// result = result.replace("\t", "")
// put a newline before print json strings for pretty output
MyLogger.print("okhttp.OkHttpClient-Iad", "\n" + result)
} catch (e: JSONException) {
if (message.isNotEmpty()) {
MyLogger.print("okhttp.OkHttpClient-Iad", message)
}
}
The result looks good on the file:
But it has some extra spaces in logcat. Please see those in red
I wish for logcat for have the same display with the file.
elvishew commented
Seems it is the display style of Android Studio when displaying line-break, not a feature of xLog.