-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from Astra-Interactive/better-logging
add better logging system
- Loading branch information
Showing
5 changed files
with
159 additions
and
44 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
core-bukkit/src/main/kotlin/ru/astrainteractive/astralibs/logging/BukkitLogger.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package ru.astrainteractive.astralibs.logging | ||
|
||
import org.bukkit.Bukkit | ||
|
||
class BukkitLogger( | ||
override val TAG: String | ||
) : Logger by JUtiltLogger( | ||
TAG = TAG, | ||
logger = Bukkit.getLogger() | ||
) |
120 changes: 96 additions & 24 deletions
120
core/src/main/kotlin/ru/astrainteractive/astralibs/logging/JUtilFileLogger.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,115 @@ | ||
package ru.astrainteractive.astralibs.logging | ||
|
||
import java.io.File | ||
import java.time.LocalDateTime | ||
import java.time.format.DateTimeFormatter | ||
|
||
/** | ||
* This logger will write all logs inside [folder] | ||
*/ | ||
class JUtilFileLogger( | ||
override val tag: String, | ||
override val folder: File, | ||
private val logger: java.util.logging.Logger = java.util.logging.Logger.getGlobal() | ||
private val folder: File, | ||
private val instance: Logger | ||
) : Logger { | ||
override val TAG: String = instance.TAG | ||
|
||
override fun warning(tag: String, msg: String, logInFile: Boolean) { | ||
logger.warning("[$tag]: $msg") | ||
if (logInFile) { | ||
logInFile(tag, msg) | ||
} | ||
/** | ||
* Returns current time in HH:mm:ss format | ||
*/ | ||
private fun getTime(): String { | ||
val now = LocalDateTime.now() | ||
val formatter = DateTimeFormatter.ofPattern("HH:mm:ss") | ||
return formatter.format(now) | ||
} | ||
|
||
override fun error(tag: String, msg: String, logInFile: Boolean) { | ||
logger.severe("[$tag]: $msg") | ||
if (logInFile) { | ||
logInFile(tag, msg) | ||
} | ||
/** | ||
* Returns current date in yyyy-MM-dd format | ||
*/ | ||
private fun getDate(): String { | ||
val now = LocalDateTime.now() | ||
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd") | ||
return formatter.format(now) | ||
} | ||
|
||
override fun debug(logMessage: () -> String) { | ||
instance.debug(logMessage) | ||
logInFile(logMessage) | ||
} | ||
|
||
override fun error(logMessage: () -> String) { | ||
instance.error(logMessage) | ||
logInFile(logMessage) | ||
} | ||
|
||
override fun info(logMessage: () -> String) { | ||
instance.info(logMessage) | ||
logInFile(logMessage) | ||
} | ||
|
||
override fun verbose(logMessage: () -> String) { | ||
instance.verbose(logMessage) | ||
logInFile(logMessage) | ||
} | ||
|
||
override fun info(tag: String, msg: String, logInFile: Boolean) { | ||
logger.info("[$tag]: $msg") | ||
if (logInFile) { | ||
logInFile(tag, msg) | ||
override fun warn(logMessage: () -> String) { | ||
instance.warn(logMessage) | ||
logInFile(logMessage) | ||
} | ||
|
||
override fun error(error: Throwable?, logMessage: () -> String) { | ||
instance.error(logMessage) | ||
error?.stackTraceToString()?.let { stackTraceToString -> | ||
logInFile { stackTraceToString } | ||
} | ||
logInFile(logMessage) | ||
} | ||
|
||
private fun logInFile(tag: String, msg: String) { | ||
private fun getLogFile(): File { | ||
if (!folder.exists()) { | ||
folder.mkdirs() | ||
} | ||
val data = Logger.getDate() | ||
val time = Logger.getTime() | ||
val file = File(folder, "$data.log") | ||
if (!folder.exists()) folder.mkdirs() | ||
if (!file.exists()) file.createNewFile() | ||
file.appendText("[$time] [$tag]: $msg\n") | ||
val data = getDate() | ||
val lastIndex = folder.listFiles().orEmpty() | ||
.filter { it.nameWithoutExtension.startsWith(data) } | ||
.maxOfOrNull { | ||
if (!it.nameWithoutExtension.contains("-")) { | ||
0 | ||
} else { | ||
it.nameWithoutExtension.split("-").lastOrNull()?.toIntOrNull() ?: 0 | ||
} | ||
} | ||
val lastFile = File(folder, "$data-$lastIndex.log") | ||
if (!lastFile.exists()) { | ||
lastFile.createNewFile() | ||
return lastFile | ||
} | ||
val fileSizeMegaBytes = lastFile.length() / 1024f / 1024f | ||
return if (fileSizeMegaBytes > MAX_FILE_SIZE_MB) { | ||
val newFile = File(folder, "$data-$lastIndex.log") | ||
newFile.createNewFile() | ||
newFile | ||
} else { | ||
lastFile | ||
} | ||
} | ||
|
||
private fun logInFile(logMessage: () -> String) { | ||
val time = getTime() | ||
val file = getLogFile() | ||
file.appendText("[$time] [$TAG]: ${logMessage.invoke()}\n") | ||
} | ||
|
||
companion object { | ||
private const val MAX_FILE_SIZE_MB = 8 * 1024 * 1024 | ||
|
||
/** | ||
* Convert default [Logger] into [JUtilFileLogger] | ||
*/ | ||
fun Logger.toFileLogger(folder: File): JUtilFileLogger { | ||
check(this !is JUtilFileLogger) { | ||
"${this::class.java} already JUtilFileLogger!" | ||
} | ||
return JUtilFileLogger(folder, this) | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
core/src/main/kotlin/ru/astrainteractive/astralibs/logging/JUtiltLogger.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package ru.astrainteractive.astralibs.logging | ||
|
||
import java.util.logging.Level | ||
import java.util.logging.Logger as JLogger | ||
|
||
/** | ||
* This is default implementation with [JLogger] | ||
*/ | ||
class JUtiltLogger( | ||
override val TAG: String, | ||
private val logger: JLogger = JLogger.getLogger(TAG) | ||
) : Logger { | ||
|
||
override fun error(logMessage: () -> String) { | ||
logger.log(Level.SEVERE, logMessage) | ||
} | ||
|
||
override fun error(error: Throwable?, logMessage: () -> String) { | ||
logger.log(Level.SEVERE, error, logMessage) | ||
} | ||
|
||
override fun info(logMessage: () -> String) { | ||
logger.info(logMessage) | ||
} | ||
|
||
override fun verbose(logMessage: () -> String) { | ||
logger.log(Level.FINE, logMessage) | ||
} | ||
|
||
override fun warn(logMessage: () -> String) { | ||
logger.log(Level.WARNING, logMessage) | ||
} | ||
|
||
override fun debug(logMessage: () -> String) { | ||
logger.log(Level.ALL, logMessage) | ||
} | ||
} |
34 changes: 15 additions & 19 deletions
34
core/src/main/kotlin/ru/astrainteractive/astralibs/logging/Logger.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,21 @@ | ||
package ru.astrainteractive.astralibs.logging | ||
|
||
import java.io.File | ||
import java.time.LocalDateTime | ||
import java.time.format.DateTimeFormatter | ||
|
||
/** | ||
* Use [Logger] with delegation | ||
*/ | ||
interface Logger { | ||
val tag: String | ||
val folder: File | ||
fun warning(tag: String, msg: String, logInFile: Boolean = false) | ||
fun error(tag: String, msg: String, logInFile: Boolean = false) | ||
fun info(tag: String, msg: String, logInFile: Boolean = false) | ||
@Suppress("VariableNaming") | ||
val TAG: String | ||
|
||
fun error(logMessage: () -> String) | ||
|
||
fun error(error: Throwable?, logMessage: () -> String) | ||
|
||
fun info(logMessage: () -> String) | ||
|
||
fun verbose(logMessage: () -> String) | ||
|
||
companion object { | ||
/** | ||
* Returns current time in HH:mm:ss format | ||
*/ | ||
fun getTime(): String = DateTimeFormatter.ofPattern("HH:mm:ss").format(LocalDateTime.now()) | ||
fun warn(logMessage: () -> String) | ||
|
||
/** | ||
* Returns current date in yyyy-MM-dd format | ||
*/ | ||
fun getDate(): String = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(LocalDateTime.now()) | ||
} | ||
fun debug(logMessage: () -> String) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters