elvishew/xLog

Date, log type, and tag are not complete when logging to FilePrinter

iadcialim opened this issue · 3 comments

This is the log in logcat (AndroidPrinter)
Screenshot 2021-07-04 at 8 56 16 PM

This is the log file (FilePrinter)
Screenshot 2021-07-04 at 8 56 02 PM

The differences are underlined in red.
It's missing that infos when logging to file which kinda mess up the formatting.
Any way possible to make it the same with logcat?

@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.

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:
Screenshot 2021-07-26 at 5 49 27 PM

But it has some extra spaces in logcat. Please see those in red
Screenshot 2021-07-26 at 5 49 16 PM

I wish for logcat for have the same display with the file.

Seems it is the display style of Android Studio when displaying line-break, not a feature of xLog.