2012-06-04 23 views
5

Tôi đã cố gắng sử dụng java.util.logging.logger để đăng nhập nội dung vào tệp trên sdcard. Bây giờ, tôi muốn trình ghi nhật ký sử dụng tệp cấu hình/thuộc tính ghi nhật ký mà tôi sẽ cung cấp trên sdcard.Cách cấu hình java.util.logging.logger trong android để sử dụng tệp thuộc tính ghi nhật ký được đặt trên sdcard?

Một cách mà tôi đã cố gắng là: -

Tôi đã cố gắng sử dụng logmanager và java.util.prefs.preferences, nhưng tôi nhận được BackingStoreException -> AccessPermission Exception perculating xuống một thông báo lỗi rằng .java/.userprefs/đường dẫn được biểu thị bởi str/prefs.xml không được tìm thấy.

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"); 
    } 

} 

Có thể ai đó cung cấp thông tin chi tiết nào không?

-Robin

Trả lời

7

Sau khi đi qua các tài liệu có sẵn và làm một số hit và thử nghiệm. Đây là cách hoạt động: -

 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 

Xin lưu ý rằng có hai bộ xử lý được sử dụng trong logging.properties đây. Một là trình xử lý tệp, đăng nhập vào tệp và tệp còn lại là com.android.internal.logging.AndroidHandler (có trách nhiệm hiển thị nhật ký chính xác trong logcat). Việc thêm com.android.internal.logging.AndroidHandler là cần thiết nếu bạn muốn xem nhật ký trong logcat. Nếu bạn không thêm trình xử lý này vào các thuộc tính ghi nhật ký, tất cả các bản ghi sẽ được ném vào logcat như sys.err (mức cảnh báo).

+0

Làm thế nào để tải tệp logging.properties? –

1

Bạn có thể có một cái nhìn @android-logging-log4j

+0

dữ liệu trong liên kết có vẻ đầy hứa hẹn. Tôi sẽ thử nó. Cảm ơn 8] – drulabs

+0

Tại thời điểm này, tôi không có ý định sử dụng log4j. Chỉ cần java util logger – Robin

Các vấn đề liên quan