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

Commit 3000bf9

Browse files
ver.2.0.pre
1 parent e8ec271 commit 3000bf9

26 files changed

Lines changed: 521 additions & 78 deletions

File tree

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2020, OpenEDGN AND QOS.ch. All rights reserved.
1+
Copyright (c) 2020-2021, OpenEDGN . All rights reserved.
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy
44
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,9 @@ logger.debugOnly {
103103

104104
```
105105

106-
Please see [PrintLogger.kt](./logger-console/src/test/kotlin/logger4k/impl/console/PrintLogger.kt)
107-
and [LoggerMainTestAll.kt](./logger-console/src/test/kotlin/logger4k/impl/console/LoggerMainTestAll.kt) under `TEST` for more usage
108-
methods.
106+
Please see [PrintLogger.kt](./logger-console/src/test/kotlin/PrintLogger.kt)
107+
and [LoggerMainTestAll.kt](./logger-console/src/test/kotlin/LoggerMainTestAll.kt) under `TEST` for more usage methods.
109108

110109
## LICENSE
111110

112-
Warn: This project uses [SLF4J](https://github.com/qos-ch/slf4j) source code.
113-
114-
SEE [LICENSE FILE](./LICENSE) AND [SLF4J LICENSE](https://github.com/qos-ch/slf4j/blob/master/LICENSE.txt)
111+
SEE [LICENSE FILE](./LICENSE)

README_CN.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,9 @@ logger.debugOnly {
101101

102102
```
103103

104-
更多使用方法请查看 `TEST` 下的 [PrintLogger.kt](./logger-console/src/test/kotlin/logger4k/impl/console/PrintLogger.kt)
105-
[LoggerMainTestAll.kt](./logger-console/src/test/kotlin/logger4k/impl/console/LoggerMainTestAll.kt) 文件。
104+
更多使用方法请查看 `TEST` 下的 [PrintLogger.kt](./logger-console/src/test/kotlin/PrintLogger.kt)
105+
[LoggerMainTestAll.kt](./logger-console/src/test/kotlin/LoggerMainTestAll.kt) 文件。
106106

107107
## LICENSE
108108

109-
注意: 此项目包含有 [SLF4J](https://github.com/qos-ch/slf4j) 的源代码
110-
111-
请转到 [LICENSE FILE](./LICENSE)[SLF4J LICENSE](https://github.com/qos-ch/slf4j/blob/master/LICENSE.txt)
109+
请转到 [LICENSE FILE](./LICENSE)

VERSION.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,25 @@
1010

1111
## 版本日志
1212

13+
### 2.0.0
14+
15+
重构的 Logger4K 2.0 来了 ✨
16+
17+
注意:此版本为 2.0 初始版本,想从`1.x.x` 迁移请查看 [迁移教程](./docs/update-1.x-2.x.md)
18+
19+
- 移动模块 `logger-slf4j``slf4j-over-logger4k`
20+
[Logger4KSupport](https://github.com/OpenEdgn/Logger4KSupport)
21+
22+
- 重构 `logger-core` 模块,现已内嵌一个简单的日志输出实现
23+
- `logger-console` 模块啥也没变
24+
1325
### 1.7.0
1426

1527
- 升级 `slf4j` 到 1.7.31
1628
- 升级 `kotlin` 到 1.5.21
1729
- 升级 `gradle` 到 6.8.3
1830
- 优化模块 `logger-slf4j` 逻辑,现在引入此模块后不会影响项目原 `slf4j` 版本了
1931

20-
2132
### 1.6.0
2233

2334
- 优化 `logger-console` 日志输出样式

docs/update-1.x-2.x.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# 1.x - 2.x 迁移
2+
3+
// TODO

logger-console/src/main/java/logger4k/impl/console/ThrowableUtils.java

Lines changed: 0 additions & 34 deletions
This file was deleted.

logger-console/src/main/java/module-info.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
requires kotlin.stdlib;
33
requires logger4k.core;
44
requires kotlin.reflect;
5-
exports logger4k.impl.console;
6-
// opens logger4k.impl.console to logger4k.core;
7-
// exports logger4k;
8-
// opens logger4k to logger4k.core;
5+
exports logger4k.console;
6+
opens logger4k.console to logger4k.core;
97
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package logger4k.console
2+
3+
import com.github.openEdgn.logger4k.LoggerLevel
4+
import com.github.openEdgn.logger4k.utils.format.classes.ClassNameFormat
5+
import com.github.openEdgn.logger4k.utils.format.classes.MaxLengthClassFormat
6+
import java.io.PrintStream
7+
import java.text.SimpleDateFormat
8+
import java.util.concurrent.ExecutorService
9+
import java.util.concurrent.Executors
10+
11+
/**
12+
* 内部日志配置
13+
*/
14+
object ConsoleLogConfig {
15+
@Volatile
16+
var loggerLevel = LoggerLevel.INFO
17+
18+
@Volatile
19+
var dateFormat = SimpleDateFormat("yy/MM/dd HH:mm:ss")
20+
21+
/**
22+
* console output
23+
*/
24+
@Volatile
25+
var output: PrintStream = System.out
26+
27+
/**
28+
* console error output
29+
*/
30+
@Volatile
31+
var error: PrintStream = System.err
32+
33+
internal val threadPool: ExecutorService = Executors.newCachedThreadPool()
34+
35+
init {
36+
loggerLevel = try {
37+
LoggerLevel.valueOf(System.getProperty("logger.level", "INFO")!!.uppercase())
38+
} catch (_: Exception) {
39+
LoggerLevel.INFO
40+
}
41+
}
42+
43+
internal val classNameFormat: ClassNameFormat = MaxLengthClassFormat()
44+
45+
/**
46+
* logger level
47+
*/
48+
internal val loggerLevelInt: Int
49+
get() = loggerLevel.level
50+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package logger4k.console
2+
3+
import com.github.openEdgn.logger4k.ILogger
4+
import com.github.openEdgn.logger4k.LoggerLevel
5+
import com.github.openEdgn.logger4k.SimpleLogger
6+
import com.github.openEdgn.logger4k.utils.ThrowableUtils
7+
8+
class ConsoleLogger(override val name: String) : SimpleLogger() {
9+
override fun printLogger(date: Long, level: LoggerLevel, message: String) {
10+
printlnLog(date, level, message, null)
11+
}
12+
13+
override fun printLogger(date: Long, level: LoggerLevel, message: String, exception: Throwable) {
14+
printlnLog(date, level, message, exception)
15+
}
16+
17+
private fun printlnLog(date: Long, level: LoggerLevel, message: String, exception: Throwable?) {
18+
if (level.level < ConsoleLogConfig.loggerLevelInt) {
19+
return
20+
}
21+
val threadInfo = Thread.currentThread()
22+
ConsoleLogConfig.threadPool.execute {
23+
if (level.level >= LoggerLevel.WARN.level) {
24+
ConsoleLogConfig.error
25+
} else {
26+
ConsoleLogConfig.output
27+
}.println(
28+
if (exception != null) format(
29+
name, date, level, threadInfo,
30+
message + "\r\n" +
31+
ThrowableUtils.format(exception)
32+
) else format(
33+
name,
34+
date,
35+
level,
36+
threadInfo,
37+
message
38+
)
39+
)
40+
}
41+
}
42+
43+
private fun format(
44+
name: String,
45+
date: Long,
46+
level: LoggerLevel,
47+
threadInfo: Thread,
48+
message: String
49+
): String {
50+
val res = StringBuilder()
51+
res.append("")
52+
.append(ConsoleLogConfig.dateFormat.format(date))
53+
.append(" - ")
54+
.append(formatThreadName(threadInfo))
55+
.append("/")
56+
.append(level.name[0])
57+
.append(" - ")
58+
.append(name)
59+
.append(" -> ")
60+
.append(message)
61+
return res.toString()
62+
}
63+
64+
private val threadNameLength = 12
65+
private fun formatThreadName(threadInfo: Thread): String {
66+
val tName = threadInfo.name
67+
return if (tName.length <= threadNameLength) {
68+
String.format("%-${threadNameLength}s", tName)
69+
} else {
70+
var replace = tName.replace(Regex("[a-z]"), "")
71+
if (replace.length > threadNameLength) {
72+
replace = replace.substring(0, 12)
73+
}
74+
String.format("%-${threadNameLength}s", replace)
75+
}
76+
}
77+
78+
override fun traceOnly(function: ILogger.() -> Unit): ILogger {
79+
if (ConsoleLogConfig.loggerLevelInt <= LoggerLevel.TRACE.level) {
80+
function(this)
81+
}
82+
return this
83+
}
84+
85+
override fun debugOnly(function: ILogger.() -> Unit): ILogger {
86+
if (ConsoleLogConfig.loggerLevelInt <= LoggerLevel.DEBUG.level) {
87+
function(this)
88+
}
89+
return this
90+
}
91+
92+
override val isDebug: Boolean
93+
get() = ConsoleLogConfig.loggerLevelInt <= LoggerLevel.DEBUG.level
94+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package logger4k.console
2+
3+
import com.github.openEdgn.logger4k.ILogger
4+
import com.github.openEdgn.logger4k.LoggerLevel
5+
import com.github.openEdgn.logger4k.plugin.IPlugin
6+
import java.util.concurrent.ConcurrentHashMap
7+
import kotlin.reflect.KClass
8+
9+
object LoggerPlugin : IPlugin {
10+
private val map = ConcurrentHashMap<String, ILogger>(100)
11+
12+
override fun getLogger(name: String): ILogger {
13+
return map[name] ?: kotlin.run {
14+
val consoleLogger = ConsoleLogger(ConsoleLogConfig.classNameFormat.format(name)) as ILogger
15+
map[name] = consoleLogger
16+
consoleLogger
17+
}
18+
}
19+
20+
override fun getLoggerLevel(name: String): LoggerLevel {
21+
return ConsoleLogConfig.loggerLevel
22+
}
23+
24+
override val ignoreOptimization = true
25+
26+
override val name: String = "ConsoleLogger"
27+
28+
override fun getLogger(clazz: KClass<*>): ILogger {
29+
return getLogger(ConsoleLogConfig.classNameFormat.format(clazz))
30+
}
31+
32+
override fun shutdown() {
33+
for (runnable in ConsoleLogConfig.threadPool.shutdownNow()) {
34+
try {
35+
runnable.run()
36+
} catch (_: Exception) {
37+
}
38+
}
39+
println("程序于 ${ConsoleLogConfig.dateFormat.format(System.currentTimeMillis())} 退出.")
40+
}
41+
}

0 commit comments

Comments
 (0)