【背景】
折腾:
期间,已经试了:
【记录】尝试用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
然后导入库,写示例代码。
结果导入的时候,就说不支持:
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
中的:
后来发现:
里面有:
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.后来发现:
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.最后去试试代码:
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了。
所以改为:
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");
结果是:
在:
logConfigurator.setLevel("org.apache", Level.ERROR);
会挂掉:
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
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
/* 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 http://www.apache.org/licenses/LICENSE-2.0 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
/* 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 http://www.apache.org/licenses/LICENSE-2.0 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.所以尝试继续看看,能否优化代码:
尽量减少文件,把相关代码弄在一个文件里面。
然后用如下代码:
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.写测试代码:
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-logging-log4j去实现log输出内容到sd卡中的文件的功能