Download the log4j-1.2.17.jar and android-logging-log4j-1.0.3.jar from the links given in the references. (the version number could vary in the file name, as of this writing these are the files i was able to download)
After this added the gradle build file using Open Module Settings -> Dependencies -> File Dependancies
The build.gradle file looked like this
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:22.1.1'
compile 'com.android.support:support-v4:22.1.1'
compile 'com.google.android.gms:play-services:7.3.0'
compile files('libs/android-logging-log4j-1.0.3.jar')
compile files('libs/log4j-1.2.17.jar')
}
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:22.1.1'
compile 'com.android.support:support-v4:22.1.1'
compile 'com.google.android.gms:play-services:7.3.0'
compile files('libs/android-logging-log4j-1.0.3.jar')
compile files('libs/log4j-1.2.17.jar')
}
Now added the Log4jHelper class like this below.
package mypackage;
import android.os.Environment;
import de.mindpipe.android.logging.log4j.LogConfigurator;
public class Log4jHelper {
private final static LogConfigurator mLogConfigrator = new LogConfigurator();
static {
configureLog4j();
}
private static void configureLog4j() {
String fileName = Environment.getExternalStorageDirectory() + "/" + "log4j.log";
String filePattern = "%d - [%c] - %p : %m%n";
int maxBackupSize = 10;
long maxFileSize = 1024 * 1024;
configure( fileName, filePattern, maxBackupSize, maxFileSize );
}
private static void configure( String fileName, String filePattern, int maxBackupSize, long maxFileSize ) {
mLogConfigrator.setFileName( fileName );
mLogConfigrator.setMaxFileSize( maxFileSize );
mLogConfigrator.setFilePattern(filePattern);
mLogConfigrator.setMaxBackupSize(maxBackupSize);
mLogConfigrator.setUseLogCatAppender(true);
mLogConfigrator.configure();
}
public static org.apache.log4j.Logger getLogger( String name ) {
org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger( name );
return logger;
}
}
import android.os.Environment;
import de.mindpipe.android.logging.log4j.LogConfigurator;
public class Log4jHelper {
private final static LogConfigurator mLogConfigrator = new LogConfigurator();
static {
configureLog4j();
}
private static void configureLog4j() {
String fileName = Environment.getExternalStorageDirectory() + "/" + "log4j.log";
String filePattern = "%d - [%c] - %p : %m%n";
int maxBackupSize = 10;
long maxFileSize = 1024 * 1024;
configure( fileName, filePattern, maxBackupSize, maxFileSize );
}
private static void configure( String fileName, String filePattern, int maxBackupSize, long maxFileSize ) {
mLogConfigrator.setFileName( fileName );
mLogConfigrator.setMaxFileSize( maxFileSize );
mLogConfigrator.setFilePattern(filePattern);
mLogConfigrator.setMaxBackupSize(maxBackupSize);
mLogConfigrator.setUseLogCatAppender(true);
mLogConfigrator.configure();
}
public static org.apache.log4j.Logger getLogger( String name ) {
org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger( name );
return logger;
}
}
From the application code, call the below,
org.apache.log4j.Logger logger= Log4jHelper.getLogger("MainActivity");
logger.info("This is a sample message as info");
logger.trace("This is a sample message as trace");
logger.warn("This is a sample message as warn");
logger.info("This is a sample message as info");
logger.trace("This is a sample message as trace");
logger.warn("This is a sample message as warn");
Thats it! . i was able to see the log4j.log file under storage in sdcard.
References:
No comments:
Post a Comment