標籤:
android中的log4j記錄檔使用需要兩個包,我們不需要進行設定檔的配置,一切都在代碼中完成。
log4j 包下載:
:http://logging.apache.org/log4j/1.2/download.html
android-logging-log4j-1.0.3.jar 下載:
https://code.google.com/archive/p/android-logging-log4j/downloads
全部下載完成後 eclipse中智捷匯入包到libs添加飲用,android studio中同樣將包放入libs中右鍵點擊包 add as librarys;然後在gradle中添加
sourceSets{
main(){
jniLibs.srcDirs = [‘libs‘]
}
}
即可使用。
建立 ConfigureLog4J 檔案 然後填寫以下代碼:注釋在代碼中
/* 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 test.myc.minyuchun.log4jtest;import android.os.Environment;import org.apache.log4j.Level;import java.io.File;import java.util.Date;import de.mindpipe.android.logging.log4j.LogConfigurator;/** * 日誌設定 */public class ConfigureLog4J {//記錄層級優先度從高到低:OFF(關閉),FATAL(致命),ERROR(錯誤),WARN(警告),INFO(資訊),DEBUG(調試),ALL(開啟所有的日誌,我的理解與DEBUG層級好像沒有什麼區別得)//Log4j建議只使用FATAL ,ERROR ,WARN ,INFO ,DEBUG這五個層級。 // "yyyy-MM-dd");// 日誌的輸出格式 public static void configure() { final LogConfigurator logConfigurator = new LogConfigurator(); Date nowtime = new Date(); // String needWriteMessage = myLogSdf.format(nowtime); //記錄檔路徑地址:SD卡下myc檔案夾log檔案夾的test檔案 String fileName = Environment.getExternalStorageDirectory() + File.separator + "myc" + File.separator + "log" + File.separator + "test.log"; //設定檔案名稱 logConfigurator.setFileName(fileName); //設定root日誌輸出層級 預設為DEBUG logConfigurator.setRootLevel(Level.DEBUG); // 設定日誌輸出層級 logConfigurator.setLevel("org.apache", Level.INFO); //設定 輸出到記錄檔的文字格式 預設 %d %-5p [%c{2}]-[%L] %m%n logConfigurator.setFilePattern("%d %-5p [%c{2}]-[%L] %m%n"); //設定輸出到控制台的文字格式 預設%m%n logConfigurator.setLogCatPattern("%m%n"); //設定總檔案大小 logConfigurator.setMaxFileSize(1024 * 1024 * 5); //設定最大產生的檔案個數 logConfigurator.setMaxBackupSize(1); //設定所有訊息是否被立刻輸出 預設為true,false 不輸出 logConfigurator.setImmediateFlush(true); //是否本地控制台列印輸出 預設為true ,false不輸出 logConfigurator.setUseLogCatAppender(true); //設定是否啟用檔案附加,預設為true。false為覆蓋檔案 logConfigurator.setUseFileAppender(true); //設定是否重設設定檔,預設為true logConfigurator.setResetConfiguration(true); //是否顯示內部初始化日誌,預設為false logConfigurator.setInternalDebugging(false); logConfigurator.configure(); }}
在主acrivity中使用以下代碼即可
//載入配置 ConfigureLog4J configureLog4J=new ConfigureLog4J(); configureLog4J.configure(); //初始化 log Logger log=Logger.getLogger(this.getClass()); //寫 info 日誌 log.info("不知道呀就是測試一下啊");
android log4j日誌管理的使用