This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
-
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
1 parent
e8ec271
commit 3000bf9
Showing
26 changed files
with
521 additions
and
78 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
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
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,3 @@ | ||
# 1.x - 2.x 迁移 | ||
|
||
// TODO |
34 changes: 0 additions & 34 deletions
34
logger-console/src/main/java/logger4k/impl/console/ThrowableUtils.java
This file was deleted.
Oops, something went wrong.
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
50 changes: 50 additions & 0 deletions
50
logger-console/src/main/kotlin/logger4k/console/ConsoleLogConfig.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,50 @@ | ||
package logger4k.console | ||
|
||
import com.github.openEdgn.logger4k.LoggerLevel | ||
import com.github.openEdgn.logger4k.utils.format.classes.ClassNameFormat | ||
import com.github.openEdgn.logger4k.utils.format.classes.MaxLengthClassFormat | ||
import java.io.PrintStream | ||
import java.text.SimpleDateFormat | ||
import java.util.concurrent.ExecutorService | ||
import java.util.concurrent.Executors | ||
|
||
/** | ||
* 内部日志配置 | ||
*/ | ||
object ConsoleLogConfig { | ||
@Volatile | ||
var loggerLevel = LoggerLevel.INFO | ||
|
||
@Volatile | ||
var dateFormat = SimpleDateFormat("yy/MM/dd HH:mm:ss") | ||
|
||
/** | ||
* console output | ||
*/ | ||
@Volatile | ||
var output: PrintStream = System.out | ||
|
||
/** | ||
* console error output | ||
*/ | ||
@Volatile | ||
var error: PrintStream = System.err | ||
|
||
internal val threadPool: ExecutorService = Executors.newCachedThreadPool() | ||
|
||
init { | ||
loggerLevel = try { | ||
LoggerLevel.valueOf(System.getProperty("logger.level", "INFO")!!.uppercase()) | ||
} catch (_: Exception) { | ||
LoggerLevel.INFO | ||
} | ||
} | ||
|
||
internal val classNameFormat: ClassNameFormat = MaxLengthClassFormat() | ||
|
||
/** | ||
* logger level | ||
*/ | ||
internal val loggerLevelInt: Int | ||
get() = loggerLevel.level | ||
} |
94 changes: 94 additions & 0 deletions
94
logger-console/src/main/kotlin/logger4k/console/ConsoleLogger.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,94 @@ | ||
package logger4k.console | ||
|
||
import com.github.openEdgn.logger4k.ILogger | ||
import com.github.openEdgn.logger4k.LoggerLevel | ||
import com.github.openEdgn.logger4k.SimpleLogger | ||
import com.github.openEdgn.logger4k.utils.ThrowableUtils | ||
|
||
class ConsoleLogger(override val name: String) : SimpleLogger() { | ||
override fun printLogger(date: Long, level: LoggerLevel, message: String) { | ||
printlnLog(date, level, message, null) | ||
} | ||
|
||
override fun printLogger(date: Long, level: LoggerLevel, message: String, exception: Throwable) { | ||
printlnLog(date, level, message, exception) | ||
} | ||
|
||
private fun printlnLog(date: Long, level: LoggerLevel, message: String, exception: Throwable?) { | ||
if (level.level < ConsoleLogConfig.loggerLevelInt) { | ||
return | ||
} | ||
val threadInfo = Thread.currentThread() | ||
ConsoleLogConfig.threadPool.execute { | ||
if (level.level >= LoggerLevel.WARN.level) { | ||
ConsoleLogConfig.error | ||
} else { | ||
ConsoleLogConfig.output | ||
}.println( | ||
if (exception != null) format( | ||
name, date, level, threadInfo, | ||
message + "\r\n" + | ||
ThrowableUtils.format(exception) | ||
) else format( | ||
name, | ||
date, | ||
level, | ||
threadInfo, | ||
message | ||
) | ||
) | ||
} | ||
} | ||
|
||
private fun format( | ||
name: String, | ||
date: Long, | ||
level: LoggerLevel, | ||
threadInfo: Thread, | ||
message: String | ||
): String { | ||
val res = StringBuilder() | ||
res.append("") | ||
.append(ConsoleLogConfig.dateFormat.format(date)) | ||
.append(" - ") | ||
.append(formatThreadName(threadInfo)) | ||
.append("/") | ||
.append(level.name[0]) | ||
.append(" - ") | ||
.append(name) | ||
.append(" -> ") | ||
.append(message) | ||
return res.toString() | ||
} | ||
|
||
private val threadNameLength = 12 | ||
private fun formatThreadName(threadInfo: Thread): String { | ||
val tName = threadInfo.name | ||
return if (tName.length <= threadNameLength) { | ||
String.format("%-${threadNameLength}s", tName) | ||
} else { | ||
var replace = tName.replace(Regex("[a-z]"), "") | ||
if (replace.length > threadNameLength) { | ||
replace = replace.substring(0, 12) | ||
} | ||
String.format("%-${threadNameLength}s", replace) | ||
} | ||
} | ||
|
||
override fun traceOnly(function: ILogger.() -> Unit): ILogger { | ||
if (ConsoleLogConfig.loggerLevelInt <= LoggerLevel.TRACE.level) { | ||
function(this) | ||
} | ||
return this | ||
} | ||
|
||
override fun debugOnly(function: ILogger.() -> Unit): ILogger { | ||
if (ConsoleLogConfig.loggerLevelInt <= LoggerLevel.DEBUG.level) { | ||
function(this) | ||
} | ||
return this | ||
} | ||
|
||
override val isDebug: Boolean | ||
get() = ConsoleLogConfig.loggerLevelInt <= LoggerLevel.DEBUG.level | ||
} |
41 changes: 41 additions & 0 deletions
41
logger-console/src/main/kotlin/logger4k/console/LoggerPlugin.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,41 @@ | ||
package logger4k.console | ||
|
||
import com.github.openEdgn.logger4k.ILogger | ||
import com.github.openEdgn.logger4k.LoggerLevel | ||
import com.github.openEdgn.logger4k.plugin.IPlugin | ||
import java.util.concurrent.ConcurrentHashMap | ||
import kotlin.reflect.KClass | ||
|
||
object LoggerPlugin : IPlugin { | ||
private val map = ConcurrentHashMap<String, ILogger>(100) | ||
|
||
override fun getLogger(name: String): ILogger { | ||
return map[name] ?: kotlin.run { | ||
val consoleLogger = ConsoleLogger(ConsoleLogConfig.classNameFormat.format(name)) as ILogger | ||
map[name] = consoleLogger | ||
consoleLogger | ||
} | ||
} | ||
|
||
override fun getLoggerLevel(name: String): LoggerLevel { | ||
return ConsoleLogConfig.loggerLevel | ||
} | ||
|
||
override val ignoreOptimization = true | ||
|
||
override val name: String = "ConsoleLogger" | ||
|
||
override fun getLogger(clazz: KClass<*>): ILogger { | ||
return getLogger(ConsoleLogConfig.classNameFormat.format(clazz)) | ||
} | ||
|
||
override fun shutdown() { | ||
for (runnable in ConsoleLogConfig.threadPool.shutdownNow()) { | ||
try { | ||
runnable.run() | ||
} catch (_: Exception) { | ||
} | ||
} | ||
println("程序于 ${ConsoleLogConfig.dateFormat.format(System.currentTimeMillis())} 退出.") | ||
} | ||
} |
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 @@ | ||
logger4k.plugin.implClass=logger4k.console.LoggerPlugin |
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,62 @@ | ||
import com.github.openEdgn.logger4k.LoggerFactory | ||
import org.junit.jupiter.api.Test | ||
|
||
/** | ||
* 此日志框架的使用方法 | ||
*/ | ||
class LoggerMainTestAll | ||
|
||
@Test | ||
fun main() { | ||
Thread.currentThread().name = "Main" | ||
System.setProperty("logger.level", "trace") | ||
System.setProperty("logger.level", "debug") | ||
System.setProperty("logger.level", "info") | ||
System.setProperty("logger.level", "warn") | ||
System.setProperty("logger.level", "error") | ||
// 指定日志级别,可以未 DEBUG INFO WARN ERROR | ||
// val logger = getLogger() | ||
// 获取Logger 实例 | ||
val logger = LoggerFactory.getLogger(LoggerMainTestAll::class) | ||
// 另一种方式获取Logger 实例 | ||
System.setProperty("logger.level", "trace") | ||
logger.trace("trace.") | ||
logger.trace("日志级别: {}.", "trace") | ||
logger.traceOnly { | ||
trace("trace 代码块 {}", arrayListOf("A", "B", "C").toString()) | ||
// 此代码块仅在trace级别时执行 | ||
} | ||
logger.debugOnly { | ||
trace("debug 代码块 {}", arrayListOf("A", "B", "C").toString()) | ||
// 此代码块仅在debug级别时执行 | ||
} | ||
// 输出 TRACE 日志 | ||
|
||
logger.debug("debug.") | ||
logger.debug("日志级别: {}.", "debug") | ||
logger.debugThrowable("输出异常信息", RuntimeException("Runtime Exception")) | ||
// 输出 DEBUG 日志 | ||
|
||
logger.info("Info.") | ||
logger.info("My name is {}.", "John") | ||
// 输出 INFO 日志 | ||
|
||
logger.warn("Warn.") | ||
logger.warn("My name is {}.", "John") | ||
logger.warnThrowable("输出异常信息", RuntimeException("Runtime Exception")) | ||
// 输出 WARN 日志 | ||
|
||
logger.error("Error.") | ||
logger.error("My name is {}.", "John") | ||
logger.errorThrowable("输出异常信息", RuntimeException("Runtime Exception")) | ||
// 输出 ERROR 日志 | ||
val logger2 = LoggerFactory.getLogger(Void::class) | ||
logger2.info("Hello World") | ||
} | ||
|
||
internal class Test { | ||
@Test | ||
fun test() { | ||
main() | ||
} | ||
} |
Oops, something went wrong.