-
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.
- Loading branch information
Showing
3 changed files
with
84 additions
and
23 deletions.
There are no files selected for viewing
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
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
87 changes: 74 additions & 13 deletions
87
universe/src/main/kotlin/relativitization/universe/utils/RelativitizationLogManager.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,53 +1,114 @@ | ||
package relativitization.universe.utils | ||
|
||
import org.apache.logging.log4j.Level | ||
import org.apache.logging.log4j.LogManager | ||
import org.apache.logging.log4j.Logger | ||
import org.apache.logging.log4j.util.StackLocatorUtil | ||
|
||
object RelativitizationLogManager { | ||
// Android does not support StackLocatorUtil.getCallerClass, use default logger instead | ||
private val useDefaultLoggerName: Boolean = | ||
private val useSimpleLogger: Boolean = | ||
System.getProperty("java.specification.vendor") == "The Android Project" | ||
|
||
private val commonLogger = RelativitizationLogger("CommonLogger") | ||
private val simpleLogger = RelativitizationSimpleLogger(Level.OFF) | ||
|
||
fun getLogger(): RelativitizationLogger = if (useDefaultLoggerName) { | ||
RelativitizationLogger("DefaultLogger") | ||
private val commonLogger: RelativitizationLogger = if (useSimpleLogger) { | ||
simpleLogger | ||
} else { | ||
RelativitizationLogger() | ||
RelativitizationLog4j2Logger("CommonLogger") | ||
} | ||
|
||
fun getLogger(): RelativitizationLogger = if (useSimpleLogger) { | ||
simpleLogger | ||
} else { | ||
RelativitizationLog4j2Logger() | ||
} | ||
|
||
fun getCommonLogger(): RelativitizationLogger = commonLogger | ||
|
||
fun getLogger(name: String): RelativitizationLogger = RelativitizationLogger(name) | ||
fun setSimpleLoggerLevel(level: Level) { | ||
simpleLogger.level = level | ||
} | ||
|
||
fun getLogger(name: String): RelativitizationLogger = if (useSimpleLogger) { | ||
simpleLogger | ||
} else { | ||
RelativitizationLog4j2Logger(name) | ||
} | ||
} | ||
|
||
abstract class RelativitizationLogger { | ||
abstract fun error(message: String) | ||
|
||
abstract fun warn(message: String) | ||
|
||
abstract fun info(message: String) | ||
|
||
abstract fun debug(message: String) | ||
|
||
abstract fun trace(message: String) | ||
} | ||
|
||
class RelativitizationLogger( | ||
class RelativitizationLog4j2Logger( | ||
name: String = "" | ||
) { | ||
) : RelativitizationLogger() { | ||
private val logger: Logger = if (name.isBlank()) { | ||
LogManager.getLogger(StackLocatorUtil.getCallerClass(4)) | ||
} else { | ||
LogManager.getLogger(name) | ||
} | ||
|
||
fun error(message: String) { | ||
override fun error(message: String) { | ||
logger.error(message) | ||
} | ||
|
||
fun warn(message: String) { | ||
override fun warn(message: String) { | ||
logger.warn(message) | ||
} | ||
|
||
fun info(message: String) { | ||
override fun info(message: String) { | ||
logger.info(message) | ||
} | ||
|
||
fun debug(message: String) { | ||
override fun debug(message: String) { | ||
logger.debug(message) | ||
} | ||
|
||
fun trace(message: String) { | ||
override fun trace(message: String) { | ||
logger.trace(message) | ||
} | ||
} | ||
|
||
class RelativitizationSimpleLogger( | ||
var level: Level, | ||
) : RelativitizationLogger() { | ||
override fun error(message: String) { | ||
if (level >= Level.ERROR) { | ||
println(message) | ||
} | ||
} | ||
|
||
override fun warn(message: String) { | ||
if (level >= Level.WARN) { | ||
println(message) | ||
} | ||
} | ||
|
||
override fun info(message: String) { | ||
if (level >= Level.INFO) { | ||
println(message) | ||
} | ||
} | ||
|
||
override fun debug(message: String) { | ||
if (level >= Level.DEBUG) { | ||
println(message) | ||
} | ||
} | ||
|
||
override fun trace(message: String) { | ||
if (level >= Level.TRACE) { | ||
println(message) | ||
} | ||
} | ||
} |