The log4j log file in Android requires two packages, we do not need to configure the configuration file, everything is done in the code.
log4j Package Download:
: http://logging.apache.org/log4j/1.2/download.html
Android-logging-log4j-1.0.3.jar Download:
Https://code.google.com/archive/p/android-logging-log4j/downloads
After all download is complete, the eclipse in the Libs import package to add drinking, Android studio also put the package into Libs right click on the package add as Librarys, and then add in Gradle
sourcesets{
Main () {
Jnilibs.srcdirs = [' Libs ']
}
}
can be used.
Create a new configurelog4j file and fill in the following code: comments in the Code
/*Copyright Rolf Kulemann Licensed under the Apache License, Version 2.0 (the "License"); You are not a use of this file except in compliance with the License. Obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0unless required by applicable law or agreed to writing, software distributed under the License is distributed On a "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. */ Packagetest.myc.minyuchun.log4jtest;Importandroid.os.Environment;ImportOrg.apache.log4j.Level;ImportJava.io.File;Importjava.util.Date;ImportDe.mindpipe.android.logging.log4j.LogConfigurator;/*** Log Settings*/ Public classconfigurelog4j {//log level priority from high to Low: off (off), FATAL (fatal), error (Error), WARN (warning), info (info), debug (Debug), all (open all logs, my understanding and debug level seems to be no different) //LOG4J recommends using only the five levels of fatal, ERROR, WARN, INFO, Debug. //"Yyyy-mm-dd");//output format of the log Public Static voidConfigure () {FinalLogconfigurator Logconfigurator =NewLogconfigurator (); Date Nowtime=NewDate (); //String needwritemessage = Mylogsdf.format (nowtime); //log file path address: SD card under Myc folder log folder test fileString FileName =environment.getexternalstoragedirectory ()+ File.separator + "Myc" + file.separator + "Log" + File.separator + "Test.log"; //Set file nameLogconfigurator.setfilename (fileName); //setting the root log output level defaults to debugLogconfigurator.setrootlevel (Level.debug); //setting the log output levelLogconfigurator.setlevel ("Org.apache", Level.info); //set text format output to log file default%d%-5p [%c{2}]-[%l]%m%nLogconfigurator.setfilepattern ("%d%-5p [%c{2}]-[%l]%m%n"); //set the text format output to console default%m%nLogconfigurator.setlogcatpattern ("%m%n"); //Set Total file sizeLogconfigurator.setmaxfilesize (1024 * 1024 * 5); //set the maximum number of files to generateLogconfigurator.setmaxbackupsize (1); //sets whether all messages are immediately output by default to True,false not outputLogconfigurator.setimmediateflush (true); //whether the local console printout defaults to True, False does not outputLogconfigurator.setuselogcatappender (true); //sets whether to enable file attach, which defaults to true. False to overwrite fileLogconfigurator.setusefileappender (true); //set whether to reset the configuration file by default to TrueLogconfigurator.setresetconfiguration (true); //whether to display the internal initialization log, false by defaultLogconfigurator.setinternaldebugging (false); Logconfigurator.configure (); }}
Use the following code in the main acrivity
// Load Configuration configurelog4j configurelog4j=New configurelog4j (); Configurelog4j.configure (); // Initialize log Logger Log=logger.getlogger (this. GetClass ()); // Write info log Log.info ("Do not know AH is testing ah");
Use of Android log4j log management