Logtool is a Java library that provides facilities to log each method's parameters and result within method-invocation-chain in Spring/SpringBoot application. It is based on JSR 269 (Pluggable Annotation Processing API) and javac-relevant api. Just use one annotation on class and another one annotation on its method to avoid redundant log hard code.
- Support any access type of method except abstract
- Currently coordinate with Lombok's
@Data
or@ToString
annotation - Support Spring Boot or Spring application
- Support configurable class/method-level-switch based on properties within
application.yml/application.properties (Spring-relevant application)
or Apollo or any similiar configuration manager in enabling to log contents - Support getting TraceId from HttpServletRequest Header
- Support configurable time period for logging upon class/method level
- Support generic methods and generic parameters
- Support log within multi-threads
- Support anonymous class
Support print line number of return statement before printing its result- Normalize line-number-table attribute of modified classes
<dependency>
<groupId>com.yuangancheng</groupId>
<artifactId>logtool</artifactId>
<version>0.0.1</version>
</dependency>
- Annotate a class with
@EnableTraceLog
. - Annotate a method with
@TraceLog
in the previous annotated class.
For example, there is a class and an affiliated method.Then, you will see printed@EnableTraceLog public class Dog { ... @TraceLog private int bark(int frequency) { ... return 1; ... } ... }
bark's
parameters and result on the screen when invoke this method at runtime.
-
Enable logging request id
In general, every servlet request should has an id within its header. So if you want to log it, you need to configure it. For example,
Given@EnableTraceLog(reqIdName="abc")
,
Then, the logtool will get the request id by searching the request header fields with the nameabc
and the printed information of methods will include the request id. -
Enable static class-level-switch
The class-level-switch means a variable in a class. This variable can control the behaviour of logtool in the whole class at runtime. The value of this variable can be1
which represents "activate printing" or0
which represents "deactivate printing". This functionality is based on Spring annotation@Value
.
Given@EnableTraceLog(enableClassLevelSwitch = true, switchKey = "x.y.z")
,
Then, you need to ensure there exists a property pair namedx.y.z=1
orx.y.z=0
inapplication.properties
orapplication.yml
file. The logtool will generate a variable and put into the class. The logtool will control the behaviour of logging part in the whole class based on the property's value at runtime.- Enable dynamic class-level-switch (advanced)
Just using Apollo or any configuration manager which works perfectly with Spring annotation@Value
in your Spring application.
- Enable dynamic class-level-switch (advanced)
-
Enable static method-level-switch
The method-level-switch has a similiar meaning as class-level-switch. However, method-level-switch has a smaller granularity that it can only control the behaviour of a single method at runtime.
Given@TraceLog(enableMethodLevelSwitch = true, switchKey = "a.b.c")
Then, as the former one, you also need to ensure there exists a property pair nameda.b.c=1
ora.b.c=0
inapplication.properties
orapplication.yml
file and the logtool will generate a variable and put into the class. The logtool will control the behaviour of logging part of the method based on the property's value at runtime.- Enable dynamic method-level-switch (advanced)
The usage is the same as the "dynamic class-level-switch".
- Enable dynamic method-level-switch (advanced)
If you want to clone this project, then you'd better do following steps:
- Import project by Intellij IDEA.
- Add local jdk(version:1.8) library (
jdk\lib\tools.jar
) as a project's library manually.