Skip to content
This repository was archived by the owner on Feb 1, 2024. It is now read-only.

Commit e2ae36c

Browse files
1.7.1
1 parent c7e6b7c commit e2ae36c

File tree

15 files changed

+79
-59
lines changed

15 files changed

+79
-59
lines changed

VERSION.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@
1010

1111
## 版本日志
1212

13+
### 1.7.1
14+
15+
- 缓解模块 `logger-console` 下日志输出过快导致的时间异常问题 (已在 2.0 修复)
16+
- 优化模块`logger-console` 下包名格式化样式
17+
1318
### 1.7.0
1419

1520
- 升级 `slf4j` 到 1.7.31
1621
- 升级 `kotlin` 到 1.5.21
1722
- 升级 `gradle` 到 6.8.3
1823
- 优化模块 `logger-slf4j` 逻辑,现在引入此模块后不会影响项目原 `slf4j` 版本了
1924

20-
2125
### 1.6.0
2226

2327
- 优化 `logger-console` 日志输出样式
@@ -82,4 +86,3 @@
8286
### 1.0.1
8387

8488
第一个稳定发行版
85-

logger-console/src/main/kotlin/logger4k/impl/console/ConsoleLogConfig.kt

+10-11
Original file line numberDiff line numberDiff line change
@@ -53,30 +53,29 @@ object ConsoleLogConfig {
5353

5454
init {
5555
loggerLevel = try {
56-
LoggerLevel.valueOf(System.getProperty("logger.level", "INFO")!!.toUpperCase())
56+
LoggerLevel.valueOf(System.getProperty("logger.level", "INFO")!!.uppercase())
5757
} catch (_: Exception) {
5858
LoggerLevel.INFO
5959
}
6060

61-
Runtime.getRuntime().addShutdownHook(Thread {
62-
for (runnable in threadPool.shutdownNow()) {
63-
try {
64-
runnable.run()
65-
} catch (_: Exception) {
61+
Runtime.getRuntime().addShutdownHook(
62+
Thread {
63+
for (runnable in threadPool.shutdownNow()) {
64+
try {
65+
runnable.run()
66+
} catch (_: Exception) {
67+
}
6668
}
69+
println("程序于 ${dateFormat.format(System.currentTimeMillis())} 退出.")
6770
}
68-
println("程序于 ${dateFormat.format(System.currentTimeMillis())} 退出.")
69-
})
71+
)
7072
}
7173

72-
7374
internal val classNameFormat: ClassNameFormat = ClassNameFormat.DEFAULT_IMPL
7475

7576
/**
7677
* logger level
7778
*/
7879
internal val loggerLevelInt: Int
7980
get() = loggerLevel.level
80-
81-
8281
}

logger-console/src/main/kotlin/logger4k/impl/console/ConsoleLogger.kt

+6-6
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ class ConsoleLogger(override val name: String) : SimpleLogger() {
4545
ConsoleLogConfig.output
4646
}.println(
4747
if (exception != null) format(
48-
name, date, level, threadInfo, message + "\r\n" +
49-
ThrowableUtils.format(exception)
48+
name, date, level, threadInfo,
49+
message + "\r\n" +
50+
ThrowableUtils.format(exception)
5051
) else format(
5152
name,
5253
date,
@@ -56,7 +57,6 @@ class ConsoleLogger(override val name: String) : SimpleLogger() {
5657
)
5758
)
5859
}
59-
6060
}
6161

6262
private fun format(
@@ -68,7 +68,9 @@ class ConsoleLogger(override val name: String) : SimpleLogger() {
6868
): String {
6969
val res = StringBuilder()
7070
res.append("")
71-
.append(ConsoleLogConfig.dateFormat.format(date))
71+
.append(
72+
synchronized(ConsoleLogConfig.dateFormat) { ConsoleLogConfig.dateFormat.format(date) }
73+
)
7274
.append(" - ")
7375
.append(formatThreadName(threadInfo))
7476
.append("/")
@@ -94,7 +96,6 @@ class ConsoleLogger(override val name: String) : SimpleLogger() {
9496
}
9597
}
9698

97-
9899
override fun traceOnly(function: ILogger.() -> Unit): ILogger {
99100
if (ConsoleLogConfig.loggerLevelInt <= LoggerLevel.TRACE.level) {
100101
function(this)
@@ -111,5 +112,4 @@ class ConsoleLogger(override val name: String) : SimpleLogger() {
111112

112113
override val isDebug: Boolean
113114
get() = ConsoleLogConfig.loggerLevelInt <= LoggerLevel.DEBUG.level
114-
115115
}

logger-console/src/test/kotlin/logger4k/impl/console/LoggerMainTestAll.kt

-4
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,19 @@ fun main() {
6363
logger.info("My name is {}.", "John")
6464
// 输出 INFO 日志
6565

66-
6766
logger.warn("Warn.")
6867
logger.warn("My name is {}.", "John")
6968
logger.warnThrowable("输出异常信息", RuntimeException("Runtime Exception"))
7069
// 输出 WARN 日志
7170

72-
7371
logger.error("Error.")
7472
logger.error("My name is {}.", "John")
7573
logger.errorThrowable("输出异常信息", RuntimeException("Runtime Exception"))
7674
// 输出 ERROR 日志
7775
val logger2 = LoggerFactory.getLogger(Void::class)
7876
logger2.info("Hello World")
79-
8077
}
8178

82-
8379
internal class Test {
8480
@Test
8581
fun test() {

logger-console/src/test/kotlin/logger4k/impl/console/PrintLogger.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ package logger4k.impl.console
2323
import com.github.openEdgn.logger4k.LoggerFactory
2424
import org.junit.jupiter.api.Test
2525
import java.text.SimpleDateFormat
26-
import java.util.*
26+
import java.util.Date
2727

2828
internal class PrintLogger {
2929
/**

logger-core/src/main/kotlin/com/github/openEdgn/logger4k/ILogger.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
package com.github.openEdgn.logger4k
2222

23-
2423
/**
2524
* # Logger 日志框架核心
2625
*
@@ -141,9 +140,8 @@ interface ILogger {
141140
*/
142141
val isDebug: Boolean
143142

144-
145143
/**
146144
* # 日志输出等级
147145
*/
148146
val level: LoggerLevel
149-
}
147+
}

logger-core/src/main/kotlin/com/github/openEdgn/logger4k/LoggerConfig.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import java.io.PrintWriter
3030
*/
3131
internal object LoggerConfig {
3232
fun internalError(msg: String, e: Exception? = null) {
33-
if (internalDebug){
33+
if (internalDebug) {
3434
System.err.printf("[ Logger4K internal Error] Error Message :%d \r\n", msg)
3535
e?.run {
3636
val byteArrayOutputStream = ByteArrayOutputStream()
@@ -47,8 +47,8 @@ internal object LoggerConfig {
4747
*/
4848
private val internalDebug: Boolean by lazy {
4949
(System.getProperty("logger4k.internal.debug", "false") ?: "false")
50-
.contentEquals("true")
50+
.contentEquals("true")
5151
}
5252
}
5353

54-
fun getMessageFormat(): IMessageFormat = LoggerConfig.messageFormat
54+
fun getMessageFormat(): IMessageFormat = LoggerConfig.messageFormat

logger-core/src/main/kotlin/com/github/openEdgn/logger4k/LoggerLevel.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@ enum class LoggerLevel(val level: Int) {
5050
* ERROR
5151
*/
5252
ERROR(4),
53-
}
53+
}

logger-core/src/main/kotlin/com/github/openEdgn/logger4k/SimpleLogger.kt

-2
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,11 @@ package com.github.openEdgn.logger4k
2222

2323
import com.github.openEdgn.logger4k.plugin.PluginManager
2424

25-
2625
/**
2726
* 默认的 Logger 实现类
2827
*/
2928
abstract class SimpleLogger : ILogger {
3029

31-
3230
abstract val name: String
3331
abstract fun printLogger(date: Long, level: LoggerLevel, message: String)
3432
abstract fun printLogger(date: Long, level: LoggerLevel, message: String, exception: Throwable)

logger-core/src/main/kotlin/com/github/openEdgn/logger4k/format/ClassNameFormat.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,4 @@ abstract class ClassNameFormat {
3535
companion object {
3636
val DEFAULT_IMPL: ClassNameFormat = MaxLengthClassFormat()
3737
}
38-
39-
}
38+
}

logger-core/src/main/kotlin/com/github/openEdgn/logger4k/format/IMessageFormat.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ package com.github.openEdgn.logger4k.format
3232
*/
3333
interface IMessageFormat {
3434
fun format(message: String, data: Array<out Any?>): String
35-
}
35+
}

logger-core/src/main/kotlin/com/github/openEdgn/logger4k/format/MaxLengthClassFormat.kt

+46-13
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,59 @@ package com.github.openEdgn.logger4k.format
2222

2323
/**
2424
* 类名称格式化工具
25-
* @property maxLine Int
25+
* @property maxLength Int
2626
*/
2727
class MaxLengthClassFormat : ClassNameFormat() {
28-
private val maxLine = 30
28+
private val maxLength = 40
2929
override fun format(name: String): String {
30-
return if (name.length > maxLine) {
31-
val list = name.split(".")
32-
val stringBuilder = StringBuilder()
33-
for (i in 0 until list.size - 1) {
34-
stringBuilder.append(list[i].first()).append(".")
30+
if (maxLength == -1) {
31+
return name
32+
}
33+
return if (name.length > maxLength) {
34+
var length = name.length
35+
var accept = false
36+
val builder = StringBuilder()
37+
val split = name.split(".")
38+
if (split.size == 1) {
39+
return formatLength(name, maxLength)
3540
}
36-
if (maxLine - stringBuilder.length < list.last().length) {
37-
stringBuilder.append(list.last().replace(Regex("[a-z]"), ""))
38-
} else {
39-
stringBuilder.append(list.last())
41+
for (item in 0 until split.size - 1) {
42+
if (accept) {
43+
builder.append(split[item]).append(".")
44+
} else {
45+
if ((length + 2 - split[item].length) <= maxLength) {
46+
accept = true
47+
}
48+
builder.append(split[item].first()).append(".")
49+
length -= split[item].length - 2
50+
}
4051
}
4152

42-
String.format("%${-maxLine}s", stringBuilder.toString())
53+
val lastName = split.last()
54+
if (lastName.length > maxLength - builder.length) {
55+
builder.append(lastName)
56+
builder.toString()
57+
} else {
58+
builder.append(lastName)
59+
formatLength(builder.toString(), maxLength)
60+
}
4361
} else {
44-
String.format("%${-maxLine}s", name)
62+
formatLength(name, maxLength)
4563
}
4664
}
65+
66+
private fun formatLength(data: String, len: Int): String {
67+
if (len <= 0) {
68+
return ""
69+
}
70+
val item = data.substring(
71+
0,
72+
if (data.length > len) {
73+
len
74+
} else {
75+
data.length
76+
}
77+
)
78+
return String.format("%${-len}s", item)
79+
}
4780
}

logger-core/src/main/kotlin/com/github/openEdgn/logger4k/format/MessageFormatImpl.kt

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020

2121
package com.github.openEdgn.logger4k.format
2222

23-
import java.util.*
24-
import kotlin.collections.ArrayList
23+
import java.util.LinkedList
2524

2625
/**
2726
* 消息格式化类
@@ -87,4 +86,4 @@ object MessageFormatImpl : IMessageFormat {
8786
}
8887
return result.toString().replace(Regex("(\\\\\\{}|\\\\\\{\\\\})"), "{}")
8988
}
90-
}
89+
}

logger-core/src/main/kotlin/com/github/openEdgn/logger4k/plugin/IPlugin.kt

-1
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,4 @@ interface IPlugin {
5555
* @return LoggerLevel 日志等级
5656
*/
5757
fun getLoggerLevel(name: String): LoggerLevel
58-
5958
}

logger-core/src/main/kotlin/com/github/openEdgn/logger4k/plugin/PluginManager.kt

+2-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ package com.github.openEdgn.logger4k.plugin
2222

2323
import com.github.openEdgn.logger4k.LoggerConfig
2424
import java.io.Closeable
25-
import java.util.*
25+
import java.util.Properties
2626
import kotlin.reflect.KClass
2727
import kotlin.reflect.full.createInstance
2828
import kotlin.reflect.full.isSubclassOf
@@ -37,7 +37,6 @@ object PluginManager : Closeable {
3737

3838
internal fun implPlugin(): IPlugin {
3939
return loggerPlugin ?: throw RuntimeException("未在类路径下找到 Logger 的实现类,无法记录默认日志.")
40-
4140
}
4241

4342
private const val PLUGIN_IMPL_CLASS_KEY = "logger4k.plugin.implClass"
@@ -78,7 +77,6 @@ object PluginManager : Closeable {
7877
}
7978
}
8079

81-
8280
/**
8381
* 注册 Plugin Manager 插件
8482
* @param pluginClass KClass<IPlugin>
@@ -99,8 +97,6 @@ object PluginManager : Closeable {
9997
}
10098
}
10199

102-
103100
override fun close() {
104-
105101
}
106-
}
102+
}

0 commit comments

Comments
 (0)