Skip to content

Commit

Permalink
Merge pull request #30 from Astra-Interactive/better-logging
Browse files Browse the repository at this point in the history
add better logging system
  • Loading branch information
makeevrserg authored Jun 9, 2024
2 parents fed81be + 40da919 commit 841fbf6
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 44 deletions.
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()
)
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)
}
}
}
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)
}
}
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)
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ makeevrserg.java.ktarget=21
# Project
makeevrserg.project.name=AstraLibs
makeevrserg.project.group=ru.astrainteractive.astralibs
makeevrserg.project.version.string=3.6.1
makeevrserg.project.version.string=3.7.0
makeevrserg.project.description=Core utilities for spigot development
makeevrserg.project.developers=makeevrserg|Makeev Roman|makeevrserg@gmail.com
makeevrserg.project.url=https://empireprojekt.ru
Expand Down

0 comments on commit 841fbf6

Please sign in to comment.