Android 如何使用log4j及注意事项
程序员文章站
2023-12-20 23:37:04
android 使用log4j
前言:
如果要直接在android工程中使用log4j,是有点问题的,会报如下的错:
11-23...
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中,增加如下设置:
<uses-permission android:name="android.permission.write_external_storage" />
然后在程序中如下使用:
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的用法差不多了。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!