Android:如何更改默认日志记录级别?

时间:2012-10-30 05:51:11

标签: android logging

根据the documentation,只有两种方法可以将默认日志记录级别从INFO更改为较低级别(例如DEBUG):

  1. setprop log.tag.<YOUR_LOG_TAG> <LEVEL>
  2. log.tag.<YOUR_LOG_TAG>=<LEVEL>位于/data/local.prop。
  3. 是否可以在应用程序中捆绑日志记录配置?我正在寻找与应用程序捆绑在一起的logging.propertieslogback.xml等效配置文件并在加载时更改日志记录行为。目标是避免手动配置应用程序将运行的每个设备。

2 个答案:

答案 0 :(得分:3)

我最终使用logback-android。如果有人提出更好的解决方案,我会将此问题保持更长时间。

答案 1 :(得分:0)

你也可以在这里使用java logger和一些自定义..

<强> 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()
    {}

    // filter logs and publish them
    @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());
    }

    // simplify class name from full applied package name to simple class
    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;
    // Global constants to give programmer choice to add if condition before
    // logging a code.
    public static boolean FINE;
    public static boolean INFO;
    public static boolean WARN;

    // initialise the logger
    public static void init(String version, String globalDebugLevel,
            String appDebugLevel) {

        Log = Logger.getLogger(version);

        // debug level settings
        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);

        // logger send its msg to several handlers by default, we have to
        // disable that
        Log.setUseParentHandlers(false);

        // avoid recreation of handler in case activities onCreate() is called
        // again but logHandler is not
        // garbage collected
        if (logHandler == null)
            logHandler = new ConsoleLogHandler();

        // remove previously assigned handlers. This is very important and
        // failing to do so may result in
        // duplication of same logs
        for (Handler handler : Log.getHandlers())
            Log.removeHandler(handler);

        // set this loggers handler and make sure that only one instance is
        // associated with it
        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;
    /*
     * set log-debugLevel as per requirement. Global level is given preference
     * over app and service levels. If global level is disabled, other
     * levels are considered
     */
    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 ");