Skip to content

Commit

Permalink
add delete logs method
Browse files Browse the repository at this point in the history
fix methods style
add Constance object
  • Loading branch information
aabolfazl committed Mar 6, 2022
1 parent 339c91d commit 9c6e9a0
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 28 deletions.
53 changes: 34 additions & 19 deletions filelogger/src/main/java/abbasi/android/filelogger/FileLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ object FileLogger {
private var initialized = false
private var isEnable: Boolean = true

private lateinit var config: Config
private var config: Config? = null
private var fileWriter: FileWriter? = null
private var logQueue: ThreadQueue = ThreadQueue()

Expand All @@ -31,41 +31,53 @@ object FileLogger {
initialized = true
}

fun i(tag: String? = config.defaultTag, msg: String) = checkBlock {
if (config.logcatEnable) {
fun i(tag: String? = config?.defaultTag, msg: String) = checkBlock {
if (config?.logcatEnable == true) {
Log.i(tag, msg)
}

postLog(LogLevel.Info, tag, msg)
}

fun e(tag: String? = config.defaultTag, msg: String, throwable: Throwable? = null) =
checkBlock {
if (config.logcatEnable) {
Log.e(tag, msg, throwable)
}

postLog(logLevel = LogLevel.Error, tag = tag, msg = msg, throwable = throwable)
fun e(
tag: String? = config?.defaultTag,
msg: String,
throwable: Throwable? = null
) = checkBlock {
if (config?.logcatEnable == true) {
Log.e(tag, msg, throwable)
}

fun e(tag: String? = config.defaultTag, throwable: Throwable) = checkBlock {
if (config.logcatEnable) {
postLog(logLevel = LogLevel.Error, tag = tag, msg = msg, throwable = throwable)
}

fun e(
tag: String? = config?.defaultTag,
throwable: Throwable
) = checkBlock {
if (config?.logcatEnable == true) {
Log.e(tag, "", throwable)
}

postLog(logLevel = LogLevel.Error, tag = tag, throwable = throwable)
}

fun w(tag: String? = config.defaultTag, msg: String) = checkBlock {
if (config.logcatEnable) {
fun w(
tag: String? = config?.defaultTag,
msg: String
) = checkBlock {
if (config?.logcatEnable == true) {
Log.w(tag, msg)
}

postLog(logLevel = LogLevel.Warning, tag = tag, msg = msg)
}

fun d(tag: String? = config.defaultTag, msg: String) = checkBlock {
if (config.logcatEnable) {
fun d(
tag: String? = config?.defaultTag,
msg: String
) = checkBlock {
if (config?.logcatEnable == true) {
Log.d(tag, msg)
}

Expand All @@ -74,7 +86,7 @@ object FileLogger {

private fun postLog(
logLevel: LogLevel,
tag: String? = config.defaultTag,
tag: String? = config?.defaultTag,
msg: String? = "",
throwable: Throwable? = null
) = fileWriter?.let { writer ->
Expand All @@ -95,6 +107,11 @@ object FileLogger {
this.isEnable = isEnable
}

fun deleteFiles() = checkBlock {
i(msg = "FileLogger delete files called")
fileWriter?.deleteLogsDir()
}

private fun checkBlock(block: () -> Unit) {
if (initialized && isEnable) {
block()
Expand All @@ -103,8 +120,6 @@ object FileLogger {
javaClass.simpleName,
"SDK not initialized maybe forgot call FileLogger.init(config: Config)"
)
} else {
Log.e(javaClass.simpleName, "SDK not enable!")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

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

class Config private constructor(
val directory: String,
val defaultTag: String,
Expand All @@ -14,9 +18,9 @@ class Config private constructor(
) {

class Builder(private val directory: String) {
private var defaultTag: String = "FileLogger"
private var logcatEnable: Boolean = true
private var dataFormatterPattern: String = "dd-MM-yyyy-HH:mm:ss"
private var defaultTag: String = DEFAULT_TAG
private var logcatEnable: Boolean = LOGCAT_ENABLE
private var dataFormatterPattern: String = DEFAULT_PATTERN

fun setDefaultTag(defaultTag: String) = apply { this.defaultTag = defaultTag }
fun setLogcatEnable(logcatEnable: Boolean) = apply { this.logcatEnable = logcatEnable }
Expand All @@ -26,7 +30,7 @@ class Config private constructor(
.trim()

if (pattern.isEmpty().or(pattern.contains("/"))) {
this.dataFormatterPattern = "dd-MM-yyyy-HH:mm:ss"
this.dataFormatterPattern = DEFAULT_PATTERN
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
*
* Copyright (c) 2022 Abolfazl Abbasi
*
* */

package abbasi.android.filelogger.config

class Constance {
companion object {
const val DIRECTORY = "/fileLogs"
const val DEFAULT_TAG = "FileLogger"
const val DEFAULT_PATTERN = "dd-MM-yyyy-HH:mm:ss"
const val LOGCAT_ENABLE = true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

package abbasi.android.filelogger.file

import abbasi.android.filelogger.config.Constance.Companion.DIRECTORY
import abbasi.android.filelogger.time.FastDateFormat
import android.util.Log
import java.io.File
Expand All @@ -14,7 +15,7 @@ import java.io.OutputStreamWriter
import java.util.*

internal class FileWriter(
directory: String, dataFormatterPattern: String
private val directory: String, dataFormatterPattern: String
) {
private var streamWriter: OutputStreamWriter? = null
private var dateFormat: FastDateFormat? = null
Expand All @@ -24,13 +25,12 @@ internal class FileWriter(
dateFormat = FastDateFormat.getInstance(dataFormatterPattern, Locale.US)
try {
val file = File(directory)
val logDir = File(file.absolutePath + "/fileLogs")
val logDir = File(file.absolutePath + DIRECTORY)
if (logDir.exists().not()) {
logDir.mkdirs()
}

logFile =
File(logDir, dateFormat?.format(System.currentTimeMillis()).toString() + ".txt")
logFile = File(logDir, "${dateFormat?.format(System.currentTimeMillis())}.txt")
} catch (e: Exception) {
e.printStackTrace()
}
Expand Down Expand Up @@ -58,7 +58,14 @@ internal class FileWriter(
}
}

fun deleteCurrentFile() {
fun deleteLogsDir() {
val currentFile = File(directory)
val logDir = File(currentFile.absolutePath + DIRECTORY)

logDir.listFiles()?.filter {
it.absolutePath != logFile?.absolutePath
}?.forEach {
it.delete()
}
}
}

0 comments on commit 9c6e9a0

Please sign in to comment.