Skip to content

Commit 7756859

Browse files
author
abolfazlabbasi
committed
add Log Interceptor
1 parent c85b0b3 commit 7756859

File tree

7 files changed

+50
-25
lines changed

7 files changed

+50
-25
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ The FileLogger is a library for saving logs on Files with custom-formatter on ba
1313
- Compress and send logs(Email and messengers)
1414
- Startup logs
1515
- Retention Policy (Size, Count, Time to Live)
16+
- Log interception
1617

1718
## TODO
1819
1. Add C++ NDK support
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package abbasi.android.filelogger.sample
2+
3+
import abbasi.android.filelogger.file.LogLevel
4+
import abbasi.android.filelogger.interceptor.LogInterceptor
5+
6+
class AppLogInterceptor : LogInterceptor {
7+
override fun intercept(
8+
level: LogLevel,
9+
tag: String,
10+
message: String,
11+
e: Throwable?
12+
): String {
13+
return if (level is LogLevel.Info) {
14+
message
15+
} else {
16+
"****************************"
17+
}
18+
}
19+
}

app/src/main/java/abbasi/android/filelogger/sample/MainActivity.kt

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class MainActivity : AppCompatActivity() {
2525
val config = Config.Builder(it.path)
2626
.setDefaultTag("TAG")
2727
.setLogcatEnable(true)
28+
.setLogInterceptor(AppLogInterceptor())
2829
.setRetentionPolicy(RetentionPolicy.TimeToLive(durationInMillis = 1000 * 60 * 1))
2930
.setStartupData(
3031
mapOf(

filelogger/src/main/java/abbasi/android/filelogger/FileLogger.kt

+6-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import java.io.File
2020
object FileLogger {
2121

2222
private var initialized = false
23-
private var isEnable: Boolean = true
23+
var isEnable: Boolean = true
2424

2525
private var config: Config? = null
2626
private lateinit var retentionChecker: RetentionPolicyChecker
@@ -120,7 +120,11 @@ object FileLogger {
120120
throwable: Throwable? = null
121121
) = fileWriter.let { writer ->
122122
logQueue.postRunnable {
123-
val stringBuilder = StringBuilder("$logLevel/$tag: $msg\n")
123+
val message = config?.logInterceptor?.intercept(
124+
logLevel, tag.orEmpty(), msg.orEmpty(), throwable
125+
) ?: msg
126+
127+
val stringBuilder = StringBuilder("$logLevel/$tag: $message\n")
124128

125129
throwable?.let { exception ->
126130
exception.stackTrace.forEach { element ->
@@ -132,10 +136,6 @@ object FileLogger {
132136
}
133137
}
134138

135-
fun setEnable(isEnable: Boolean) {
136-
this.isEnable = isEnable
137-
}
138-
139139
fun deleteFiles() = checkBlock {
140140
i(msg = "FileLogger delete files called")
141141
logFileManager.deleteLogsDir(fileWriter.openedFilePath)

filelogger/src/main/java/abbasi/android/filelogger/config/Config.kt

+11-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package abbasi.android.filelogger.config
99
import abbasi.android.filelogger.config.Constance.Companion.DEFAULT_PATTERN
1010
import abbasi.android.filelogger.config.Constance.Companion.DEFAULT_TAG
1111
import abbasi.android.filelogger.config.Constance.Companion.LOGCAT_ENABLE
12+
import abbasi.android.filelogger.interceptor.LogInterceptor
1213

1314
class Config private constructor(
1415
val directory: String,
@@ -17,6 +18,7 @@ class Config private constructor(
1718
val dataFormatterPattern: String,
1819
val startupData: Map<String, String>?,
1920
val retentionPolicy: RetentionPolicy?,
21+
val logInterceptor: LogInterceptor?,
2022
) {
2123

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

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

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

4448
fun build() = Config(
45-
directory,
46-
defaultTag,
47-
logcatEnable,
48-
dataFormatterPattern,
49-
startupData,
50-
retentionPolicy,
49+
directory = directory,
50+
defaultTag = defaultTag,
51+
logcatEnable = logcatEnable,
52+
dataFormatterPattern = dataFormatterPattern,
53+
startupData = startupData,
54+
retentionPolicy = retentionPolicy,
55+
logInterceptor = logInterceptor
5156
)
5257
}
5358
}

filelogger/src/main/java/abbasi/android/filelogger/file/LogLevel.kt

+5-13
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,18 @@
66

77
package abbasi.android.filelogger.file
88

9-
internal sealed class LogLevel {
9+
sealed class LogLevel {
1010
object Info : LogLevel()
1111
object Error : LogLevel()
1212
object Warning : LogLevel()
1313
object Debug : LogLevel()
1414

1515
override fun toString(): String {
1616
return when (this) {
17-
is Info -> {
18-
"I"
19-
}
20-
is Error -> {
21-
"E"
22-
}
23-
is Warning -> {
24-
"W"
25-
}
26-
is Debug -> {
27-
"D"
28-
}
17+
is Info -> "I"
18+
is Error -> "E"
19+
is Warning -> "W"
20+
is Debug -> "D"
2921
}
3022
}
3123
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package abbasi.android.filelogger.interceptor
2+
3+
import abbasi.android.filelogger.file.LogLevel
4+
5+
interface LogInterceptor {
6+
fun intercept(level: LogLevel, tag: String, message: String, e: Throwable?): String
7+
}

0 commit comments

Comments
 (0)