栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android 如何使用log4j及注意事项

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap

Android 如何使用log4j及注意事项

 Android 使用log4j

前言:

 如果要直接在android工程中使用log4j,是有点问题的,会报如下的错:

11-23 09:44:56.947: D/dalvikvm(1585): GC_FOR_MALLOC freed 3278 objects / 311568 bytes in 31ms 
rejecting opcode 0x21 at 0x000a 
rejected Lorg/apache/log4j/config/PropertySetter;.getPropertyDescriptor 
(Ljava/lang/String;)Ljava/beans/PropertyDescriptor; 
Verifier rejected class Lorg/apache/log4j/config/PropertySetter; 
Exception Ljava/lang/VerifyError; thrown during Lorg/apache/log4j/LogManager;. 
Shutting down VM 
threadid=1: thread exiting with uncaught exception (group=0x400259f8) 
FATAL EXCEPTION: main 
java.lang.ExceptionInInitializerError 
at org.slf4j.impl.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:64) 
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:253) 
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:265) 
... 
Caused by: java.lang.VerifyError: org.apache.log4j.config.PropertySetter 
at org.apache.log4j.PropertyConfigurator.parseAppender(PropertyConfigurator.java:772) 
at org.apache.log4j.PropertyConfigurator.parseCategory(PropertyConfigurator.java:735) 
at org.apache.log4j.PropertyConfigurator.configureRootCategory(PropertyConfigurator.java:615) 
at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:502) 
at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:547) 
at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:483) 
at org.apache.log4j.LogManager.(LogManager.java:127) 
... 20 more 

因此,可以另外多下载一个叫android-logging-log4j的项目,地址在:

http://code.google.com/p/android-logging-log4j/downloads/list,注意,原本的log4j还是需要的。

  在AndroidManifest.xml中,增加如下设置:

 

   然后在程序中如下使用:  

package com.android.myapp; 
  
. 
import java.io.File; 
  
 
import org.apache.log4j.Level; 
  
 
import org.apache.log4j.Logger; 
. 
import android.app.Application; 
  
 
import android.os.Environment; 
  
 
import de.mindpipe.android.logging.log4j.LogConfigurator; 
 . 
  
public class MyApplication extends Application { 
  
 
    @Override 
  
 
    public void onCreate() { 
  
 
 super.onCreate(); 
  
 
 LogConfigurator logConfigurator = new LogConfigurator(); 
  
 
 logConfigurator.setFileName(Environment.getExternalStorageDirectory() 
  
 
  + File.separator + "MyApp" + File.separator + "logs" 
  
 
  + File.separator + "log4j.txt"); 
  
 
 logConfigurator.setRootLevel(Level.DEBUG); 
  
 
 logConfigurator.setLevel("org.apache", Level.ERROR); 
  
 
 logConfigurator.setFilePattern("%d %-5p [%c{2}]-[%L] %m%n"); 
  
 
 logConfigurator.setMaxFileSize(1024 * 1024 * 5); 
  
 
 logConfigurator.setImmediateFlush(true); 
  
 
 logConfigurator.configure(); 
  
 
 Logger log = Logger.getLogger(MyApplication.class); 
  
 
 log.info("My Application Created"); 
  
 
    } 
  
 
} 

     现在日志则是以:

Environment.getExternalStorageDirectory() + File.separator + "MyApp" + File.separator + "logs" + File.separator + "log4j.txt
   的方式保存了。其他和log4j的用法差不多了。

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/160031.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号