8

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

  1. setprop log.tag.<YOUR_LOG_TAG> <LEVEL>, 或者
  2. log.tag.<YOUR_LOG_TAG>=<LEVEL>在 /data/local.prop 中。

是否可以在应用程序中捆绑日志记录配置?我正在寻找与应用程序捆绑在一起并在加载时更改日志记录行为的等效配置文件logging.properties或配置文件。logback.xml目标是避免手动配置应用程序将在其上运行的每个设备。

4

2 回答 2

3

我最终使用了logback-android。如果有人提出更好的解决方案,我会将这个问题留得更久一些。

于 2012-10-30T17:01:58.227 回答
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 ");
于 2012-10-30T07:09:54.563 回答