Skip to content

Commit

Permalink
✨ Added parameters and return value logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mfalcier committed Jul 30, 2019
1 parent fd6acfa commit c1d5523
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

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

This library provides an annotation, `@LogMe` that will log your functions in order to monitor their execution and duration.
When a function is annotated, it will create a log before and after it's execution, showing it's execution time.
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.
When a function is annotated, it will create a log before and after it's execution, showing all the details.

For Example:

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

@LogMe
fun foo(){
fun foo(name: String){
Thread.sleep(1000) // I.E.
log.info("Hello World!")
val result = "Hello $name!"
log.info(result)
return result
}
}
```

When foo() is executed will result in:

``` shell
23:29:26.969 [main] INFO com.mfalcier.yessir.Foo - com.mfalcier.yessir.Foo.foo has started its execution
23:29:27.971 [main] INFO com.mfalcier.yessir.Foo - Hello World!
23:29:27.972 [main] INFO com.mfalcier.yessir.Foo - com.mfalcier.yessir.Foo.foo has ended its execution after 1000ms
23:29:26.969 [main] INFO com.mfalcier.yessir.Foo - [com.mfalcier.yessir.Foo.foo] has started its execution with parameters {name=Marco}
23:29:27.971 [main] INFO com.mfalcier.yessir.Foo - Hello Marco!
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!]
```

The `@LogMe` annotation can also be used on classes, in order to automatically log each of its method:
Expand Down Expand Up @@ -125,7 +127,7 @@ Maven:
<dependency>
<groupId>com.github.mfalcier</groupId>
<artifactId>YesSir</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>
</dependency>
...
<repositories>
Expand Down
15 changes: 14 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<artifactId>yessir</artifactId>
<packaging>jar</packaging>
<name>YesSir</name>
<version>1.0.0</version>
<version>1.1.0</version>

<properties>
<java.version>1.8</java.version>
Expand Down Expand Up @@ -121,6 +121,19 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.mfalcier.yessir.MainKt</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
25 changes: 20 additions & 5 deletions src/main/kotlin/com/mfalcier/yessir/YesSir.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class YesSir {
val stopNanos = System.nanoTime()
val lengthMillis = TimeUnit.NANOSECONDS.toMillis(stopNanos - startNanos)

afterExecution(joinPoint, lengthMillis)
afterExecution(joinPoint, lengthMillis, result)

return result
}
Expand All @@ -44,18 +44,33 @@ class YesSir {
val codeSignature = joinPoint.signature as CodeSignature
val cls = codeSignature.declaringType.name
val methodName = codeSignature.name
val log = LoggerFactory.getLogger(cls)
val parameterNames = codeSignature.parameterNames
val parameterValues = joinPoint.args

val parameters = mutableMapOf<String, Any?>()
parameterNames.forEachIndexed { index, name ->
parameters[name] = parameterValues[index]
}

log.info("$cls.$methodName has started its execution")
val log = LoggerFactory.getLogger(cls)
if (parameters.isEmpty()) {
log.info("[$cls.$methodName] has started its execution")
} else {
log.info("[$cls.$methodName] has started its execution with parameters: $parameters")
}
}

private fun afterExecution(joinPoint: JoinPoint, lengthMillis: Long) {
private fun afterExecution(joinPoint: JoinPoint, lengthMillis: Long, result: Any?) {

val signature = joinPoint.signature
val cls = signature.declaringType.name
val methodName = signature.name
val log = LoggerFactory.getLogger(cls)

log.info("$cls.$methodName has ended its execution after ${lengthMillis}ms")
if (result == null) {
log.info("[$cls.$methodName] has ended its execution after ${lengthMillis}ms")
} else {
log.info("[$cls.$methodName] has ended its execution after ${lengthMillis}ms with result: [$result]")
}
}
}

0 comments on commit c1d5523

Please sign in to comment.