-
Notifications
You must be signed in to change notification settings - Fork 0
[FEAT] 안드로이드 로그 디버그모드 관리 #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| package com.alarmy.near.utils.logger | ||
|
|
||
| import android.util.Log | ||
| import com.alarmy.near.BuildConfig | ||
|
|
||
| private const val TAG = "Near" | ||
| private const val STACK_TRACE_INDEX = 5 | ||
|
|
||
| /** | ||
| * 호출자 정보를 포함한 로그 메시지를 생성합니다 | ||
| * 스택 트레이스 추출에 실패하면 원본 메시지를 반환합니다 | ||
| */ | ||
| private fun buildLogMessage(message: String): String { | ||
| return try { | ||
| val stackTrace = Thread.currentThread().stackTrace | ||
|
|
||
| // 실제 호출자를 찾기 위해 스택을 순회 | ||
| for (i in 4 until stackTrace.size) { | ||
| val element = stackTrace[i] | ||
| val fileName = element.fileName ?: continue | ||
| val methodName = element.methodName ?: continue | ||
|
|
||
| // 로그 관련 메서드들을 건너뛰고 실제 호출자 찾기 | ||
| if (!methodName.startsWith("log") && | ||
| !methodName.contains("\$default") && | ||
| !fileName.contains("Log") | ||
| ) { | ||
| val cleanFileName = | ||
| fileName | ||
| .replace(".java", "") | ||
| .replace(".kt", "") | ||
|
|
||
| return "[$cleanFileName::$methodName (${element.fileName}:${element.lineNumber})] $message" | ||
| } | ||
| } | ||
|
|
||
| // 찾지 못하면 기본 인덱스 사용 | ||
| if (stackTrace.size > STACK_TRACE_INDEX) { | ||
| val element = stackTrace[STACK_TRACE_INDEX] | ||
| val fileName = | ||
| element.fileName | ||
| ?.replace(".java", "") | ||
| ?.replace(".kt", "") | ||
| ?: "Unknown" | ||
|
|
||
| "[$fileName::${element.methodName} (${element.fileName}:${element.lineNumber})] $message" | ||
| } else { | ||
| message | ||
| } | ||
| } catch (exception: Exception) { | ||
| // 스택 트레이스 추출 실패 시 원본 메시지 반환 | ||
| message | ||
| } | ||
|
||
| } | ||
|
|
||
| /** | ||
| * 디버그 모드인지 확인합니다 | ||
| */ | ||
| private fun isLoggingEnabled(): Boolean = BuildConfig.DEBUG | ||
|
|
||
| // Verbose 로그 | ||
| fun logv( | ||
| message: String, | ||
| tag: String = TAG, | ||
| ) { | ||
| if (!isLoggingEnabled()) return | ||
| Log.v(tag, buildLogMessage(message)) | ||
| } | ||
|
|
||
| // Debug 로그 | ||
| fun logd( | ||
| message: String, | ||
| tag: String = TAG, | ||
| ) { | ||
| if (!isLoggingEnabled()) return | ||
| Log.d(tag, buildLogMessage(message)) | ||
| } | ||
|
|
||
| // Info 로그 | ||
| fun logi( | ||
| message: String, | ||
| tag: String = TAG, | ||
| ) { | ||
| if (!isLoggingEnabled()) return | ||
| Log.i(tag, buildLogMessage(message)) | ||
| } | ||
|
|
||
| // Warning 로그 | ||
| fun logw( | ||
| message: String, | ||
| tag: String = TAG, | ||
| ) { | ||
| if (!isLoggingEnabled()) return | ||
| Log.w(tag, buildLogMessage(message)) | ||
| } | ||
|
|
||
| // Error 로그 | ||
| fun loge( | ||
| message: String, | ||
| tag: String = TAG, | ||
| ) { | ||
| if (!isLoggingEnabled()) return | ||
| Log.e(tag, buildLogMessage(message)) | ||
| } | ||
|
|
||
| // Error 로그 (이름 포함) | ||
| fun loge( | ||
| name: String, | ||
| message: String, | ||
| tag: String = TAG, | ||
| ) { | ||
| if (!isLoggingEnabled()) return | ||
| Log.e(tag, buildLogMessage("$name: $message")) | ||
| } | ||
|
|
||
| // Error 로그 (예외 포함) | ||
| fun loge( | ||
| message: String, | ||
| throwable: Throwable, | ||
| tag: String = TAG, | ||
| ) { | ||
| if (!isLoggingEnabled()) return | ||
| Log.e(tag, buildLogMessage(message), throwable) | ||
| } | ||
|
|
||
| // Error 로그 (이름과 예외 모두 포함) | ||
| fun loge( | ||
| name: String, | ||
| message: String, | ||
| throwable: Throwable, | ||
| tag: String = TAG, | ||
| ) { | ||
| if (!isLoggingEnabled()) return | ||
| Log.e(tag, buildLogMessage("$name: $message"), throwable) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
꼼꼼하게 처리주셨네요👍
예외가 발생하면 어떤 상황에서 발생할까요? 만약 예외가 발생하고 catch되어 message로 나오는 상황이면 어떤 예외인지,어쩌면 예외가 발생했는지도 모르게 다른 부분을 디버깅 하는 상황이 생길 수 있을 것 같습니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
코멘트 감사합니다 :D
우선 예외가 발생하는 경우는 로그에 해당 파일까지 남기는 형태로 작성하였는데, 파일을 찾는 방식이
앱의 스택 트레이스를 순회하면서 탐색하고 있습니다! 그래서 이 순회과정에서 문제가 생길 수 있어 try~catch로 예외처리를 두었어요
예외처리를 두긴 하였지만 웬만하면 에러가 발생 안하지 않을까.. 싶습니다