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

Commit

Permalink
ver.2.0.pre
Browse files Browse the repository at this point in the history
  • Loading branch information
ExplodingDragon committed Jul 27, 2021
1 parent e8ec271 commit 3000bf9
Show file tree
Hide file tree
Showing 26 changed files with 521 additions and 78 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2020, OpenEDGN AND QOS.ch. All rights reserved.
Copyright (c) 2020-2021, OpenEDGN . All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,9 @@ logger.debugOnly {

```

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

## LICENSE

Warn: This project uses [SLF4J](https://github.com/qos-ch/slf4j) source code.

SEE [LICENSE FILE](./LICENSE) AND [SLF4J LICENSE](https://github.com/qos-ch/slf4j/blob/master/LICENSE.txt)
SEE [LICENSE FILE](./LICENSE)
8 changes: 3 additions & 5 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,9 @@ logger.debugOnly {

```

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

## LICENSE

注意: 此项目包含有 [SLF4J](https://github.com/qos-ch/slf4j) 的源代码

请转到 [LICENSE FILE](./LICENSE)[SLF4J LICENSE](https://github.com/qos-ch/slf4j/blob/master/LICENSE.txt)
请转到 [LICENSE FILE](./LICENSE)
13 changes: 12 additions & 1 deletion VERSION.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,25 @@

## 版本日志

### 2.0.0

重构的 Logger4K 2.0 来了 ✨

注意:此版本为 2.0 初始版本,想从`1.x.x` 迁移请查看 [迁移教程](./docs/update-1.x-2.x.md)

- 移动模块 `logger-slf4j``slf4j-over-logger4k`
[Logger4KSupport](https://github.com/OpenEdgn/Logger4KSupport)

- 重构 `logger-core` 模块,现已内嵌一个简单的日志输出实现
- `logger-console` 模块啥也没变

### 1.7.0

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


### 1.6.0

- 优化 `logger-console` 日志输出样式
Expand Down
3 changes: 3 additions & 0 deletions docs/update-1.x-2.x.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 1.x - 2.x 迁移

// TODO

This file was deleted.

6 changes: 2 additions & 4 deletions logger-console/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
requires kotlin.stdlib;
requires logger4k.core;
requires kotlin.reflect;
exports logger4k.impl.console;
// opens logger4k.impl.console to logger4k.core;
// exports logger4k;
// opens logger4k to logger4k.core;
exports logger4k.console;
opens logger4k.console to logger4k.core;
}
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 logger-console/src/main/kotlin/logger4k/console/ConsoleLogger.kt
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 logger-console/src/main/kotlin/logger4k/console/LoggerPlugin.kt
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())} 退出.")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
logger4k.plugin.implClass=logger4k.console.LoggerPlugin
62 changes: 62 additions & 0 deletions logger-console/src/test/kotlin/LoggerMainTestAll.kt
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()
}
}
Loading

0 comments on commit 3000bf9

Please sign in to comment.