You could use AspectJ to intercept field assignments; in this way you can avoid cluttering your logic with logging code.
SomeClass.java:
public class SomeClass {
private static int someVariableA;
private static int someVariableB;
private static int someVariableC;
private static int someVariableD;
public static void main(String[] args) {
someVariableA = 0;
someVariableB = 5;
someVariableC = 8;
someVariableD = someVariableA + someVariableB;
someVariableD = 9;
}
}
SomeAspect.java:
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class SomeAspect {
@After("set(static * SomeClass.*) && args(newValue)")
public void afterFieldSet(JoinPoint joinPoint, Object newValue) {
System.out.println(joinPoint.getSignature().getName() + " = " + newValue + " at " + joinPoint.getSourceLocation());
}
}
The console output will be:
someVariableA = 0 at SomeClass.java:9
someVariableB = 5 at SomeClass.java:10
someVariableC = 8 at SomeClass.java:11
someVariableD = 5 at SomeClass.java:12
someVariableD = 9 at SomeClass.java:13
When you want to disable the logging, simply remove the aspect.
For sake of completeness, I also post here the pom.xml used for the build:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>aspectj-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.11</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<complianceLevel>${maven.compiler.source}</complianceLevel>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>SomeClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeScope>runtime</includeScope>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.13</version>
</dependency>
</dependencies>
</project>
To run this example, just launch mvn clean package
and then java -jar target/aspectj-example.jar
.
You can found more information about AspectJ on its website. I hope it helped.