Skip to content

Commit

Permalink
add Log Interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
abolfazlabbasi committed May 28, 2024
1 parent c85b0b3 commit 7756859
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 25 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The FileLogger is a library for saving logs on Files with custom-formatter on ba
- Compress and send logs(Email and messengers)
- Startup logs
- Retention Policy (Size, Count, Time to Live)
- Log interception

## TODO
1. Add C++ NDK support
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package abbasi.android.filelogger.sample

import abbasi.android.filelogger.file.LogLevel
import abbasi.android.filelogger.interceptor.LogInterceptor

class AppLogInterceptor : LogInterceptor {
override fun intercept(
level: LogLevel,
tag: String,
message: String,
e: Throwable?
): String {
return if (level is LogLevel.Info) {
message
} else {
"****************************"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class MainActivity : AppCompatActivity() {
val config = Config.Builder(it.path)
.setDefaultTag("TAG")
.setLogcatEnable(true)
.setLogInterceptor(AppLogInterceptor())
.setRetentionPolicy(RetentionPolicy.TimeToLive(durationInMillis = 1000 * 60 * 1))
.setStartupData(
mapOf(
Expand Down
12 changes: 6 additions & 6 deletions filelogger/src/main/java/abbasi/android/filelogger/FileLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import java.io.File
object FileLogger {

private var initialized = false
private var isEnable: Boolean = true
var isEnable: Boolean = true

private var config: Config? = null
private lateinit var retentionChecker: RetentionPolicyChecker
Expand Down Expand Up @@ -120,7 +120,11 @@ object FileLogger {
throwable: Throwable? = null
) = fileWriter.let { writer ->
logQueue.postRunnable {
val stringBuilder = StringBuilder("$logLevel/$tag: $msg\n")
val message = config?.logInterceptor?.intercept(
logLevel, tag.orEmpty(), msg.orEmpty(), throwable
) ?: msg

val stringBuilder = StringBuilder("$logLevel/$tag: $message\n")

throwable?.let { exception ->
exception.stackTrace.forEach { element ->
Expand All @@ -132,10 +136,6 @@ object FileLogger {
}
}

fun setEnable(isEnable: Boolean) {
this.isEnable = isEnable
}

fun deleteFiles() = checkBlock {
i(msg = "FileLogger delete files called")
logFileManager.deleteLogsDir(fileWriter.openedFilePath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package abbasi.android.filelogger.config
import abbasi.android.filelogger.config.Constance.Companion.DEFAULT_PATTERN
import abbasi.android.filelogger.config.Constance.Companion.DEFAULT_TAG
import abbasi.android.filelogger.config.Constance.Companion.LOGCAT_ENABLE
import abbasi.android.filelogger.interceptor.LogInterceptor

class Config private constructor(
val directory: String,
Expand All @@ -17,6 +18,7 @@ class Config private constructor(
val dataFormatterPattern: String,
val startupData: Map<String, String>?,
val retentionPolicy: RetentionPolicy?,
val logInterceptor: LogInterceptor?,
) {

class Builder(private val directory: String) {
Expand All @@ -25,11 +27,13 @@ class Config private constructor(
private var dataFormatterPattern: String = DEFAULT_PATTERN
private var startupData: Map<String, String>? = null
private var retentionPolicy: RetentionPolicy? = null
private var logInterceptor: LogInterceptor? = null

fun setDefaultTag(defaultTag: String) = apply { this.defaultTag = defaultTag }
fun setLogcatEnable(logcatEnable: Boolean) = apply { this.logcatEnable = logcatEnable }
fun setStartupData(startupData: Map<String, String>?) = apply { this.startupData = startupData }
fun setRetentionPolicy(retentionPolicy: RetentionPolicy) = apply { this.retentionPolicy = retentionPolicy }
fun setLogInterceptor(logInterceptor: LogInterceptor) = apply { this.logInterceptor = logInterceptor }

fun setDataFormatterPattern(pattern: String) = apply {
this.dataFormatterPattern = pattern.replace("/", "-")
Expand All @@ -42,12 +46,13 @@ class Config private constructor(
}

fun build() = Config(
directory,
defaultTag,
logcatEnable,
dataFormatterPattern,
startupData,
retentionPolicy,
directory = directory,
defaultTag = defaultTag,
logcatEnable = logcatEnable,
dataFormatterPattern = dataFormatterPattern,
startupData = startupData,
retentionPolicy = retentionPolicy,
logInterceptor = logInterceptor
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,18 @@

package abbasi.android.filelogger.file

internal sealed class LogLevel {
sealed class LogLevel {
object Info : LogLevel()
object Error : LogLevel()
object Warning : LogLevel()
object Debug : LogLevel()

override fun toString(): String {
return when (this) {
is Info -> {
"I"
}
is Error -> {
"E"
}
is Warning -> {
"W"
}
is Debug -> {
"D"
}
is Info -> "I"
is Error -> "E"
is Warning -> "W"
is Debug -> "D"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package abbasi.android.filelogger.interceptor

import abbasi.android.filelogger.file.LogLevel

interface LogInterceptor {
fun intercept(level: LogLevel, tag: String, message: String, e: Throwable?): String
}

0 comments on commit 7756859

Please sign in to comment.