當前位置:首頁 » 安卓系統 » log4jforandroid

log4jforandroid

發布時間: 2022-12-06 16:33:08

1. log4j怎樣能生成日期分割的文件

寫一個log4jInit吧,,在你的log4j裡面設置幾個變數,例如${some_day_path},然後使用apache有個項目叫configuration來替換你的變數。
/**
* @Synopsis 讀取多個log4j配置文件合並成一個。main-log4j.xml為主配置文件。只有主配置文件中的root與root appender
* 才生效。
*/
public class LoggingInit{
private static final String CONFIG_FILE_NAME_PATTERN = "^(?!log4j)[a-zA-Z0-9]+.xml";
private static final String LOG4J_FILE_NAME = "log4j.xml";
private static File config_file = null;
/**
* @Synopsis configure
* @Param File _file
* @Return
*/
public static void init(){
LoggingInit init = new LoggingInit();
File file = init.combined();
config_file = init.molate( file );
DOMConfigurator.configure( config_file.getPath() );
}
/**
* @Synopsis getConfigFile
* @Return
*/
public static File getConfigFile(){
return config_file;
}
/**
* @Synopsis replace path variables
*/
class RealPath extends StrLookup{
@Override
public String lookup( String _key ){
if( _key.trim().equals( "log_dir" ) ){
return Path.singleInstance().getLogPath();
}
return "not-found";
}
}
/**
* @Synopsis combined
* @Return
*/
public File combined(){
File out = new File( Path.singleInstance().getLogConfPath() + File.separator + LOG4J_FILE_NAME );
try{
NodeCombiner node_combiner = new MergeCombiner();
node_combiner.addListNode( "log4j:configuration" );
CombinedConfiguration combined = new CombinedConfiguration( node_combiner );
File dir = new File( Path.singleInstance().getLogConfPath() );
IOFileFilter file_filter = new RegexFileFilter( CONFIG_FILE_NAME_PATTERN );
IOFileFilter directory_filter = TrueFileFilter.INSTANCE;
Collection< File > files = FileUtils.listFiles( dir, file_filter, directory_filter );
for( File f : files ){
XMLConfiguration c = new XMLConfiguration( f );
combined.addConfiguration( c );
}
XMLConfiguration configuration = new XMLConfiguration( combined );
configuration.setPublicID( "-//log4j/log4j Configuration//EN" );
configuration.setSystemID( "log4j.dtd" );
configuration.setRootElementName( "log4j:configuration" );
ConfigurationInterpolator interpolator = configuration.getInterpolator();
<span style="color: #FF0000;">interpolator.registerLookup( "path", new RealPath() );</span>(按你的規則替換成你的日誌路勁)
XMLConfiguration rs = ( XMLConfiguration ) configuration.interpolatedConfiguration();
rs.save( out );
}catch( ConfigurationException e ){
throw new InitException( "combined exection", e );
}
return out;
}
/**
* @Synopsis molate
* @Param File _file
* @Return
*/
public File molate( File _file ){
try{
SAXReader reader = new SAXReader();
Document dom = reader.read( _file );
Element configuration = ( Element ) dom.selectSingleNode( "/configuration" );
List appender_list = configuration.selectNodes( "appender" );
List logger_list = configuration.selectNodes( "logger" );
Node root = configuration.selectSingleNode( "root" );
List< Element > elements = configuration.elements();
elements.clear();
for( Object object : appender_list ){
elements.add( ( Element ) object );
}
for( Object object : logger_list ){
elements.add( ( Element ) object );
}
elements.add( ( Element ) root );
OutputFormat format = OutputFormat.createPrettyPrint();
format.setIndentSize( 4 );
XMLWriter writer = new XMLWriter( new FileOutputStream( _file ), format );
writer.write( dom );
writer.close();
}catch( DocumentException e ){
throw new InitException( "fromat exception", e );
}catch( FileNotFoundException e ){
throw new InitException( "fromat exception", e );
}catch( UnsupportedEncodingException e ){
throw new InitException( "fromat exception", e );
}catch( IOException e ){
throw new InitException( "fromat exception", e );
}
return _file;
}
}

2. eclipse運行android出現這個怎麼解決

包不兼容,(org.apache.log4j.chainsaw.ControlPanel$1) that doesn't come with an
associated EnclosingMethod attribute 這個一看就是log4j用的要麼新了,要麼舊了,如果是你用得包是自帶的,仔細看一下是不是有兩個重復的jar包

3. android中 log4j.properties應該放在哪

我原來通常都是把 log4j.properties放在WEB-INF目錄下, web.xml相應的定義為:
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
這個設置對於該web app沒有任何問題。但是如果你要進行JUnit Test,在運行TestCase時,就會發生錯誤:
log4j:WARN No appenders could be found for logger
log4j:WARN Please initialize the log4j system properly.

那是因為在classpath里找不到log4j.properties,因此我把log4j.properties改放在WEB-INF/classes目錄下,web.xml相應的定義改為:
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>

這樣運行Junit時也會正常!

4. microlog4android如何將Android Log日誌寫到SD

我們在進行Android開發時,經常會有這種體會,由於客戶的設備型號各不相同,從而導致出現多種問題,但對於研發人員,因為沒有log日誌文件,不太容易定位問題的具體位置。所以我們急需一個能將程序日誌寫到SD卡文件里的工具,類似web的log4j那樣。這時microlog4android進入我們的視野,雖然它尚有不足,但也已能滿足大部分需求。

使用microlog4android的步驟如下:

1、下載

到http://code.google.com/p/microlog4android/downloads/list 下載microlog4android-1.0.0.jar和microlog.properties文件。

2、建立使用logger對象

private static final Logger logger = LoggerFactory.getLogger(main.class);

3、在程序的第一個activity的oncreate方法里初始化方法

PropertyConfigurator.getConfigurator(this).configure();

4、把microlog.properties文件放到assets文件夾

注意:assets文件夾是與res文件夾平級的。

然後更改microlog.properties文件為以下內容:

microlog.level=DEBUG microlog.appender=LogCatAppender;FileAppender microlog.formatter=PatternFormatter microlog.formatter.PatternFormatter.pattern=%c [%P] %m %T

5、寫日誌記錄

logger.debug("這是debug信息");

6、在AndroidManifest.xml 添加寫sd卡的許可權

運行程序,然後到SD卡根目錄,可以發現有一個microlog.txt的'文件,裡面就是我們的日誌了。

更改日誌問價的路徑和名稱

有朋友問如何更改日誌文件的路徑和名稱,在網上找了一通的資料,沒找到答案,只好到https://github.com/johanlkarlsson/microlog4android 下載源代碼,經過查看源代碼的PropertyConfigurator類,發現源代碼里有這么一個配置參數microlog.appender.FileAppender.File,這樣應該就可以更改日誌文件的路徑和名稱了。

配置文件如下:

microlog.level=DEBUG microlog.appender=FileAppender;LogCatAppender microlog.appender.FileAppender.File=mylog.txt microlog.formatter=PatternFormatter microlog.formatter.PatternFormatter.pattern=%c [%P] %m %T

運行程序,發現日誌文件還是叫microlog.txt,更改名稱無效,找了各種原因都無法解決,只好反編譯我們之前下載的microlog4android-1.0.0.jar包,發現PropertyConfigurator這個類和剛剛從GIT下載的源代碼PropertyConfigurator類不一樣,microlog4android-1.0.0.jar包里的PropertyConfigurator類沒有這樣的一個參數microlog.appender.FileAppender.File,只好把從GIT下載下來的源代碼,重新打個包,暫時打成microlog4android-1.1.jar,然後重新運行程序,OK,搞定,日誌文件名稱變成了我們配置的mylog.txt。

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

熱點內容
OP編譯驅動 發布:2025-03-13 05:54:41 瀏覽:73
圖片集體壓縮 發布:2025-03-13 05:53:47 瀏覽:875
壓縮文件是什麼 發布:2025-03-13 05:19:38 瀏覽:996
debian自啟動腳本 發布:2025-03-13 05:16:23 瀏覽:429
如何登錄微信找回密碼 發布:2025-03-13 05:06:20 瀏覽:450
pc游戲編程人機博弈源碼 發布:2025-03-13 04:51:45 瀏覽:605
手機原生配置低怎麼玩流暢 發布:2025-03-13 04:35:31 瀏覽:736
分線器安卓供電口有什麼用 發布:2025-03-13 04:19:54 瀏覽:137
埠訪問關系 發布:2025-03-13 03:49:50 瀏覽:789
運用零基預演算法 發布:2025-03-13 03:45:30 瀏覽:791