【已解决】android中使用android-logging-log4j调用logConfigurator.setLevel会出错:ERROR StatusLogger Unable to locate a logging implementation, using SimpleLogger

【问题】

折腾:

【记录】尝试用android-logging-log4j去实现log输出内容到sd卡中的文件的功能

期间,用如下代码:

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import de.mindpipe.android.logging.log4j.LogConfigurator;

public class BaseActivity extends Activity {
	private LogConfigurator logConfigurator;
    private Logger gLogger;
    
	public void configLog()
	{
		logConfigurator = new LogConfigurator();
	    logConfigurator.setFileName(Environment.getExternalStorageDirectory() + "crifanli_log4j.log");
	    logConfigurator.setRootLevel(Level.DEBUG);
	    // Set log level of a specific logger
	    logConfigurator.setLevel("org.apache", Level.ERROR);
	    logConfigurator.configure();
	    
	    gLogger = Logger.getLogger(logConfigurator.getClass());
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		configLog();
		
		gLogger.debug("test android log to file using log4j");

去运行,结果在:

logConfigurator.setLevel("org.apache", Level.ERROR);

会挂掉:

11-01 17:05:40.130: D/dalvikvm(8475): VFY: replacing opcode 0x22 at 0x000d
11-01 17:05:40.130: W/dalvikvm(8475): Unable to resolve superclass of Lde/mindpipe/android/logging/log4j/LogCatAppender; (2629)
11-01 17:05:40.130: W/dalvikvm(8475): Link of class 'Lde/mindpipe/android/logging/log4j/LogCatAppender;' failed
11-01 17:05:40.130: E/dalvikvm(8475): Could not find class 'de.mindpipe.android.logging.log4j.LogCatAppender', referenced from method de.mindpipe.android.logging.log4j.LogConfigurator.configureLogCatAppender
11-01 17:05:40.130: W/dalvikvm(8475): VFY: unable to resolve new-instance 1368 (Lde/mindpipe/android/logging/log4j/LogCatAppender;) in Lde/mindpipe/android/logging/log4j/LogConfigurator;
11-01 17:05:40.130: D/dalvikvm(8475): VFY: replacing opcode 0x22 at 0x000d
11-01 17:05:40.130: I/dalvikvm(8475): Could not find method org.apache.log4j.helpers.LogLog.setInternalDebugging, referenced from method de.mindpipe.android.logging.log4j.LogConfigurator.configure
11-01 17:05:40.130: W/dalvikvm(8475): VFY: unable to resolve static method 17895: Lorg/apache/log4j/helpers/LogLog;.setInternalDebugging (Z)V
11-01 17:05:40.130: D/dalvikvm(8475): VFY: replacing opcode 0x71 at 0x0015
11-01 17:05:40.130: D/dalvikvm(8475): DexOpt: unable to opt direct call 0x45db at 0x13 in Lde/mindpipe/android/logging/log4j/LogConfigurator;.configureFileAppender
11-01 17:05:40.130: W/dalvikvm(8475): Unable to resolve superclass of Lde/mindpipe/android/logging/log4j/LogCatAppender; (2629)
11-01 17:05:40.130: W/dalvikvm(8475): Link of class 'Lde/mindpipe/android/logging/log4j/LogCatAppender;' failed
11-01 17:05:40.130: D/dalvikvm(8475): DexOpt: unable to opt direct call 0x260f at 0x0f in Lde/mindpipe/android/logging/log4j/LogConfigurator;.configureLogCatAppender
11-01 17:05:46.140: W/System.err(8475): ERROR StatusLogger Unable to locate a logging implementation, using SimpleLogger
11-01 17:05:49.251: D/AndroidRuntime(8475): Shutting down VM
11-01 17:05:49.251: W/dalvikvm(8475): threadid=1: thread exiting with uncaught exception (group=0xb4e1d288)
11-01 17:05:49.251: E/AndroidRuntime(8475): FATAL EXCEPTION: main
11-01 17:05:49.251: E/AndroidRuntime(8475): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mm.mobilehandheld/com.mm.mobilehandheld.activities.DeviceListActivity}: java.lang.ClassCastException: org.apache.logging.log4j.simple.SimpleLoggerContext cannot be cast to org.apache.logging.log4j.core.LoggerContext
11-01 17:05:49.251: E/AndroidRuntime(8475): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-01 17:05:49.251: E/AndroidRuntime(8475): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-01 17:05:49.251: E/AndroidRuntime(8475): 	at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-01 17:05:49.251: E/AndroidRuntime(8475): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-01 17:05:49.251: E/AndroidRuntime(8475): 	at android.os.Handler.dispatchMessage(Handler.java:99)
11-01 17:05:49.251: E/AndroidRuntime(8475): 	at android.os.Looper.loop(Looper.java:137)
11-01 17:05:49.251: E/AndroidRuntime(8475): 	at android.app.ActivityThread.main(ActivityThread.java:4745)
11-01 17:05:49.251: E/AndroidRuntime(8475): 	at java.lang.reflect.Method.invokeNative(Native Method)
11-01 17:05:49.251: E/AndroidRuntime(8475): 	at java.lang.reflect.Method.invoke(Method.java:511)
11-01 17:05:49.251: E/AndroidRuntime(8475): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-01 17:05:49.251: E/AndroidRuntime(8475): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-01 17:05:49.251: E/AndroidRuntime(8475): 	at dalvik.system.NativeStart.main(Native Method)
11-01 17:05:49.251: E/AndroidRuntime(8475): Caused by: java.lang.ClassCastException: org.apache.logging.log4j.simple.SimpleLoggerContext cannot be cast to org.apache.logging.log4j.core.LoggerContext
11-01 17:05:49.251: E/AndroidRuntime(8475): 	at org.apache.log4j.Logger.getLogger(Logger.java:39)
11-01 17:05:49.251: E/AndroidRuntime(8475): 	at de.mindpipe.android.logging.log4j.LogConfigurator.setLevel(LogConfigurator.java:118)
11-01 17:05:49.251: E/AndroidRuntime(8475): 	at com.mm.mobilehandheld.common.ConfigureLog4J.configure(ConfigureLog4J.java:38)
11-01 17:05:49.251: E/AndroidRuntime(8475): 	at com.mm.mobilehandheld.activities.BaseActivity.configLog(BaseActivity.java:33)
11-01 17:05:49.251: E/AndroidRuntime(8475): 	at com.mm.mobilehandheld.activities.BaseActivity.onCreate(BaseActivity.java:39)
11-01 17:05:49.251: E/AndroidRuntime(8475): 	at com.mm.mobilehandheld.activities.DeviceListActivity.onCreate(DeviceListActivity.java:183)
11-01 17:05:49.251: E/AndroidRuntime(8475): 	at android.app.Activity.performCreate(Activity.java:5008)
11-01 17:05:49.251: E/AndroidRuntime(8475): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-01 17:05:49.251: E/AndroidRuntime(8475): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-01 17:05:49.251: E/AndroidRuntime(8475): 	... 11 more

如图:

ERROR StatusLogger Unable to locate a logging implementation

即:

ERROR StatusLogger Unable to locate a logging implementation

其后背景是:

android中,为了使用android-logging-log4j

加了对应的库:

apache-log4j-2.0-beta9-bin

中的:

log4j-1.2-api-2.0-beta9.jar

log4j-api-2.0-beta9.jar

 

【解决过程】

1.通过:

http://android-logging-log4j.googlecode.com/svn/trunk/src/main/java/de/mindpipe/android/logging/log4j/LogConfigurator.java

中的:

	/**
	 * Sets the level of logger with name <code>loggerName</code>.
	 * Corresponds to log4j.properties <code>log4j.logger.org.apache.what.ever=ERROR</code>
	 * @param loggerName
	 * @param level
	 */
	public void setLevel(final String loggerName, final Level level) {
		Logger.getLogger(loggerName).setLevel(level);
	}

发现:

此处,当前,根本就没有一个叫做:

org.apache

的Logger,所以内部调用:

Logger.getLogger(loggerName).

再去:

setLevel(level);

必然出错。

2.参考:

http://www.360doc.com/content/11/0408/14/987036_108161048.shtml

感觉还是需要:

单独新建一个logConfigurator的类,然后再调用,估计才行。

然后下载:

http://android-logging-log4j.googlecode.com/svn/trunk/src/test/java/de/mindpipe/android/logging/log4j/example/ConfigureLog4J.java

然后改为:

/* 
   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 de.mindpipe.android.logging.log4j.example;

import java.io.File;

import org.apache.log4j.Level;

import android.os.Environment;
import de.mindpipe.android.logging.log4j.LogConfigurator;

/**
 * Example how to to configure Log4J in Android.
 * Call {@link #configure()}} from your application's activity.
 * @author Rolf Kulemann
 */
public class ConfigureLog4J {
	public static void configure() {
		final LogConfigurator logConfigurator = new LogConfigurator();
		
		logConfigurator.setFileName(Environment.getExternalStorageDirectory() + File.separator + "myapp.log");
		// Set the root log level
		logConfigurator.setRootLevel(Level.DEBUG);
		// Set log level of a specific logger
		logConfigurator.setLevel("org.apache", Level.ERROR);
		logConfigurator.configure();
	}
}

加到项目中。

然后还得再去下载:

http://android-logging-log4j.googlecode.com/svn/trunk/src/test/java/de/mindpipe/android/logging/log4j/example/ExampleLog4J.java

也放到项目中。

结果弄了半天,还是不行。

3.参考了:

ERROR StatusLogger Unable to locate a logging implementation, using SimpleLogge

Log4j2 – configuring

还是无解。

4.换用之前在:

http://logging.apache.org/log4j/1.2/download.html

中下载的:

log4j-1.2.17.zip

然后解压缩得到的:

apache-log4j-1.2.17\log4j-1.2.17.jar

看看如何。

然后发现:

logConfigurator.setLevel("org.apache", Level.ERROR);

就不会挂掉,就可以正常执行了。

 

【总结】

此处,android中使用log4j出现:

ERROR StatusLogger Unable to locate a logging implementation, using SimpleLogger

的错误,

原因是:

2.0的log4j:log4j-api-2.0-beta9.jar

有问题。

解决办法:

换用旧的,1.0的:

log4j-1.2.17.zip的log4j-1.2.17.jar

即可解决问题。



发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量