This is one of the most responsible people I've ever seen writing a blog:
http://www.crifan.com/android_try_use_android_logging_log4j_to_output_log_to_sd_card_file/
Why do you say he is responsible??
Because the author writes out the whole process from determining the need to finding a solution, it will not only make the reader less detours, but also come clean the solution.
Of course there is some nonsense, the essence of it absorbed, nonsense not to say, on dry:
In Android, the implementation of the output log content in the SD card file, the practice is:
1. Download the log4j library for Android
Go: http://code.google.com/p/android-logging-log4j/
Download the corresponding Android-logging-log4j-1.0.3.jarand add it to the project.
2. Download the dependent Apache log4j Library
Go: http://logging.apache.org/log4j/1.2/download.html
Download the 1.2 series version of: Log4j-1.2.17.zip
Unzip to get Log4j-1.2.17.jar added to the project.
3. Write the Test code:
| 12345678910111213141516171819202122232425262728 |
importde.mindpipe.android.logging.log4j.LogConfigurator;importjava.io.File;importandroid.os.Environment;importorg.apache.log4j.Level;importorg.apache.log4j.Logger;public classBaseActivity extendsActivity { privateLogger gLogger; publicvoidconfigLog() { finalLogConfigurator logConfigurator = newLogConfigurator(); logConfigurator.setFileName(Environment.getExternalStorageDirectory() + File.separator + "crifanli_log4j.log"); // Set the root log level logConfigurator.setRootLevel(Level.DEBUG); // Set log level of a specific logger logConfigurator.setLevel("org.apache", Level.ERROR); logConfigurator.configure(); //gLogger = Logger.getLogger(this.getClass()); gLogger = Logger.getLogger("CrifanLiLog4jTest"); } @Override protectedvoid onCreate(Bundle savedInstanceState) { configLog(); gLogger.debug("test android log to file in sd card using log4j"); |
Can be achieved:
(1) The corresponding Crifanli_log4j.log file can be generated in the/mnt/sdcard
(2) The contents of the log output are debug and correspond to their own string identifiers Crifanlilog4jtest
Android normal log output to SD card