【转】Android使用Log4j例子
Step 1
Download Log4J library from http://logging.apache.org/log4j/1.2/download.html
Step 2
Configuring Log4J library the normal way - using XML configuration file - can't be used in Android based Java application as the configuration classes of Log4J use beans from the package "java.beans" (e.g. PropertyDescriptor). Not all classes of this package are supported in Android, so using Log4J directly in Android Java application will case exceptions like the following one to be thrown:
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
There is a project called android-logging-log4j, which provides a convenient way to configure the log4j system properly.
Download "Android Logging Log4J" library fromhttp://code.google.com/p/android-logging-log4j/downloads/list
Step 3
Add Both libraries "log4j" and "android-logging-log4j" to your application libraries
Step 4
In order to log to a file on the external storage, the following permission needs to be placed in AndroidManifest.xml
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Step 5
In your application Main class:
- 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 + "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");
- }
- }
Now you will have log4j configured to log to Path: (Environment.getExternalStorageDirectory() + File.separator + "MyApp" + File.separator + "logs" + File.separator + "log4j.txt") with DEBUG level and ERROR lever for "org.apache" package with file pattern "%d %-5p [%c{2}]-[%L] %m%n" and other configuration parameters.
6. 程序中使用的实例。
import org.apache.log4j.Logger;
public class ExampleLog4J{
privatefinalLogger log =Logger.getLogger(ExampleLog4J.class);
publicvoid myMethod(){
log.info("This message should be seen in log file and logcat");
}
}
【转】Android使用Log4j例子的更多相关文章
- [Android Pro] 完美Android Cursor使用例子(Android数据库操作)
reference to : http://www.ablanxue.com/prone_10575_1.html 完美 Android Cursor使用例子(Android数据库操作),Androi ...
- Android MediaCodec 使用例子
Android MediaCodec 使用例子 下面的例子是使用MediaCodec 录制到文件的例子. 1 public class AvcEncoder { private MediaCodec ...
- 我的Android进阶之旅------>Android拍照小例子
今天简单的学习了一下android拍照的简单实现. 当然该程序是个小例子,非常简单,没有什么复杂的操作,但是可以学习到Android 拍照API流程. 1.在布局文件中添加一个 surfaceView ...
- Android中log4j的运用
网上一查关于android上面运用Log4j的运用,各种说需要添加多样的包的,照着log4j的官网教程看了下,给了个简单的输出到console上面的代码,似乎没什么用.看网上关于Log4j更多是在ja ...
- android highcharts 柱状图例子
android提供achartengine api 只能做简单的,如果是复杂的图表,个人的想法结合highcharts来完成:减小工作量,官方提供的例子也非常丰富. 通过android webview ...
- 编译android framework的例子【转】
本文转载自:http://blog.csdn.net/brucexu1978/article/details/7610358 在开发过程中,尤其是Framework相关开发时,有时候需要重新编译资源文 ...
- Android test---robotium----简单例子
1.首先新建一个要被测试的工程,命名为”robotium“:一个很简单的Android 应用程序:主页面只有个 TextView 控件: 2. 在建一个用于测试的工程 ,命名为”robotiumTes ...
- Android密码约束规则例子一
Android常用的一个密码规则 (一)密码必须是8至16位:(二)密码必须包含英文字母和数字:(三)密码不能包含4位连续相同的字符,如0000或AAAA:(四)密码不能包含4位连续递增或连续递减的数 ...
- PhoneGap: Android平台入门例子(Hello World)
Hello World Demo: http://docs.phonegap.com/en/2.0.0/guide_getting-started_android_index.md.html#Gett ...
随机推荐
- Cocoapods的安装与使用
一.安装 1.CocoaPods是用Ruby实现的,要想使用它首先需要有Ruby的环境.OS X系统默认已经可以运行Ruby了,因此我们只需执行以下命令: sudo gem install cocoa ...
- Autolayout-VFL语言添加约束
一.VFL语言简洁 VFL(Visual format language)语言是苹果为了简化手写Autolayout代码所创建的专门负责编写约束的代码.为我们简化了许多代码量. 二.使用步骤 使用步骤 ...
- 银行支票和汇票中使用的专用字体MICR E13B条形码控件字体
MICR E13B条形码控件字体是一种在美国.加拿大.波多黎各.巴拿马.英国和其它少数国家的银行支票和汇票中使用的专用字体,主要用来打印适用于磁性和光学字符识别系统的MICR字符.MICR E13B条 ...
- mybatis ForEach Collection集合等规范解析(转)
转自:http://blog.csdn.net/wj3319/article/details/9025349 在SQL开发过程中,动态构建In集合条件查询是比较常见的用法,在Mybatis中提供了fo ...
- yeild
正在使用cpu的线程,退出,返回等待池,其他优先级相同的线程竞争上岗.
- 对于C#中的一些点滴你真的理解了吗?
废话不多说看题目,看看我们自己真的理解了吗? 1.如下代码输出的结果是什么? public class A{ public virtual void Func(int number=10) { Co ...
- linux命令存放 bash: xxx command not found
参考资料:http://blog.sina.com.cn/s/blog_688077cf01013qrk.html 提示:bash: xxx command not found 首先就要考虑root ...
- MongoDB C#驱动中Query几个方法 (转)
Query.All("name", "a", "b");//通过多个元素来匹配数组 Query.And(Query.EQ("nam ...
- UITableViewCell 自适应高度 ios8特性
这篇文章介绍了在一个动态数据的 table view 中,cell 根据 text view 内容的输入实时改变 cell 和 table view 的高度.自动计算 cell 高度的功能使用 iOS ...
- hdu 2064
ps:分析发现,要移动n个到最左端,首先要移动n-1个从A到C,然后从C到A,然后再从A到C,然后中间是把大盘从A到C,两步.递推公式自然就是a[n]=3*a[n-1]+2 代码: #include ...