이 블로그 검색

2015년 10월 26일 월요일

Log 작성 시 유용한 툴: Thread 객체

    private static void log(char level, String msg) {
        if(DEBUG) {
            Thread thread = Thread.currentThread();      // <- 이와같이 Thread 객체를 이용하면
            String threadName = thread.getName();        // 현재 Thread 이름
            String  fileName = thread.getStackTrace()[4].getFileName();  // 현재 로그가 찍히는 file 이름
            int nLine = thread.getStackTrace()[4].getLineNumber();     // 현재 로그가 찍히는 줄넘버 까지 다 알수 있음.

            if(fileName.length() > 20) {
                fileName = fileName.substring(0, 20);
            }

            String strLog = String.format("%s:%-10s[%-20s %5d]%s\n", TAG, threadName, fileName, nLine, msg);

            switch(level) {
                case 'D':
                    Log.d(TAG, strLog);
                    break;
                case 'E':
                    Log.e(TAG, strLog);
                    break;
                case 'W':
                    Log.w(TAG, strLog);
                    break;
                case 'I':
                    Log.i(TAG, strLog);
                    break;
                case 'V':
                    Log.v(TAG, strLog);
                    break;
            }

        }

댓글 없음:

댓글 쓰기