Skip to content

Commit c1d5523

Browse files
committed
✨ Added parameters and return value logging
1 parent fd6acfa commit c1d5523

File tree

3 files changed

+44
-14
lines changed

3 files changed

+44
-14
lines changed

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
![Image of YesSir](http://www.quickmeme.com/img/ea/ea716f8278f2c0aa9044513ef3b8acc2362567dc1a4456a27343f21a9f0b519f.jpg)
55

6-
This library provides an annotation, `@LogMe` that will log your functions in order to monitor their execution and duration.
7-
When a function is annotated, it will create a log before and after it's execution, showing it's execution time.
6+
This library provides an annotation, `@LogMe` that will log your functions in order to monitor their execution (with input parameters and results if those are available) and duration.
7+
When a function is annotated, it will create a log before and after it's execution, showing all the details.
88

99
For Example:
1010

@@ -13,19 +13,21 @@ class Foo {
1313
private val log = LoggerFactory.getLogger(this::class.java)
1414

1515
@LogMe
16-
fun foo(){
16+
fun foo(name: String){
1717
Thread.sleep(1000) // I.E.
18-
log.info("Hello World!")
18+
val result = "Hello $name!"
19+
log.info(result)
20+
return result
1921
}
2022
}
2123
```
2224

2325
When foo() is executed will result in:
2426

2527
``` shell
26-
23:29:26.969 [main] INFO com.mfalcier.yessir.Foo - com.mfalcier.yessir.Foo.foo has started its execution
27-
23:29:27.971 [main] INFO com.mfalcier.yessir.Foo - Hello World!
28-
23:29:27.972 [main] INFO com.mfalcier.yessir.Foo - com.mfalcier.yessir.Foo.foo has ended its execution after 1000ms
28+
23:29:26.969 [main] INFO com.mfalcier.yessir.Foo - [com.mfalcier.yessir.Foo.foo] has started its execution with parameters {name=Marco}
29+
23:29:27.971 [main] INFO com.mfalcier.yessir.Foo - Hello Marco!
30+
23:29:27.972 [main] INFO com.mfalcier.yessir.Foo - [com.mfalcier.yessir.Foo.foo] has ended its execution after 1000ms with result [Hello Marco!]
2931
```
3032

3133
The `@LogMe` annotation can also be used on classes, in order to automatically log each of its method:
@@ -125,7 +127,7 @@ Maven:
125127
<dependency>
126128
<groupId>com.github.mfalcier</groupId>
127129
<artifactId>YesSir</artifactId>
128-
<version>1.0.0</version>
130+
<version>1.1.0</version>
129131
</dependency>
130132
...
131133
<repositories>

pom.xml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<artifactId>yessir</artifactId>
66
<packaging>jar</packaging>
77
<name>YesSir</name>
8-
<version>1.0.0</version>
8+
<version>1.1.0</version>
99

1010
<properties>
1111
<java.version>1.8</java.version>
@@ -121,6 +121,19 @@
121121
</execution>
122122
</executions>
123123
</plugin>
124+
<plugin>
125+
<groupId>org.apache.maven.plugins</groupId>
126+
<artifactId>maven-jar-plugin</artifactId>
127+
<version>2.6</version>
128+
<configuration>
129+
<archive>
130+
<manifest>
131+
<addClasspath>true</addClasspath>
132+
<mainClass>com.mfalcier.yessir.MainKt</mainClass>
133+
</manifest>
134+
</archive>
135+
</configuration>
136+
</plugin>
124137
</plugins>
125138
</build>
126139

src/main/kotlin/com/mfalcier/yessir/YesSir.kt

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class YesSir {
3434
val stopNanos = System.nanoTime()
3535
val lengthMillis = TimeUnit.NANOSECONDS.toMillis(stopNanos - startNanos)
3636

37-
afterExecution(joinPoint, lengthMillis)
37+
afterExecution(joinPoint, lengthMillis, result)
3838

3939
return result
4040
}
@@ -44,18 +44,33 @@ class YesSir {
4444
val codeSignature = joinPoint.signature as CodeSignature
4545
val cls = codeSignature.declaringType.name
4646
val methodName = codeSignature.name
47-
val log = LoggerFactory.getLogger(cls)
47+
val parameterNames = codeSignature.parameterNames
48+
val parameterValues = joinPoint.args
49+
50+
val parameters = mutableMapOf<String, Any?>()
51+
parameterNames.forEachIndexed { index, name ->
52+
parameters[name] = parameterValues[index]
53+
}
4854

49-
log.info("$cls.$methodName has started its execution")
55+
val log = LoggerFactory.getLogger(cls)
56+
if (parameters.isEmpty()) {
57+
log.info("[$cls.$methodName] has started its execution")
58+
} else {
59+
log.info("[$cls.$methodName] has started its execution with parameters: $parameters")
60+
}
5061
}
5162

52-
private fun afterExecution(joinPoint: JoinPoint, lengthMillis: Long) {
63+
private fun afterExecution(joinPoint: JoinPoint, lengthMillis: Long, result: Any?) {
5364

5465
val signature = joinPoint.signature
5566
val cls = signature.declaringType.name
5667
val methodName = signature.name
5768
val log = LoggerFactory.getLogger(cls)
5869

59-
log.info("$cls.$methodName has ended its execution after ${lengthMillis}ms")
70+
if (result == null) {
71+
log.info("[$cls.$methodName] has ended its execution after ${lengthMillis}ms")
72+
} else {
73+
log.info("[$cls.$methodName] has ended its execution after ${lengthMillis}ms with result: [$result]")
74+
}
6075
}
6176
}

0 commit comments

Comments
 (0)