Skip to content

Commit

Permalink
Update to 1.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
wsargent committed Jan 19, 2022
1 parent c51d66c commit e84bf09
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 56 deletions.
58 changes: 32 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ First, we add the logstash implementation of Echopraxia to `build.gradle`:

```groovy
dependencies {
implementation 'com.tersesystems.echopraxia:logstash:1.1.0'
implementation 'com.tersesystems.echopraxia:scripting:1.1.0'
implementation 'com.tersesystems.echopraxia:logstash:1.1.1'
implementation 'com.tersesystems.echopraxia:scripting:1.1.1'
// typically you also want the latest version of logstash-logback-encoder as well..
implementation 'net.logstash.logback:logstash-logback-encoder:7.0.1'
Expand All @@ -24,8 +24,8 @@ configurations {
}
dependencies {
implementation 'com.tersesystems.echopraxia:log4j:1.1.0'
implementation 'com.tersesystems.echopraxia:scripting:1.1.0'
implementation 'com.tersesystems.echopraxia:log4j:1.1.1'
implementation 'com.tersesystems.echopraxia:scripting:1.1.1'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-log4j2'
Expand Down Expand Up @@ -60,25 +60,43 @@ private final Logger<HttpRequestFieldBuilder> logger = LoggerFactory.getLogger(g
});
```

We can also add conditions that allow us to debug code with targeted debugging statements. Here, we'll set up a debug logger based off the original logger:
We can also add conditions that allow us to debug code with targeted debugging statements.

```java
private final Condition debugCondition = ScriptCondition.create(false,
Paths.get("condition.tf"),
e -> logger.error("Script failed!", e));
public class Conditions {

private final Logger<HttpRequestFieldBuilder> debugLogger = logger.withCondition(debugCondition);
private static final Logger logger = LoggerFactory.getLogger();

// This should generally be global to the application, as it creates a watcher thread internally
private static final Path scriptDirectory = Paths.get("scripts").toAbsolutePath();

// Watch the directory
private static final ScriptWatchService scriptWatchService = new ScriptWatchService(scriptDirectory);

private static final ScriptHandle scriptHandle = scriptWatchService.watchScript(
scriptDirectory.resolve("condition.tf"), e -> logger.error(e.getMessage(), e));

// Creates a condition from a script and re-evaluates it whenever the script changes
public static final Condition debugCondition = ScriptCondition.create(scriptHandle);
}
```

This will connect to a [Tweakflow](https://twineworks.github.io/tweakflow/) script that can evaluate context fields passed in. In this case, we only want to return true if the remote address starts with `127`:
Here, we'll set up a debug logger based off the original logger:

```java
private final Logger<HttpRequestFieldBuilder> debugLogger = logger.withCondition(Conditions.debugCondition);
```
import * as std from "std";
alias std.strings as str;

This will connect to a [Tweakflow](https://twineworks.github.io/tweakflow/) script that can evaluate context fields passed in. In this case, we only want to return true if the remote address starts with `127`:

```
library echopraxia {
# level: the logging level
# fields: the dictionary of fields
#
function evaluate: (string level, dict fields) ->
str.starts_with?(fields[:request_remote_addr], "127");
fields[:request_remote_addr] != nil && str.starts_with?(fields[:request_remote_addr], "127");
}
```

Expand All @@ -87,7 +105,6 @@ Using a script is very useful for debugging as you can change conditions in the
Here's the whole `GreetingController` code:

```java
@RestController
public class GreetingController {

private static final String template = "Hello, %s!";
Expand All @@ -104,19 +121,8 @@ public class GreetingController {
return fb.requestFields(request);
});

// This should generally be global to the application, as it creates a watcher thread internally
private final Path scriptDirectory = Paths.get("scripts").toAbsolutePath();

private final ScriptWatchService scriptWatchService = new ScriptWatchService(scriptDirectory);

// Creates a condition from a script and re-evaluates it whenever the script changes
private final Condition debugCondition =
ScriptCondition.create(
scriptWatchService.watchScript(
scriptDirectory.resolve("condition.tf"), e -> logger.error(e.getMessage(), e)));

// Creates a debug logger that will filter out any requests that doesn't meet the condition.
private final Logger<HttpRequestFieldBuilder> debugLogger = logger.withCondition(debugCondition);
private final Logger<HttpRequestFieldBuilder> debugLogger = logger.withCondition(Conditions.debugCondition);

@GetMapping("/greeting")
public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
Expand Down
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'

implementation 'com.tersesystems.echopraxia:scripting:1.1.0'
implementation 'com.tersesystems.echopraxia:scripting:1.1.1'

// Logstash implementation
implementation 'com.tersesystems.echopraxia:logstash:1.1.0'
implementation 'com.tersesystems.echopraxia:logstash:1.1.1'
implementation 'net.logstash.logback:logstash-logback-encoder:7.0.1'

// Log4J implementation
// implementation 'com.tersesystems.echopraxia:log4j:1.1.0'
// implementation 'com.tersesystems.echopraxia:log4j:1.1.1'
// implementation 'org.springframework.boot:spring-boot-starter-log4j2'
// implementation 'org.apache.logging.log4j:log4j-layout-template-json:2.17.1'

Expand Down
15 changes: 6 additions & 9 deletions scripts/condition.tf
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
# https://marketplace.visualstudio.com/items?itemName=twineworks.tweakflow
# https://twineworks.github.io/tweakflow/reference.html
# https://twineworks.github.io/tweakflow/modules/std.html#std

import * as std from "std";

# local alias for imported library
alias std.strings as str;

# Documentation is found in std, i.e. str.starts_with? is in
# https://twineworks.github.io/tweakflow/modules/std.html#strings-starts_with?
# Note that starts_with? returns nil rather than false if a passed in field is nil

library echopraxia {

# level: the logging level
# fields: the dictionary of fields
#
function evaluate: (string level, dict fields) ->
fields[:request_remote_addr] == "127.0.0.1";
fields[:request_remote_addr] != nil && str.starts_with?(fields[:request_remote_addr], "127");

}
}
28 changes: 28 additions & 0 deletions src/main/java/com/example/restservice/Conditions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.restservice;

import com.tersesystems.echopraxia.Condition;
import com.tersesystems.echopraxia.Logger;
import com.tersesystems.echopraxia.LoggerFactory;
import com.tersesystems.echopraxia.scripting.ScriptCondition;
import com.tersesystems.echopraxia.scripting.ScriptHandle;
import com.tersesystems.echopraxia.scripting.ScriptWatchService;

import java.nio.file.Path;
import java.nio.file.Paths;

public class Conditions {

private static final Logger logger = LoggerFactory.getLogger();

// This should generally be global to the application, as it creates a watcher thread internally
private static final Path scriptDirectory = Paths.get("scripts").toAbsolutePath();

// Watch the directory
private static final ScriptWatchService scriptWatchService = new ScriptWatchService(scriptDirectory);

private static final ScriptHandle scriptHandle = scriptWatchService.watchScript(
scriptDirectory.resolve("condition.tf"), e -> logger.error(e.getMessage(), e));

// Creates a condition from a script and re-evaluates it whenever the script changes
public static final Condition debugCondition = ScriptCondition.create(scriptHandle);
}
29 changes: 11 additions & 18 deletions src/main/java/com/example/restservice/GreetingController.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package com.example.restservice;

import com.tersesystems.echopraxia.Condition;
import com.tersesystems.echopraxia.Logger;
import com.tersesystems.echopraxia.LoggerFactory;
import com.tersesystems.echopraxia.scripting.ScriptCondition;
import com.tersesystems.echopraxia.scripting.ScriptWatchService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.atomic.AtomicLong;

@RestController
Expand All @@ -24,21 +19,19 @@ public class GreetingController {

private final Logger<HttpRequestFieldBuilder> logger =
LoggerFactory.getLogger(getClass())
.withFieldBuilder(HttpRequestFieldBuilder.class);

// This should generally be global to the application, as it creates a watcher thread internally
private final Path scriptDirectory = Paths.get("scripts").toAbsolutePath();

private final ScriptWatchService scriptWatchService = new ScriptWatchService(scriptDirectory);

// Creates a condition from a script and re-evaluates it whenever the script changes
private final Condition debugCondition =
ScriptCondition.create(
scriptWatchService.watchScript(
scriptDirectory.resolve("condition.tf"), e -> logger.error(e.getMessage(), e)));
.withFieldBuilder(HttpRequestFieldBuilder.class)
.withFields(
fb -> {
// Any fields that you set in context you can set conditions on later,
// i.e. on the URI path, content type, or extra headers.
HttpServletRequest request =
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.getRequest();
return fb.requestFields(request);
});

// Creates a debug logger that will filter out any requests that doesn't meet the condition.
private final Logger<HttpRequestFieldBuilder> debugLogger = logger.withCondition(debugCondition);
private final Logger<HttpRequestFieldBuilder> debugLogger = logger.withCondition(Conditions.debugCondition);

@GetMapping("/greeting")
public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
Expand Down

0 comments on commit e84bf09

Please sign in to comment.