Skip to content

Tosan/tosan-tostring-builder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


Tosan Log Tools

toString builder and service logger utility

About The Project

This project try to solve two problems which are explained below:

  • In logging input or output of a service, object may contain sensitive data such as password, pan number, account balance, etc that can not show in plain format. some fields such as password should never be presented in log. some fields are sensitive but can be presented semi masked for debugging purposes.
    also you might want to present log in json format for later processing or might want it to be simple raw string for manual reading. implementing this requirement in every project leads to many code duplication and inconsistent formatting styles. its better you only state in toString method, how each field should be presented and leave the how's to other code. using this pattern leads to consistent log format. also changing the code is much easier because all the codes are in single place.
  • For logging service calls, best practice is to do such things in aspects. in a log aspect, you first log which service called. then you log input provided to service. in the end you log the result of service or the thrown exception. you also may log part of result based on log level. in addition, you can log execution time of service. doing such can get more complex, as you must choose one style for logging objects and service call data. in other words, if you log objects in json format, for consistency must log service data in json too. its ideal you have integrated tool for logging service call, which take in mind all these considerations.

This java project, provides integrated solution for above mentioned problems. First it provides interface for use in toString method and determine each field how should be shown. also, you can choose from two implementations provided for simple and json formatting. also, provides interface for use in aspect to log service call.

Getting Started

For start, you should add the following dependency to your pom:

<groupId>com.tosan.tools</groupId>
<artifactId>tosan-tostring-builder</artifactId>
<version>${version}</version>

for use in toString method you can code like this:

@Override
public String toString() {
final ToStringBuilder sb = new ToStringBuilderImpl(this);
        sb.append("superClass", super.toString());
        sb.append("name", name);
        sb.semiEncryptedAppend("code", code);
        sb.encryptedAppend("password", address);
        sb.middleEncryptedAppend("mobile", mobile);
        sb.rightEncryptedAppend("address", address);
        sb.leftEncryptedAppend("cityCode", cityCode);
        return sb.toString();
        }

ToStringBuilder is an interface which provides multiple methods to determine how the field should be presented. you can choose from three implementations:

  • ToStringBuilderImpl this implementation based on static field format delegates formatting to JsonToStringBuilderImpl or SimpleToStringBuilderImpl implementation. default format for the field is json. you can set it to simple format by setter method before creating new instance.
  • JsonToStringBuilderImpl this implementation formats object`s fields in json format.
  • SimpleToStringBuilderImpl this implementation formats object`s fields in simple format.

In log aspect, you can use `ServiceLogger` as simple as below:
  @Aspect
public class LogAspect {
  private static final ServiceLogger serviceLogger = new ServiceLoggerImpl();

  @Around("com.tosan.modern.serviceCall()")
  public Object logFacadeCall(ProceedingJoinPoint pjp) throws Throwable {
    return serviceLogger.log(pjp);
  }
}

ServiceLoggerImpl this implementation based on ToStringBuilderImpl.getFormat() delegates formatting to JsonServiceLoggerImpl or SimpleServiceLoggerImpl implementation. serviceLogger.log(pjp) method, hides all the complexity of logging class name, called method, inputs, output, exception and execution time of called method.

(back to top)

Prerequisites

This Library requires java version 8 or above.

Contributing

Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement".

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

(back to top)

License

Distributed under the Apache License. See LICENSE.txt for more information.

(back to top)