Android 中的log4j使用

在前一篇文章中讲到了log4j 2.x版本的使用,一般来说,log4j都是使用在java开发中,安卓需要中无法直接使用log4j,需要其他的配置才能使用。要在Android开发中使用log4j,需要配置几个jar包。我使用的包分别为1)log4j-1.2.17.jar,2)android-logging-log4j-1.0.3.jar,3)slf4j-api-1.7.6.jar。其中android-logging-log4j-1.0.3.jar是专门开发来使log4j能够用在Android中的。下载这三个包之后,配置好工程中的路径,在工程下添加一个logback.xml文件,文件内容如下:


<configuration debug="true"> 

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> 
    <!-- encoders are  by default assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="debug">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>


然后需要一个配置类,配置类是在最初运行时配置log的一些属性,比如log文件的名字,log的格式,log的存放位置等等。我们把配置类设计成单实例类,命名为ConfigureLog4J,在我的工程中其代码如下:

import java.io.File;

import org.apache.log4j.*;
import org.apache.log4j.LogManager;

import android.os.Environment;
import de.mindpipe.android.logging.log4j.LogConfigurator;



public enum ConfigureLog4J {
	
	INSTANCE;
	
	private LogConfigurator log_config = null;
	
	private final String directory = Environment.getExternalStorageDirectory() + File.separator + "/single_execution";
	private final String file_name = this.directory + "/execution.txt";
	
    public void configure() 
    {
    	if (this.log_config == null)
    	{
	        log_config = new LogConfigurator();
	        
	        log_config.setFileName(file_name);
	        log_config.setMaxFileSize(50 * 1024 * 1024);	// 50MB
	        log_config.setRootLevel(Level.DEBUG);
	        log_config.setFilePattern("%d{HH:mm:ss.S} %-5l - %m%n");
	        log_config.setUseLogCatAppender(false);
	        
	        
	        log_config.setImmediateFlush(false);
	        
	        // Set log level of a specific logger
	        log_config.setLevel("ics.mobilememo", Level.DEBUG);
	        
	        log_config.configure();
    	}
    }
    
    /**
     * @return {@link #directory}: directory containing all the execution-related files
     */
    public String getDirectory()
    {
    	return this.directory;
    }
    
    /**
     * @return {@link #file_name}: name of file in which the logs are stored
     */
    public String getFileName()
    {
    	return this.file_name;
    }
    
    /**
     * Shut down the logger and let all buffered logs get flushed.
     * See http://stackoverflow.com/a/3078377/1833118
     */
    public void shutdown()
    {
    	LogManager.shutdown();
    }
}
shutdown()是log结束时必须要调用的,否则log不会写到文件中,文件写失败。

写好这些,就可以开始使用了,我写了一个BatteryActivity的安卓工程来测试,

private final Logger log4android = Logger.getLogger(BatteryActivity.class);

在运行初始时,调用
ConfigureLog4J.INSTANCE.configure();
然后就可以使用log4j了。像这样:

log4android.debug("hello world");
在Activity的onDestroy()中调用

ConfigureLog4J.INSTANCE.shutdown();
当应用运行结束,被销毁时,就可以得到一个log文件,log文件的位置在ConfigureLog4J类的配置所指明的位置。打开log文件就可以看到写入的log。







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值