【背景】

折腾:

【记录】给Android中添加log日志输出到文件

期间,已经试了:

【记录】尝试用android中microlog4android实现log输出到文件的功能

但是不好用。

然后就是参考:

http://stackoverflow.com/questions/2116260/logging-to-a-file-on-android

去看看:

http://code.google.com/p/android-logging-log4j/

【[折腾过程】

1.去:

https://code.google.com/p/android-logging-log4j/downloads/list

下载:

android-logging-log4j-1.0.3.jar

然后导入库,写示例代码。

结果导入的时候,就说不支持:

1
import org.apache.log4j.Level;

2.然后找到:

http://logging.apache.org/log4j/2.x/

然后去下载:

apache-log4j-2.0-beta9-bin.zip

发现有4M多,好大。

3.然后换用:

http://logging.apache.org/log4j/1.2/download.html

中的:

log4j-1.2.17.zip

后来发现:

里面有:

36KB的log4j-1.2-api-2.0-beta9.jar

log4j-1.2.17\apache-log4j-1.2.17中有:479KB的log4j-1.2.17.jar

所以还是去用:

apache-log4j-2.0-beta9-bin\log4j-1.2-api-2.0-beta9.jar

加到项目中。

4.后来发现:

1
final Logger gLogger = Logger.getLogger(logConfigurator.getClass());

会出错,貌似需要导入:

apache-log4j-2.0-beta9-bin

下面的:

log4j-api-2.0-beta9.jar

(和? 或?)

log4j-core-2.0-beta9.jar

然后发现导入:

log4j-core-2.0-beta9.jar

就可以了。

5.最后去试试代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import de.mindpipe.android.logging.log4j.LogConfigurator;
 
public class BaseActivity extends Activity {
    final LogConfigurator logConfigurator = new LogConfigurator();
    final Logger gLogger = Logger.getLogger(logConfigurator.getClass());
     
    //public Logger createLogFile()
    public void configLog()
    {
        logConfigurator.setFileName(Environment.getExternalStorageDirectory() + "crifanli_log4j.log");
        logConfigurator.setRootLevel(Level.DEBUG);
        // Set log level of a specific logger
        logConfigurator.setLevel("org.apache", Level.ERROR);
        logConfigurator.configure();
    }
     
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        configLog();
         
        gLogger.debug("test android log to file using log4j");

效果如何,

貌似发现会挂掉。

6.然后再去把:

log4j-api-2.0-beta9.jar

也加进来,看看是否可以。

貌似还是不行。

然后发现是代码写的有问题:

还没初始化,就去getLogger了。

所以改为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import de.mindpipe.android.logging.log4j.LogConfigurator;
 
public class BaseActivity extends Activity {
    private LogConfigurator logConfigurator;
    private Logger gLogger;
     
    public void configLog()
    {
        logConfigurator = new LogConfigurator();
        logConfigurator.setFileName(Environment.getExternalStorageDirectory() + "crifanli_log4j.log");
        logConfigurator.setRootLevel(Level.DEBUG);
        // Set log level of a specific logger
        logConfigurator.setLevel("org.apache", Level.ERROR);
        logConfigurator.configure();
         
        gLogger = Logger.getLogger(logConfigurator.getClass());
    }
     
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        configLog();
         
        gLogger.debug("test android log to file using log4j");

结果是:

在:

1
logConfigurator.setLevel("org.apache", Level.ERROR);

会挂掉:

【已解决】android中使用android-logging-log4j调用logConfigurator.setLevel会出错:ERROR StatusLogger Unable to locate a logging implementation, using SimpleLogger

7.然后可以正常生成log文件:

对应的log的内容,还是不错的:

至此:

终于可以在android中,正常使用这个log4j去实现log输出内容到sd中的文件了。

8.目前,正常工作的配置和代码是:

(1)android项目中,导入:

  • android-logging-log4j-1.0.3.jar
  • log4j-1.2.17.jar
    • 注意:我此处用apache-log4j-2.0-beta9-bin中的log4j-1.2-api-2.0-beta9.jar + log4j-core-2.0-beta9.jar 会出错:
      • 这句:logConfigurator.setLevel("org.apache", Level.ERROR);,会挂掉的。

如图:

(2)代码这么写:

(a)BaseActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
 
public class BaseActivity extends Activity {
    private Logger gLogger;
     
    public void configLog()
    {
        ConfigureLog4J.configure();
        gLogger = Logger.getLogger(ExampleLog4J.class);
    }
     
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        configLog();
         
        gLogger.debug("test android log to file using log4j");
                

(b)ExampleLog4J.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
   Copyright 2011 Rolf Kulemann
 
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
 
 
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/ 
package com.mm.mobilehandheld.common;
 
 
import org.apache.log4j.Logger;
 
/**
 * Demonstrates using log4j directly.
 * @author Rolf Kulemann
 */
public class ExampleLog4J {
    private final Logger log = Logger.getLogger(ExampleLog4J.class);
     
    public void myMethod() {
        log.info("This message should be seen in log file and logcat");
    }
}

(c)ConfigureLog4J.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
   Copyright 2011 Rolf Kulemann
 
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
 
 
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/ 
package com.mm.mobilehandheld.common;
 
import java.io.File;
 
import org.apache.log4j.Level;
 
import android.os.Environment;
import de.mindpipe.android.logging.log4j.LogConfigurator;
 
/**
 * Example how to to configure Log4J in Android.
 * Call {@link #configure()}} from your application's activity.
 * @author Rolf Kulemann
 */
public class ConfigureLog4J {
    public static void configure() {
        final LogConfigurator logConfigurator = new LogConfigurator();
         
        logConfigurator.setFileName(Environment.getExternalStorageDirectory() + File.separator + "myapp.log");
        // Set the root log level
        logConfigurator.setRootLevel(Level.DEBUG);
        // Set log level of a specific logger
        logConfigurator.setLevel("org.apache", Level.ERROR);
        logConfigurator.configure();
    }
}

即可。

不过,很明显,为了简单的log到文件,弄了这么多类,这么多文件,很不爽。

9.所以尝试继续看看,能否优化代码:

尽量减少文件,把相关代码弄在一个文件里面。

然后用如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.io.File;
import android.os.Environment;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
 
public class BaseActivity extends Activity {
    private Logger gLogger;
     
    public void configLog()
    {
        final LogConfigurator logConfigurator = new LogConfigurator();
         
        logConfigurator.setFileName(Environment.getExternalStorageDirectory() + File.separator + "crifanli_log4j.log");
        // Set the root log level
        logConfigurator.setRootLevel(Level.DEBUG);
        // Set log level of a specific logger
        logConfigurator.setLevel("org.apache", Level.ERROR);
        logConfigurator.configure();
 
        //gLogger = Logger.getLogger(this.getClass());
        gLogger = Logger.getLogger("CrifanLiLog4jTest");
    }
     
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        configLog();
        gLogger.debug("test android log to file in sd card using log4j");

测试效果为:

log的内容为:

【总结】

至此,终于基本实现需要的。

在android中,实现输出log内容到sd卡中的文件里面,做法是:

还是相对来说,log4j,算是好用。

1.下载android的log4j的库(的封装)

去:http://code.google.com/p/android-logging-log4j/

下载对应的android-logging-log4j-1.0.3.jar,加到项目中。

2.再去下载所依赖的apache的log4j库

去:http://logging.apache.org/log4j/1.2/download.html

下载1.2系列版本的:log4j-1.2.17.zip

解压得到log4j-1.2.17.jar加到项目中。

3.写测试代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import de.mindpipe.android.logging.log4j.LogConfigurator;
import java.io.File;
import android.os.Environment;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
 
public class BaseActivity extends Activity {
    private Logger gLogger;
     
    public void configLog()
    {
        final LogConfigurator logConfigurator = new LogConfigurator();
         
        logConfigurator.setFileName(Environment.getExternalStorageDirectory() + File.separator + "crifanli_log4j.log");
        // Set the root log level
        logConfigurator.setRootLevel(Level.DEBUG);
        // Set log level of a specific logger
        logConfigurator.setLevel("org.apache", Level.ERROR);
        logConfigurator.configure();
 
        //gLogger = Logger.getLogger(this.getClass());
        gLogger = Logger.getLogger("CrifanLiLog4jTest");
    }
     
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        configLog();
        gLogger.debug("test android log to file in sd card using log4j");

即可实现:

(1)可以在/mnt/sdcard中生成对应的crifanli_log4j.log文件

(2)log输出的内容中,是DEBUG,且对应的是自己的字符串标识符CrifanLiLog4jTest

 
您可能也喜欢:

【记录】尝试用android中microlog4android实现log输出到文件的功能

【已解决】android中使用android-logging-log4j调用logConfigurator.setLevel会出错:ERROR StatusLogger Unable to locate a logging implementation, using SimpleLogger

【记录】对于android中的log4j去停止log

【记录】go语言中通过log4go实现同时输出log信息到log文件和console

【已解决】Android的logcat中如何根据标签去过滤掉不显示某些log信息

【记录】给Android中添加log日志输出到文件

【已解决】C#中实现Log同时输出内容到文件和文本框(或终端)

【已解决】go语言中实现输出日志内容到log文件

 
防君子不防也无法防小人的声明如未注明转载等字样则均为crifan原创。对于原创文章,转载请注明出处:
在路上 - on the way - 走别人没走过的路,让别人有路可走
本文链接: 
【记录】尝试用android-logging-log4j去实现log输出内容到sd卡中的文件的功能 


捐赠(Donate):  | 

分类 Android标签 androidandroid-logging-log4jloglogging文件

 
 

2 Thoughts on “【记录】尝试用android-logging-log4j去实现log输出内容到sd卡中的文件的功能”

【记录】尝试用android-logging-log4j去实现log输出内容到sd卡中的文件的功能的更多相关文章

  1. Android 5.1.1在外置SD卡中创建文件夹

    Android 4.4之后WRITE_MEDIA_STORAGE 权限仅提供给系统应用,不再授予第三方App,WRITE_EXTERNAL_STORAGE 权限,仅仅用于授权用户写 primary e ...

  2. Android扫描SD卡中的文件

    当android的系统启动的时候,系统会自动扫描sdcard内的多媒体文件,并把获得的信息保存在一个系统数据库中,以后在其他程序中如果想要访问多媒体文件的信息,其实就是在这个数据库中进行的,而不是直接 ...

  3. android 往sd卡中写入文件

    在调用前需要判断是否有写入权限 Environment类提供了比较丰富的方法 static File getDataDirectory() 获得android data的目录. static File ...

  4. 【Android 界面效果30】Android中ImageSwitcher结合Gallery展示SD卡中的资源图片

    本文主要是写关于ImageSwitcher结合Gallery组件如何展示SDCard中的资源图片,相信大家都看过API Demo 中也有关于这个例子的,但API Demo 中的例子是展示工程中Draw ...

  5. 转-Android 之 使用File类在SD卡中读取数据文件

    如果需要在程序中使用sdcard进行数据的存储,那么需要在AndroidMainfset.xml文件中 进行权限的配置: Java代码:   <!-- 在sd中创建和删除文件的权限 --> ...

  6. Android 异步加载图片,使用LruCache和SD卡或手机缓存,效果非常的流畅

      Android 高手进阶(21)  版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明出处http://blog.csdn.net/xiaanming/article/details ...

  7. Android自定义照相机实现(拍照、保存到SD卡,利用Bundle在Acitivity交换数据)

    Android自定义照相机实现 近期小巫在学校有一个创新项目,也不是最近,是一个拖了很久的项目,之前一直没有去搞,最近因为要中期检查,搞得我跟小组成员一阵忙活,其实开发一款照相机软件并不太难,下面就是 ...

  8. Android将应用调试log信息保存在SD卡

    转载:http://blog.csdn.net/way_ping_li/article/details/8487866 把自己应用的调试信息写入到SD卡中. package com.sdmc.hote ...

  9. Android中使用SQLiteOpenHelper管理SD卡中的数据库

    使用Android中自带的SQLiteOpenHelper可以完成数据库的创建与管理,但有两点局限: (1)数据库创建在内存卡中,大小受限,创建位置位于/data/data/应用程序名/databas ...

随机推荐

  1. ubuntu qtcreator 硬件权限问题

    在使用 qtcreator 在 ubuntu(debian.mint 等类同)下做开发时,常用到权限问题,无法直接操作硬件,比如串口等. 办法之一是使用 root 打开 creator,进而进行其他操 ...

  2. node 常用命令

    nvm nvm list  列出安装的node npm install -g cnpm --registry=https://registry.npm.taobao.org  安装cnpm npm i ...

  3. RabbitMQ 参数们的Power “续”

    参数中的 arguments 之前讲参数的一些作用的时候,忽略了最后一个字典类型的参数,因为这个参数是大有文章的,值得单独进出来说道说道. 这时,就不得不打开我们的 Web UI管理系统了,可以看到在 ...

  4. HTML5学习总结-番外05 响应式布局

    1. 响应式布局 响应式布局是2015年5月份提出的一个概念,简而言之,就是一个网站能够兼容多个终端,而不是为每个终端做一个特定的版本.这个概念是为解决移动互联网浏览而诞生的.其目的是为用户提欧共更加 ...

  5. html页面输入框input的美化

    input输入框是网页必不可少的组件,可是每个浏览器对于输入框的显示样式各有不同 例如:    上图分别就是谷歌浏览器和IE浏览器自带显示的输入框,样式也不足人意,所以大多都会自己写样式 以下是一个简 ...

  6. [Head First设计模式]餐馆中的设计模式——命令模式

    系列文章 [Head First设计模式]山西面馆中的设计模式——装饰者模式 [Head First设计模式]山西面馆中的设计模式——观察者模式 [Head First设计模式]山西面馆中的设计模式— ...

  7. 什么叫session和cookie-及其设置

    http的无状态? 保持状态, 是指当程序关闭后重启, 上一次操作的历史还能继续, 保持的. 如word中的 "选项"设置. 如windows系统的设置等等. http的设计目的, ...

  8. JDI tutorial (trace example)

    Components Debugger Interfaces / |--------------| / | VM | debuggee ----( |--------------| <----- ...

  9. windows环境下面安装Apache2.4+MySql5.7+PHP5.6

    之前学习PHP一致是只用phpStudy集成开发环境,这对于新手而言无疑是帮助极大的,因为傻瓜式安装与使用方法减少了我们很多不必要的麻烦.但是作为一名合格的PHP开发人员,掌握PHP+MySQL+Ap ...

  10. $_SERVER["SCRIPT_NAME"]、$_SERVER["PHP_SELF"]、$_SERVER["QUERY_STRING"]、$_SERVER["REQUEST_URI"]

    1.$_SERVER["SCRIPT_NAME"] 说明:包含当前脚本的路径 2.$_SERVER["PHP_SELF"] 说明:当前正在执行脚本的文件名 3. ...