Recently I learned about Mars, the open source framework of wechat, and I felt quite interesting, so I wrote a Demo to try it. This post is a record post (the legend of learning how to use xLog in ten minutes). This article mainly uses Kotlin language to achieve, but Java friends should not be difficult to understand, the overall use is similar

What is XLog

First of all, I will briefly introduce XLog. As a part of Mars, the open source framework of wechat, XLog can achieve high performance and high compression to write logs, so as to avoid delays and other effects on performance when writing logs.

Configuration depends on

Add dependencies. Version 1.2.3 is used instead of 1.2.4, but the overall difference is not that big, just the use of config

Implementation 'com. Tencent. Mars: Mars - xlog: 1.2.3'Copy the code

permissions

To require write permission, add the following to AndroidManifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Copy the code

So file loading

Be sure to include the following in the initialization code (Kotlin voluntarily removed the semicolon)

System.loadLibrary("stlport_shared");
System.loadLibrary("marsxlog");
Copy the code

Libstlport_shared. so” error “: xlog file is stored under arm64-V8A and armeabi-v7A. Add the following code to Gradle android to solve the problem. If the problem is not solved, use the real machine to debug.

ndk {
abiFilters "armeabi-v7a", "arm64-v8a"
    }
Copy the code

Initialize the

System.loadlibrary (XXX) is a necessary statement to load two so files. LEVEL can be set to DEBUG or ALL to print different logs, respectively. Because if it is empty, it is easy to cause the SIGBUS signal error.

// in version 1.2.3 system.loadlibrary ("c++_shared") system.loadlibrary ("marsxlog") val SDCARD = getExternalFilesDir("XLogF").toString() val logPath = SDCARD+"/Xlog" // this is necessary, or may crash for SIGBUS val cachePath = this.getCacheDir().toString() + "/xlog" if (BuildConfig.DEBUG) { Xlog.appenderOpen(Xlog.LEVEL_DEBUG, Xlog.AppednerModeAsync, cachePath, logPath, "MarsSample", 0, ""); Xlog.setConsoleLogOpen(true); } else { Xlog.appenderOpen(Xlog.LEVEL_INFO, Xlog.AppednerModeAsync, cachePath, logPath, "MarsSample", 0, ""); Xlog.setConsoleLogOpen(false); } Log.setLogImp(Xlog())Copy the code

Close the XLog

In addition to the use of XLog, of course, also need to close, both of the two can not be missing either part, when XLog is closed, the corresponding log file will be generated. Add the following statement at the end of log writing

com.tencent.mars.xlog.Log.appenderClose()
Copy the code

At this time, we can find the corresponding xlog log file under the corresponding Path, but the current log file still cannot be opened directly, we need to use the corresponding program to decode and generate it.

The log generated

Xlog_log_file. py from log/crypt/ in Mars source code. Put xlog in the same folder as xlog. We CMD to the corresponding directory, execute the following statement to generate the log file

Python decode_mars_nocrypt_log_file.py file name.xlogCopy the code

After execution, a log file is generated, which can be opened using Notepad or other programs to view the log. Note that XLog only records logs from Mars, not util, so if we need to write logs to the Log, we need to be aware of which library we are using (I won’t bother to post the Log content).

conclusion

It took a long time to access this component in the project. Generally speaking, I have a deeper understanding of the access and operation of the component. Let’s write an article to record the pit we stepped on before and help us solve the problem.