提交 7ef88277 编写于 作者: 梦境迷离's avatar 梦境迷离

support slf4j log4j2

上级 19bade7e
......@@ -71,7 +71,8 @@ The `@builder` used to generate builder pattern for Scala classes.
- Support `case class` / `class`.
- It can be used with `@toString`. But `@builder` needs to be put in the back.
- If there is no companion object, one will be generated to store the `builder` method and `Builder` class.
- IDE support is not very good, a red prompt will appear, but the compilation is OK.
> IDE support is not very good, a red prompt will appear, but the compilation is OK.
- Example
......@@ -158,6 +159,31 @@ Compiler intermediate code:
def getStr(k: Int): String = this.synchronized(k.$plus(""))
```
## @log
The `@log` does not use mixed or wrapper, but directly uses macro to generate default log object and operate log.
- Note
- `verbose` Whether to enable detailed log.
- `logType` Specifies the type of `log` that needs to be generated.
- `java.util.logging.Logger`
- `org.apache.logging.log4j.Logger`
- `org.slf4j.Logger`
- Support `class`, `case class` and `object`.
> IDE support is not very good, a red prompt will appear, but the compilation is OK. Need to provide their dependencies, refer to the test.
- Example
```scala
@log(verbose = true) class TestClass1(val i: Int = 0, var j: Int) {
log.info("hello")
}
@log(verbose=true, logType=io.github.dreamylost.LogType.Slf4j) class TestClass6(val i: Int = 0, var j: Int){ log.info("hello world") }
```
# How to use
Add library dependency
......
......@@ -91,6 +91,7 @@ object logMacro extends MacroCommon {
val args: (Boolean, LogType) = c.prefix.tree match {
case q"new log(logType=$logType)" => (false, c.eval[LogType](c.Expr(logType)))
case q"new log(verbose=$verbose)" => (c.eval[Boolean](c.Expr(verbose)), LogType.JLog)
case q"new log($logType)" => (false, c.eval[LogType](c.Expr(logType)))
case q"new log(verbose=$verbose, logType=$logType)" => (c.eval[Boolean](c.Expr(verbose)), c.eval[LogType](c.Expr(logType)))
case q"new log()" => (false, LogType.JLog)
case _ => c.abort(c.enclosingPosition, "unexpected annotation pattern!")
......
......@@ -87,10 +87,12 @@ class LogTest extends FlatSpec with Matchers {
log.info("hello")
}""" should compile
"""@log class TestClass2(val i: Int = 0, var j: Int)""" should compile
"""@toString @builder @log class TestClass2(val i: Int = 0, var j: Int)""" should compile //Use with multiple annotations
"""@log() class TestClass3(val i: Int = 0, var j: Int)""" should compile
"""@log(verbose=true) class TestClass4(val i: Int = 0, var j: Int)""" should compile
"""@log(logType=io.github.dreamylost.LogType.Slf4j) class TestClass5(val i: Int = 0, var j: Int)""" should compile
"""@log(verbose=true, logType=io.github.dreamylost.LogType.Slf4j) class TestClass6(val i: Int = 0, var j: Int)""" should compile
"""@log(verbose=true, logType=io.github.dreamylost.LogType.Slf4j) class TestClass6(val i: Int = 0, var j: Int){ log.info("hello world") }""" should compile
"""@log(io.github.dreamylost.LogType.Slf4j) class TestClass6(val i: Int = 0, var j: Int){ log.info("hello world") }""" should compile //default verbose is false
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册