Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 87 additions & 1 deletion Developer_Guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,4 +363,90 @@ global with sharing class CustomerWebServiceDemo {

> **_NOTE:_** In this example We've put the rest processors as a subclasses in owr web service just to simplify reading the code but feel free to keep them as independent classes.

# Advanced Topics
# Advanced Topics

## Logging REST Requests with libak_RestFramework

By default, the REST Framework does not log any requests. However, logging can be enabled with a few simple steps, allowing you to capture detailed information about each REST request and its lifecycle. This guide outlines how to implement logging using the framework's extensibility.

---

### Enabling Logging
To enable logging, follow these steps:

### 1. Implement the `libak_IRestLogger` Interface
The `libak_IRestLogger` interface defines three methods that you must implement to customize logging behavior.

#### Methods Overview
- **`void initLog(RestRequest request)`**
Used for initializing log entries with details about the incoming request.
- **`void addErrorDetails(Exception exc)`**
Captures and adds error information to the log.
- **`void createLog()`**
Finalizes and persists the log entry (e.g., inserts a database record or writes to an external system).

### 2. Inject Logging Dependency
Pass your logger class when invoking `libak_RestFramework.handleRequest`.

---

### Step 1: Create a Logger Class
Define a new class implementing the `libak_IRestLogger` interface.

```java
public with sharing class RestLogger implements libak_IRestLogger {
public void initLog(RestRequest request) {
// Initialize a log entry with details from the incoming REST request.
}

public void addErrorDetails(Exception exc) {
// Add error-specific details to the log entry.
}

public void createLog() {
// Finalize and save the log entry.
}
}
```

---

### Step 2: Update the HTTP Methods
Modify your REST Web Service class to enable logging. Pass your custom logger class as a dependency in the `handleRequest` method.

```java
@HttpGet
global static void doGet() {
libak_RestFramework.handleRequest(CustomerRestRouter.class, RestLogger.class);
}

@HttpPost
global static void doPost() {
libak_RestFramework.handleRequest(CustomerRestRouter.class, RestLogger.class);
}

@HttpPut
global static void doPut() {
libak_RestFramework.handleRequest(CustomerRestRouter.class, RestLogger.class);
}

@HttpDelete
global static void doDelete() {
libak_RestFramework.handleRequest(CustomerRestRouter.class, RestLogger.class);
}
```

---

### How It Works
After completing these steps, the REST Framework will invoke your `RestLogger` implementation for every incoming request. The logger will:
- Record request details during the `initLog` method.
- Append error information in the event of an exception via the `addErrorDetails` method.
- Save the log (e.g., as a Salesforce record) when the `createLog` method is called.

This flexible approach lets you tailor logging to meet your organization’s auditing, debugging, or monitoring requirements.

---

### Conclusion
By implementing the `libak_IRestLogger` interface and injecting your logger class into the request-handling pipeline, you gain full control over how REST requests are logged. This setup ensures your system captures the information you need for effective troubleshooting and operational visibility.
8 changes: 4 additions & 4 deletions demo-app/main/default/classes/CustomerWebServiceDemo.cls
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
global with sharing class CustomerWebServiceDemo {
@HttpGet
global static void doGet() {
libak_RestFramework.handleRequest(CustomerRestRouter.class);
libak_RestFramework.handleRequest(CustomerRestRouter.class, RestLogger.class);
}
@HttpPost
global static void doPost() {
libak_RestFramework.handleRequest(CustomerRestRouter.class);
libak_RestFramework.handleRequest(CustomerRestRouter.class, RestLogger.class);
}
@HttpPut
global static void doPut() {
libak_RestFramework.handleRequest(CustomerRestRouter.class);
libak_RestFramework.handleRequest(CustomerRestRouter.class, RestLogger.class);
}
@HttpDelete
global static void doDelete() {
libak_RestFramework.handleRequest(CustomerRestRouter.class);
libak_RestFramework.handleRequest(CustomerRestRouter.class, RestLogger.class);
}

public class CustomerRestRouter extends libak_RestRouter {
Expand Down
13 changes: 13 additions & 0 deletions demo-app/main/default/classes/RestLogger.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public with sharing class RestLogger implements libak_IRestLogger {
public void initLog(RestRequest request) {
// Initialize a log entry with details from the incoming REST request.
}

public void addErrorDetails(Exception exc) {
// Add error-specific details to the log entry.
}

public void createLog() {
// Finalize and save the log entry.
}
}
5 changes: 5 additions & 0 deletions demo-app/main/default/classes/RestLogger.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
Loading