2011-01-12 6 views
8

기본적으로 Log on android 플랫폼에는 콘솔 출력에 제한된 문자가 있습니다. 따라서 3000보다 약간 큰 메시지입니다. 따라서 메시지가 3000자를 초과하면 화면에 표시되지 않습니다.android에서 콘솔 출력을 늘리는 방법 Log class

public class Log { 
    private static int mode = android.util.Log.INFO; 

    public static void e(String tag, String msg, Throwable tr) { 
     if ((mode & android.util.Log.ERROR) <= 0) return; 
     android.util.Log.e(tag, msg, tr); 
    } 

    public static void e(String tag, String msg) { 
     if ((mode & android.util.Log.ERROR) <= 0) return; 
     android.util.Log.e(tag, msg); 
    } 

    public static void w(String tag, String msg) { 
     if ((mode & android.util.Log.WARN) <= 0) return; 
     android.util.Log.w(tag, msg); 
    } 

    public static void i(String tag, String msg) { 
     if ((mode & android.util.Log.INFO) <= 0) return; 
     android.util.Log.i(tag, msg); 
    } 

    public static void d(String tag, String msg) { 
     if ((mode & android.util.Log.DEBUG) <= 0) return; 

      int length = msg.length(); 
      int kind = 3000; 
      if (length >= kind) { 
       int count = length/kind; 
       int u = length % kind; 
       int i = 0; 
       for (i = 0; i < count; ++i) { 
        int start = i * kind; 
        int end = start + kind; 
        android.util.Log.d(tag, msg.substring(start, end)); 
       } 
       if (u != 0) { 
        int start = length - u; 
        int end = start + u; 
        android.util.Log.d(tag, msg.substring(start, end)); 
       } 
      } else { 
       android.util.Log.d(tag, msg); 
      } 
    } 

} 

이 문제에 더 나은 솔루션이 있습니까 :

나는이보다 더 나은 해결책을 발견하지?

+3

무엇을하려하십니까? – Vinay

+0

로그 출력 텍스트를 줄 바꿈하려고합니까? – Marcovena

+0

아니요, 기본 로그 온 안드로이드 플랫폼에는 콘솔 출력에 제한된 문자가 있습니다. 따라서 3000보다 약간 큰 메시지입니다. 따라서 메시지가 3000자를 초과하면 화면에 표시되지 않습니다. –

답변

9

큰 출력 (JSON 등) 을 수행 할 때 1,024 자 제한을 해결하는 방법은 for 루프를 만들어 청크 로깅하는 것입니다.

당신만큼 정교하지는 않을 수도 있지만, 눈이 쉽고 코드가 너무 복잡하지는 않습니다.

 String json = data.toString(); 
     int length = json.length(); 

     for(int i=0; i<length; i+=1024) 
     { 
      if(i+1024<length) 
       Log.d("JSON OUTPUT", json.substring(i, i+1024)); 
      else 
       Log.d("JSON OUTPUT", json.substring(i, length)); 
     } 
관련 문제