你可以在这里使用Java日志记录器,并进行一些自定义设置。
ConsoleLogHandler.java
package com.sony.evc.vis.system.util.logger;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import android.util.Log;
public class ConsoleLogHandler extends Handler
{
@Override
public void close()
{}
@Override
public void flush()
{}
@Override
public void publish(LogRecord record)
{
String className = record.getLoggerName() + ": "
+ simplifyClassName(record.getSourceClassName());
int threadID = record.getThreadID();
String methodName = record.getSourceMethodName();
String msg = record.getMessage();
int level = record.getLevel().intValue();
logMessage(className, threadID, methodName, msg, level, record.getThrown());
}
private String simplifyClassName(String sourceClassName)
{
String fullClassName[] = sourceClassName.split("\\.");
return fullClassName[fullClassName.length - 1];
}
private void logMessage(String className, int threadID, String methodName, String msg,
int level, Throwable throwable)
{
if (level == Level.FINE.intValue()) {
Log.d(className, "ThreadID:" + threadID + "," + methodName + "()," + msg);
}
else if (level == Level.INFO.intValue()) {
Log.i(className, "ThreadID:" + threadID + "," + methodName + "()," + msg);
}
else if (level == Level.WARNING.intValue()) {
Log.w(className, "ThreadID:" + threadID + "," + methodName + "()," + msg, throwable);
}
else if (level == Level.SEVERE.intValue()) {
Log.e(className, "ThreadID:" + threadID + "," + methodName + "()," + msg, throwable);
}
}
}
创建一个名为
LoggerFactory.java 的类。
package com.sony.evc.vis.system.util.logger;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
public class LoggerFactory {
public static Logger Log;
private static ConsoleLogHandler logHandler;
public static boolean FINE;
public static boolean INFO;
public static boolean WARN;
public static void init(String version, String globalDebugLevel,
String appDebugLevel) {
Log = Logger.getLogger(version);
Level debuglevel = LogLevel.setDebugLevel(globalDebugLevel,
appDebugLevel);
if (debuglevel.equals(Level.ALL) || debuglevel.equals(Level.FINE)
|| debuglevel.equals(Level.FINER)
|| debuglevel.equals(Level.FINEST)) {
FINE = INFO = WARN = true;
} else if (debuglevel.equals(Level.INFO)) {
INFO = WARN = true;
} else if (debuglevel.equals(Level.WARNING)) {
WARN = true;
}
Log.setLevel(debuglevel);
Log.setUseParentHandlers(false);
if (logHandler == null)
logHandler = new ConsoleLogHandler();
for (Handler handler : Log.getHandlers())
Log.removeHandler(handler);
Log.addHandler(logHandler);
}
}
创建枚举类型
LogLevel.java。
package com.sony.evc.vis.system.util.logger;
import java.util.logging.Level;
public enum LogLevel {
DEBUG, INFO, WARNING, ERROR, NO_LOG, DISABLE;
public static Level setDebugLevel(String sGlobalDebugLevel,
String sAppDebugLevel) {
String strGlobalDebugLevel = sGlobalDebugLevel.trim().toUpperCase();
String strAppDebugLevel = sAppDebugLevel.trim().toUpperCase();
LogLevel globalDebugLevel = LogLevel.valueOf(strGlobalDebugLevel);
LogLevel appDebugLevel = LogLevel.valueOf(strAppDebugLevel);
LogLevel logLevel = globalDebugLevel == LogLevel.DISABLE ? appDebugLevel
: globalDebugLevel;
switch (logLevel) {
case DEBUG:
return Level.FINE;
case INFO:
return Level.INFO;
case WARNING:
return Level.WARNING;
case ERROR:
return Level.SEVERE;
case NO_LOG:
return Level.OFF;
case DISABLE:
return Level.OFF;
default:
return Level.INFO;
}
}
}
您还需要更新Strings.xml文件
<!--
:-Accepted values for global_debuglevel are: "debug", "info", "warning", "error"(in increasing order of priority),No_Log
and Disable.
:-Setting any other value will set "info" level
-->
<string name="global_debuglevel">debug</string>
<!--
:-Accepted values for app_debuglevel are: "debug", "info", "warning", "error"(in increasing order of priority)
and No_Log
:-Setting any other value will set "info" level
-->
<string name="app_debuglevel">debug</string>
从应用程序开始,只需初始化Logger即可
String service_version = getString(R.string.service_version)
String global_debugLevel = getString(R.string.global_debuglevel)
String service_debugLevel = getString(R.string.service_debuglevel)
LoggerFactory.init(service_version, global_debugLevel,
service_debugLevel)
打印日志的代码如下:
Log.info(" message ");
Log.fine(" message ");
Log.severe(" message ");
Log.warning(" message ");