当前位置:首页 » 安卓系统 » 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。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

热点内容
如何快速解压很多文件手机版 发布:2024-04-30 21:45:06 浏览:435
redhatpython安装 发布:2024-04-30 21:37:31 浏览:355
长城大屏导航初始密码多少 发布:2024-04-30 21:37:18 浏览:181
知道源码 发布:2024-04-30 20:51:27 浏览:346
c语言视频教程夏老师 发布:2024-04-30 20:47:54 浏览:318
linux五子棋 发布:2024-04-30 20:46:40 浏览:285
chrome添加脚本 发布:2024-04-30 20:44:25 浏览:788
多脚本界面 发布:2024-04-30 20:38:45 浏览:738
我的世界显示无法访问服务器 发布:2024-04-30 20:35:00 浏览:256
台式电脑如何自己配置 发布:2024-04-30 20:34:49 浏览:753