Log4a is a mMAP-based, high-performance, highly available Android log collection framework
Copy the code
WHY:
Log4a uses mMAP file mapped memory as the cache to maximize log integrity without sacrificing performance. The log is first written to the mMAP file mapped memory. Based on the mMAP feature, even if the user forcibly kills the process, the log file will not be lost and will be written back to the log file the next time Log4a is initialized.
For mobile developers, analyzing logs is sometimes a necessary solution to online problems that users report are difficult to reproduce. However, there has always been a pain point in log collection that performance and log integrity are not compatible. High-performance log collection requires a lot of memory. Logs are written to memory and then written to the file system when appropriate (Flush). If the user forces the process to kill before flush, the memory is lost. Writing logs to files in real time ensures log integrity. However, writing logs to files is an I/O operation, which involves switching between user mode and kernel mode. Writing files to memory takes more time than writing files to memory.
HOW:
The usage method is the same as android.util.Log, but the difference is that you need to configure it simply. Of course, there are also plenty of interfaces reserved for further use.
- Add dependencies to build.gradle file:
allprojects { repositories { maven { url 'https://jitpack.io' } } } dependencies { compile 'com. Making. Pqpo: Log4a: v1.0.0'}Copy the code
- Set and initialize Log4a:
AndroidAppender.Builder androidBuild = new AndroidAppender.Builder();
File log = FileUtils.getLogDir(context);
String log_path = log.getAbsolutePath() + File.separator + "log.txt";
FileAppender.Builder fileBuild = new FileAppender.Builder(context)
.setLogFilePath(log_path);
Logger logger = new Logger.Builder()
.enableAndroidAppender(androidBuild)
.enableFileAppender(fileBuild)
.create();
Log4a.setLogger(logger);Copy the code
- Use the same as android.util.log:
Log4a i. (TAG, "Hello, Log4a!" );Copy the code
- Flush the cache or free memory at the appropriate time
// Flush the cache log4a.flush (); Log4a.release(); log4a.release (); log4a.release ();Copy the code
The performance test
The performance test code is found in Sample, which tests Log4a and android.util.Log respectively, writes directly to memory (saves Log content to ArrayList), writes files in real time, uses Buffer to write files, you can also download Sample APK by yourself. Test it on your own device.
Write 1W logs on Google Pixel and Moto X respectively.
Google Pixel | Moto X |
---|---|
The test data of Google Pixel is shown in the following table (in order of elapsed time) :
equipment | Test type | Elapsed time | Log integrity | Persistent or not | Verify that logs are complete after a power failure |
---|---|---|---|---|---|
Google Pixel | Mem | 13ms | Y | N | N |
Google Pixel | Log4a | 50ms | Y | Y | Y |
Google Pixel | File with Buffer | 61ms | Y | Y | N |
Google Pixel | Android Log | 184ms | N | N | N |
Google Pixel | File no Buffer | 272ms | Y | Y | Y |
As you can see, Log4a’s log writing performance is second only to that of direct memory writing, and is basically the same as that of BufferOutputStream writing. In fact, to ensure multithreaded security, Log4a locks mMAP memory writing. You can get closer to the speed of writing directly to memory without locking (test yourself if you are interested). BufferOutputStream is a BufferOutputStream that stores data in memory before flushing it to a file. If the power fails or the process is forcibly killed before flushing, the data in memory will be lost irretrievably. Log4a will recover the log file during the next startup to ensure log integrity.
Thank you
- Tencent/mars
- XLog
About me:
- Email address: [email protected]
- Making: pqpo
- Blog: PQPO’s Notes
- Twitter: Pqponet
- Wechat official account: PqPO_me (Scan the QR code below)
License
Copyright 2017 pqpo
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Copy the code