zoukankan      html  css  js  c++  java
  • 6、android 普通日志输出到SD卡

    这是本人见过写博文最负责的一个人:

    http://www.crifan.com/android_try_use_android_logging_log4j_to_output_log_to_sd_card_file/

    为什么说他负责呢??

    因为作者写出了从确定需求到找到解决方案的整个过程,不仅会让读者少走弯路,还把解决方案全盘托出,简直是太赞啦!

    当然也有些废话,把它的精华吸收过来,废话不说,上干货:

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

    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

  • 相关阅读:
    UVa 11300 Spreading the Wealth(有钱同使)
    hihoCoder 1385 : A Simple Job(简单工作)
    hihoCoder 1383 : The Book List(书目表)
    HDU 5724 Chess(国际象棋)
    Sobytiynyy Proyekt Casino Gym
    Course recommendation Gym
    Poor Folk Gym
    How far away? (HDU
    BAPC 2016 ----Brexit (BFS + vector)
    Simpsons’ Hidden Talents(扩展KMP)
  • 原文地址:https://www.cnblogs.com/kunyashaw/p/4260706.html
Copyright © 2011-2022 走看看