log4j保存日志到本地

 
  

log4j是一个保存日志到本地的第三方框架,Log4jConfigure 是配置类。如果是项目已经写到后期才加上保存日志的功能,

可以写一个Log类替换原来的调用系统的Log类,换掉import即可。直接调用Log.x就可以把日志打印在文件里了。

 
  
public class Log4jConfigure {
    private static final int MAX_FILE_SIZE = 1024*1024 * 10;
    private static final String DEFAULT_LOG_DIR = "";
    private static final String DEFAULT_LOG_FILE_NAME = "sdcm.log";
    private static final String TAG = "Log4jConfigure";
    // 对应AndroidManifest文件中的package
    private static final String PACKAGE_NAME = "xxx";
    public static void configure(String fileName) {
        final LogConfigurator logConfigurator = new LogConfigurator();
        try {
            Log.d(TAG, "configure: "+Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()));
                logConfigurator.setFileName("//data//data//" + PACKAGE_NAME + "//files"
                        + File.separator + fileName);
            //以下设置是按指定大小来生成新的文件
              logConfigurator.setMaxBackupSize(4);
              logConfigurator.setMaxFileSize(MAX_FILE_SIZE); 
            //以下设置是按天生成新的日志文件,与以上两句互斥,MAX_FILE_SIZE将不在起作用
            //文件名形如 MyApp.log.2016-06-02,MyApp.lo.2016-06-03
            //logConfigurator.setUseDailyRollingFileAppender(true);
            //以下为通用配置
            logConfigurator.setImmediateFlush(true);
            logConfigurator.setRootLevel(Level.DEBUG);
            logConfigurator.setFilePattern("%d\t%p/%c:\t%m%n");
            logConfigurator.configure();
            Log.e(TAG, "Log4j config finish");
        } catch (Throwable throwable) {
            logConfigurator.setResetConfiguration(true);
            Log.e(TAG, "Log4j config error, use default config. Error:" + throwable);
        }
    }
    public static void configure() {
        configure(DEFAULT_LOG_FILE_NAME);
    }
    private static boolean isSdcardMounted() {
        return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
    }
}

 
  
public class Log {
    /** log开关 */
    public static final boolean SWITCH_LOG = true;
    private static boolean isConfigured = false;
    public static void d(String tag, String message) {
        if (SWITCH_LOG) {
            Logger LOGGER = getLogger(tag);
            LOGGER.debug(message);
        }
    }
    public static void d(String tag, String message, Throwable exception) {
        if (SWITCH_LOG) {
            Logger LOGGER = getLogger(tag);
            LOGGER.debug(message, exception);
        }
    }
    public static void i(String tag, String message) {
        if (SWITCH_LOG) {
            Logger LOGGER = getLogger(tag);
            LOGGER.info(message);
        }
    }

    public static void i(String tag, String message, Throwable exception) {
        if (SWITCH_LOG) {
            Logger LOGGER = getLogger(tag);
           LOGGER.info(message, exception);
        }
    }

    public static void w(String tag, String message) {
        if (SWITCH_LOG) {
            Logger LOGGER = getLogger(tag);
            LOGGER.warn(message);
        }
    }
    public static void w(String tag, String message, Throwable exception) {
        if (SWITCH_LOG) {
            Logger LOGGER = getLogger(tag);
            LOGGER.warn(message, exception);
        }
    }

    public static void e(String tag, String message) {
        if (SWITCH_LOG) {
            Logger LOGGER = getLogger(tag);
            LOGGER.error(message);
        }
    }
    public static void e(String tag, String message, Throwable exception) {
        if (SWITCH_LOG) {
            Logger LOGGER = getLogger(tag);
            LOGGER.error(message, exception);
        }
    }
    private static Logger getLogger(String tag) {
        if (!isConfigured) {
            Log4jConfigure.configure();
            isConfigured = true;
        }
        Logger logger;
        if (TextUtils.isEmpty(tag)) {
            logger = Logger.getRootLogger();
        } else {
            logger = Logger.getLogger(tag);
        }
        return logger;
    }
}




你可能感兴趣的:(android控件大集合)