Skip to content

Commit

Permalink
✨ Working version
Browse files Browse the repository at this point in the history
  • Loading branch information
mfalcier committed Jul 5, 2019
1 parent 2dd82bc commit 2a7ceb1
Show file tree
Hide file tree
Showing 6 changed files with 252 additions and 52 deletions.
124 changes: 122 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,123 @@
# YesSir: a simple Kotlin Method Logger
# YesSir
## A Kotlin Autologger

A simple Kotlin Method Logger.
![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.

For Example:

```
class Foo {
private val log = LoggerFactory.getLogger(this::class.java)
@LogMe
fun foo(){
Thread.sleep(1000) // I.E.
log.info("Hello World!")
}
}
```

When foo() is executed will result in:

```
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]
```

The `@LogMe` annotation can also be used on classes, in order to automatically log each of its method:

```
@LogMe
class Foo {
private val log = LoggerFactory.getLogger(this::class.java)
fun foo(){
Thread.sleep(1000) // I.E.
log.info("Hello World!")
}
}
```

In order to weave, your POM.xml plugins section must at least contains:

```
<plugins>
<!-- KAPT EXECUTION -->
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>kapt</id>
<goals>
<goal>kapt</goal>
</goals>
</execution>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals> <goal>compile</goal> </goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals> <goal>test-compile</goal> </goals>
</execution>
</executions>
</plugin>
<!-- WEAVER -->
<plugin>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-maven-plugin</artifactId>
<version>0.14.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
<executions>
<execution>
<goals>
<goal>ajc</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
<!-- FAT JAR -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
```

## COMING SOON
Some way to directly include this library as a dependency to your project :sweat_smile:
98 changes: 65 additions & 33 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,51 @@

<properties>
<java.version>1.8</java.version>
<kotlin.version>1.2.40</kotlin.version>
<kotlin.version>1.3.40</kotlin.version>
<aspectj.version>1.9.4</aspectj.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven-plugin-version>1.0.0</maven-plugin-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<!-- KOTLIN -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>${kotlin.version}</version>
</dependency>

<!-- TEST -->
<!-- LOGS -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
<groupId>io.symphonia</groupId>
<artifactId>lambda-logging</artifactId>
<version>1.0.0</version>
<classifier>no-config</classifier>
</dependency>

<!-- ASPECTJ -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test-junit</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>

<build>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
Expand All @@ -69,6 +62,12 @@
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>kapt</id>
<goals>
<goal>kapt</goal>
</goals>
</execution>
<execution>
<id>compile</id>
<phase>compile</phase>
Expand All @@ -81,15 +80,48 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-maven-plugin</artifactId>
<version>0.14.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
<executions>
<execution>
<goals>
<goal>ajc</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>

<extensions>
<extension>
<groupId>org.springframework.build</groupId>
<artifactId>aws-maven</artifactId>
<version>5.0.0.RELEASE</version>
</extension>
</extensions>
</build>

</project>
Expand Down
4 changes: 0 additions & 4 deletions src/main/kotlin/com/mfalcier/yessir/Log.kt

This file was deleted.

4 changes: 4 additions & 0 deletions src/main/kotlin/com/mfalcier/yessir/LogMe.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.mfalcier.yessir

@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
annotation class LogMe
61 changes: 61 additions & 0 deletions src/main/kotlin/com/mfalcier/yessir/YesSir.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.mfalcier.yessir

import org.aspectj.lang.JoinPoint
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.Around
import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Pointcut
import org.aspectj.lang.reflect.CodeSignature
import org.slf4j.LoggerFactory
import java.util.concurrent.TimeUnit


@Aspect
class YesSir {

@Pointcut("within(@com.mfalcier.yessir.LogMe *)")
fun withinAnnotatedClass() {
}

@Pointcut("execution(!synthetic * *(..)) && withinAnnotatedClass()")
fun methodInsideAnnotatedType() {
}

@Pointcut("execution(@com.mfalcier.yessir.LogMe * *(..)) || methodInsideAnnotatedType()")
fun method() {
}

@Around("method()")
fun logMe(joinPoint: ProceedingJoinPoint): Any? {
beforeExecution(joinPoint)

val startNanos = System.nanoTime()
val result = joinPoint.proceed()
val stopNanos = System.nanoTime()
val lengthMillis = TimeUnit.NANOSECONDS.toMillis(stopNanos - startNanos)

afterExecution(joinPoint, lengthMillis)

return result
}

private fun beforeExecution(joinPoint: JoinPoint) {

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

log.info("$cls.$methodName has started its execution")
}

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

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")
}
}
13 changes: 0 additions & 13 deletions src/test/kotlin/com/mfalcier/yessir/UnitTests.kt

This file was deleted.

0 comments on commit 2a7ceb1

Please sign in to comment.