2012-06-04 6 views
5

나는 java.util.logging.logger를 사용하여 sdcard의 파일에 물건을 로깅하려고 노력했다. 이제 로거가 sdcard에 제공 할 로깅 구성/등록 정보 파일을 사용하도록합니다. 내가 시도하고있다sdcard에있는 로깅 속성 파일을 사용하도록 android에서 java.util.logging.logger를 구성하는 방법은 무엇입니까?

한 가지 방법은 다음과 같습니다 -

내가 LogManager의 및 java.util.prefs.preferences를 사용하는 것을 시도하고있다,하지만 난 한 BackingStoreException 얻을 -> AccessPermission 예외이 아래로 perculating .java/.userprefs/path가 str/prefs.xml으로 표시되는 오류 메시지가 없습니다.

private void setLoggingProperties(File logProperties) throws Exception { 
    try { 
    if(logProperties!=null && logProperties.isFile() && logProperties.exists()) { 
     String str = logProperties.getAbsolutePath(); 
     MyLogger.v(TAG,"Log Properties file path: " + str); 
     if(str!=null && str.length()>=0) { 
      Preferences logPropPref = Preferences.userNodeForPackage(SSCService.class); 

      if(logPropPref!=null) { 
       String path = logPropPref.get(LOG_CONFIG_FILE_KEY,""); 
       if(path.equals(str)) { 
        SirfLogger.v(TAG,"No need to set config for log"); 
        return; 
       } 
       MyLogger.v(TAG,"Setting log properties: " + str); 
       logPropPref.put(LOG_CONFIG_FILE_KEY, str); 
       logPropPref.flush(); 
      } 
      LogManager lManager = LogManager.getLogManager(); 
      if(lManager!=null) { 
       lManager.readConfiguration(); 
      } 

     } 

    } 
    } catch(Exception ex) { 
     MyLogger.v(TAG, "Exception setting log properties: " + ex.toString() + " , ignoring"); 
    } 

} 

누구나 통찰력을 주시기 바랍니다.

답변

7

@ 모양을 가질 수있다. 이것은 어떻게 작동 : -

 static final String LOGGER_NAME = "com.robin.mylogger" 
     LogManager lManager = LogManager.getLogManager(); 
     FileInputStream is = new FileInputStream(logProperties); 
     if(lManager!=null) { 
      lManager.readConfiguration(is); 

     } 
     mLoggerInstance = Logger.getLogger(LOGGER_NAME); 
     if(mLoggerInstance!=null) 
      LogManager.getLogManager().addLogger(mLoggerInstance); 

logging.properties

   ############################################################ 
       # Default Logging Configuration File 
# 
# You can use a different file by specifying a filename 
# with the java.util.logging.config.file system property. 
# For example java -Djava.util.logging.config.file=myfile 
############################################################ 

############################################################ 
# Global properties 
############################################################ 

# "handlers" specifies a comma separated list of log Handler 
# classes. These handlers will be installed during VM startup. 
# Note that these classes must be on the system classpath. 
# By default we only configure a ConsoleHandler, which will only 
# show messages at the INFO and above levels. 
#handlers= java.util.logging.ConsoleHandler 

# To also add the FileHandler, use the following line instead. 
handlers= java.util.logging.FileHandler, com.android.internal.logging.AndroidHandler 

# Default global logging level. 
# This specifies which kinds of events are logged across 
# all loggers. For any given facility this global level 
# can be overriden by a facility specific level 
# Note that the ConsoleHandler also has a separate level 
# setting to limit messages printed to the console. 
.level= FINEST 

############################################################ 
# Handler specific properties. 
# Describes specific configuration info for Handlers. 
############################################################ 

# default file output is in user's home directory. 
java.util.logging.FileHandler.pattern = /mnt/sdcard/csr/logs/test.log 
java.util.logging.FileHandler.limit = 5000000 
java.util.logging.FileHandler.count = 1 
java.util.logging.FileHandler.level = FINEST 
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter 

# Limit the message that are printed on the console to INFO and above. 
#java.util.logging.ConsoleHandler.level = FINEST 
#java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter 

com.android.internal.logging.AndroidHandler.level = FINEST 
com.android.internal.logging.AndroidHandler.formatter = java.util.logging.SimpleFormatter 


############################################################ 
# Facility specific properties. 
# Provides extra control for each logger. 
############################################################ 

# For example, set the com.xyz.foo logger to only log SEVERE 
# messages: 
#com.robin.mylogger.level = FINEST 

여기 logging.properties에 사용되는 두 개의 핸들러가 있음을 유의하시기 바랍니다. 하나는 파일 핸들러이며 파일에 로그온하고 다른 하나는 com.android.internal.logging.AndroidHandler입니다 (logcat에 로그를 적절하게 표시 할 책임이 있습니다). logcat의 로그를보고 싶다면 com.android.internal.logging.AndroidHandler를 추가해야합니다. 로깅 속성에이 핸들러를 추가하지 않으면 logcat에서 모든 로그가 sys.err (경고 레벨)로 처리됩니다.

+0

logging.properties 파일을로드하는 방법은 무엇입니까? –

1

-Robin 사용 가능한 문서를 통과하고 일부 히트 및 시험을 수행 한 후 android-logging-log4j

+0

링크의 데이터가 유망 해 보입니다. 나는 그것을 시도 할 것이다. 감사합니다 8] – drulabs

+0

이 시점에서, 나는 log4j를 사용하려고하지 않습니다. Java 유틸리티 로거 – Robin

관련 문제