如何从android中创建的文件中删除log 4j默认的时间戳和日志级别信息?(How to remove log 4j default info of timestamp and log level from the created file in android?)

我已经通过GitHub中的可用源在sdcard中创建了一个log 4 j文件。 这是输出

2013-03-04 06:21:14,097 - [DEBUG::abc::com.newvisioninteractive.androidlog4jprototype.SampleClass] - test test!

2013-03-04 06:21:14,101 - [INFO::abc::com.newvisioninteractive.androidlog4jprototype.SampleClass] - abc

我的问题是删除文件的所有默认信息,如filecreation的时间戳和“DEBUG”或“INFO”包信息。简而言之,我只想要字符串“测试测试!” 在第一行和第二行文件中的“abc”。

请建议我可能的解决方案。

I have created a log 4 j file in sdcard by the available source in GitHub. Here is output

2013-03-04 06:21:14,097 - [DEBUG::abc::com.newvisioninteractive.androidlog4jprototype.SampleClass] - test test!

2013-03-04 06:21:14,101 - [INFO::abc::com.newvisioninteractive.androidlog4jprototype.SampleClass] - abc

My question is to remove all the default info of file like, time stamp of filecreation & "DEBUG" or "INFO" package info.In short I want only String "test test!" in first line and "abc" in second line of file.

Please suggest me the possible solution.

原文:https://stackoverflow.com/questions/15195946

更新时间:2019-12-09 17:09

最满意答案

尝试将此添加到方法configure() :

logConfigurator.setFilePattern("%m%n");

Try adding this to the method configure():

logConfigurator.setFilePattern("%m%n");

相关问答

在PatternLayout中使用%d。 另外%d可以采用格式模式,如%d {dd MMM yyyy HH:mm:ss,SSS},您可以选择并选择所需的元素。 当格式模式被省略时,日期将以ISO8601格式。 Use %d in your PatternLayout. Also %d can take a format pattern as in %d{dd MMM yyyy HH:mm:ss,SSS} you can pick and choose the elements that you w

...

查看PatternLayout的文档。 请注意,确定文件名和文件编号是很慢的 (我想它涉及到获取堆栈跟踪并分析它)。 Look at the documentation for PatternLayout. Note that figuring out file name and file number is slow (I guess it involves getting a stack trace and analyzing it).

我终于开发了log4j-additions库,它允许创建一个Lazy初始化appender,避免创建空的日志文件。 log4.properties #Lazy File Appender

log4j.appender.A1=org.pollerosoftware.log4j.additions.appenders.LazyFileAppender

log4j.appender.A1.File=juakaritoglory.log

log4j.appender.A1.layout=org.apach

...

如果你真的命名它 “log4j.propeties”而不是 “log4j.properties”,那么这可能是原因。 If you really named it "log4j.propeties" instead of "log4j.properties", then this may be the reason.

我经历过类似的行为,结果发现Log4J配置了不止一次; 使用BasicConfigurator,还有我忘记的log4j.xml文件。 是不是在类路径的某处有一个额外的Log4J配置? I've experienced similar behavior, and it turned out that Log4J was configured more than once; using the BasicConfigurator, but also with a log4j.xml file I ha

...

尝试将此添加到方法configure() : logConfigurator.setFilePattern("%m%n");

Try adding this to the method configure(): logConfigurator.setFilePattern("%m%n");

我相信如果要使用相同的日志文件,您应该为所有适配器使用单个共享日志4J配置和实例。 因此,Log 4j实例可能正在竞争文件的控制。 I believe you should be using a single shared log 4J configuration and instance for all your adapters if you want to use the the same log file. As is, Log 4j instances are probably comp

...

如果您在appender-ref中指定INFO,appender将收到INFO,WARN,ERROR和FATAL事件。 通过筛选出WARN,ERROR和FATAL级别事件,您可以进一步限制为INFO:

pattern="%d{yyyy-mm-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%nZ

...

log4j.properties应该在您的类路径中。 从命令行运行时将其添加到类路径中。 更好的选择是使用指定属性文件 -Dlog4j.configuration=relative path/log4j.properties 从命令行。 在这种情况下,您可以删除PropertyConfigurator.configure("log4j.properties"); 从您的代码 - 您不需要在代码中执行任何操作来指定属性文件。 log4j.properties should be in your cl

...

复制你的appender environments {

production {

log4j = { root ->

appenders {

rollingFile name:'stdout', file:"${logDirectory}/myapp.log".toString(), maxFileSize:'100KB'

rollingFile name:'stacktrace', f

...

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐