auto-log

Auto-log is an automatic log monitoring framework designed for Java.

I have already written two:

Java annotations and Spring AOP implement automatic logging

Java annotations combined with Spring AOP implement log traceId unique identification

After the previous two code implementation, the following problems are still found:

(1) The use of annotations is still not convenient.

If you specify @autolog on every method, it would still be a hassle. Individuals don’t want to go to so much trouble when they use it.

I wanted to add class-based annotations.

Later consider whether AOP’s sweep package scope can be dynamically specified, based on packages.

(2) The processing of logs is too single.

Let’s say I want to add audit logs for all operations and store them in the database. Meanwhile, the log output does not change.

Found that the previous logging framework is not well supported. You want to add an interceptor for logging output.

(3) Filtering of parameters

I used to implement it directly based on toString(), but found that some people were lazy and didn’t write toString(like me), so the argument printed information didn’t make sense.

The result is FastJSON, but it introduces new problems, such as HttpRequest and other parameters serialized directly error, so we want to add parameter-based filter implementation.

Demand is the best creative power.

At present, a total of 12 iterations have been completed, and the above three features have been satisfied.

features

  • Flexible configuration based on annotations + bytecode

  • Automatically ADAPTS to common logging frameworks

  • Support for programmatic calls

  • Annotated support for perfect spring integration

  • Support for spring-boot integration

  • Supports slow log threshold, time, input and output parameters, exception information and other common attributes

  • Supports the traceId feature

  • Class level definition annotations are supported

  • Support for custom interceptors and filters

Quick start

Maven is introduced into

<dependency>
    <group>com.github.houbb</group>
    <artifact>auto-log-core</artifact>
    <version>${latest version}</version>
</dependency>
Copy the code

An introduction to case

UserService userService = AutoLogHelper.proxy(new UserServiceImpl());
userService.queryLog("1");
Copy the code
  • Log as follows
[the INFO] [the 2020-05-29 16:24:06. 227] [the main] [C.G.H.A.L.C.S.I.A utoLogMethodInterceptor. Invoke] - public Java. Lang. String com.github.houbb.auto.log.test.service.impl.UserServiceImpl.queryLog(java.lang.String) param is [1] [INFO] [2020-05-29 16:24:06. 228] [the main] [C.G.H.A.L.C.S.I.A utoLogMethodInterceptor. Invoke] - public Java. Lang. String com.github.houbb.auto.log.test.service.impl.UserServiceImpl.queryLog(java.lang.String) result is result-1Copy the code

code

The method is as follows:

  • UserService.java
public interface UserService {

    String queryLog(final String id);

}
Copy the code
  • UserServiceImpl.java

Use the @autolog annotation to specify which logging method is required.

public class UserServiceImpl implements UserService {

    @Override
    @AutoLog
    public String queryLog(String id) {
        return "result-"+id; }}Copy the code

An example of TraceId

code

UserService service =  AutoLogProxy.getProxy(new UserServiceImpl());
service.traceId("1");
Copy the code

The traceId methods are as follows:

@AutoLog
@TraceId
public String traceId(String id) {
    return id+"1";
}
Copy the code

The test results

Information: [ba7ddaded5a644e5a58fbd276b6657af] < traceId > into arguments: [1]. Information: [ba7ddaded5a644e5a58fbd276b6657af] < traceId > reference: 1-1.Copy the code

One of ba7ddaded5a644e5a58fbd276b6657af is corresponding traceId, throughout the thread cycle, can facilitate our log viewer.

Note that

@AutoLog

The attributes of the core annotation @Autolog are described as follows:

attribute type The default value instructions
enable boolean true Whether to enable
param boolean true Whether to print input parameters
result boolean true Whether to print the parameter
costTime boolean false Print time
exception boolean true Check whether printing is abnormal.
slowThresholdMills long – 1 If the value is greater than or equal to 0 and the time consumed exceeds the configured value, slow logs are generated
description string “” Method description, method name is selected by default
interceptor Class[] The default implementation Interceptor implementation, support to specify multiple and custom
paramFilter Class empty Input parameter filter, support custom

@TraceId

@traceid is placed on methods that need to set TraceId, such as the Controller layer, the consumer of MQ, the recipient of RPC requests, etc.

attribute type The default value instructions
id Class The default is uuid Implementation policy of traceId
putIfAbsent boolean false Whether to set a value when the current thread has no value
enable boolean true Whether to enable
interceptor Class[] The default implementation Interceptor implementation, support to specify multiple and custom

Custom Policies

Custom log interceptor

Built-in interceptor

AutoLogInterceptor is implemented by default

define

Directly inherited from AbstractAutoLogInterceptor class, and implement the corresponding method.

public class MyAutoLogInterceptor extends AbstractAutoLogInterceptor {

    @Override
    protected void doBefore(AutoLog autoLog, IAutoLogInterceptorContext context) {
        System.out.println("Custom input parameter:" + Arrays.toString(context.filterParams()));
    }

    @Override
    protected void doAfter(AutoLog autoLog, Object result, IAutoLogInterceptorContext context) {
        System.out.println("Custom parameter:" + result);
    }

    @Override
    protected void doException(AutoLog autoLog, Exception exception, IAutoLogInterceptorContext context) {
        System.out.println(Custom exception:); exception.printStackTrace(); }}Copy the code

use

So that log output will use the policy specified above.

@AutoLog(interceptor = MyAutoLogInterceptor.class)
public String my(a) {
    return "Custom Policy";
}
Copy the code

Custom input Parameter Filter (paramFilter)

built-in

WebParamFilter is used to filter objects that cannot be directly serialized by JSON, such as HttpRequest HttpServlet.

The custom

Implement the corresponding methods directly from the AbstractParamFilter class.

public class MyParamFilter extends AbstractParamFilter {

    @Override
    protected Object[] doFilter(Object[] params) {
        Object[] newParams = new Object[1];
        newParams[0] = "Set the value I want.";
        returnnewParams; }}Copy the code

use

Specify the corresponding parameter filter. This way, whatever the input parameter is, will become the set I want value we specify.

@AutoLog(paramFilter = MyParamFilter.class)
public String paramFilter(a) {
    return "Custom input filter";
}
Copy the code

Spring integration

See SpringServiceTest for a complete example

Annotation statement

Use @enableautolog to enable automatic log output

@Configurable
@ComponentScan(basePackages = "com.github.houbb.auto.log.test.service")
@EnableAutoLog
public class SpringConfig {}Copy the code

The test code

@ContextConfiguration(classes = SpringConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringServiceTest {

    @Autowired
    private UserService userService;

    @Test
    public void queryLogTest(a) {
        userService.queryLog("1"); }}Copy the code
  • The output
Information: public java.lang.String com.github.houbb.auto.log.test.service.impl.UserServiceImpl.queryLog(java.lang.String) param is [1] on May 30, 2020 com 12:17:51 afternoon. Making. Houbb. Auto. The core. Support. The interceptor. AutoLogMethodInterceptor info information: public java.lang.String com.github.houbb.auto.log.test.service.impl.UserServiceImpl.queryLog(java.lang.String) result is Result - 1 May 30, 2020 12:17:51 afternoon org. Springframework. Context. Support. GenericApplicationContext doCloseCopy the code

Integrated use of Springboot

Maven is introduced into

<dependency>
    <groupId>com.github.houbb</groupId>
    <artifactId>auto-log-springboot-starter</artifactId>
    <version>The latest version</version>
</dependency>
Copy the code

Just import the JAR and configure nothing else.

Use the same way as Spring.

test

@Autowired
private UserService userService;

@Test
public void queryLogTest(a) {
    userService.query("spring-boot");
}
Copy the code

Open source address

Making: github.com/houbb/auto-…

Gitee: gitee.com/houbinbin/a…

Road-Map

  • Optimize the method path name in the log

Consider completing the corresponding class information

  • Global configuration

For example, set the global slow log threshold

  • The JVM – the sandbox feature

  • Compile-time annotations feature

The original address

Java annotations combined with Spring AOP automatic logging add interceptors and filters