Skip to content

Commit 84e55b8

Browse files
committed
Add logging section to the documentation
1 parent 4496e38 commit 84e55b8

File tree

4 files changed

+109
-5
lines changed

4 files changed

+109
-5
lines changed

Developer_Guide.md

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,4 +363,90 @@ global with sharing class CustomerWebServiceDemo {
363363

364364
> **_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.
365365
366-
# Advanced Topics
366+
# Advanced Topics
367+
368+
## Logging REST Requests with libak_RestFramework
369+
370+
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.
371+
372+
---
373+
374+
### Enabling Logging
375+
To enable logging, follow these steps:
376+
377+
### 1. Implement the `libak_IRestLogger` Interface
378+
The `libak_IRestLogger` interface defines three methods that you must implement to customize logging behavior.
379+
380+
#### Methods Overview
381+
- **`void initLog(RestRequest request)`**
382+
Used for initializing log entries with details about the incoming request.
383+
- **`void addErrorDetails(Exception exc)`**
384+
Captures and adds error information to the log.
385+
- **`void createLog()`**
386+
Finalizes and persists the log entry (e.g., inserts a database record or writes to an external system).
387+
388+
### 2. Inject Logging Dependency
389+
Pass your logger class when invoking `libak_RestFramework.handleRequest`.
390+
391+
---
392+
393+
### Step 1: Create a Logger Class
394+
Define a new class implementing the `libak_IRestLogger` interface.
395+
396+
```java
397+
public with sharing class RestLogger implements libak_IRestLogger {
398+
public void initLog(RestRequest request) {
399+
// Initialize a log entry with details from the incoming REST request.
400+
}
401+
402+
public void addErrorDetails(Exception exc) {
403+
// Add error-specific details to the log entry.
404+
}
405+
406+
public void createLog() {
407+
// Finalize and save the log entry.
408+
}
409+
}
410+
```
411+
412+
---
413+
414+
### Step 2: Update the HTTP Methods
415+
Modify your REST Web Service class to enable logging. Pass your custom logger class as a dependency in the `handleRequest` method.
416+
417+
```java
418+
@HttpGet
419+
global static void doGet() {
420+
libak_RestFramework.handleRequest(CustomerRestRouter.class, RestLogger.class);
421+
}
422+
423+
@HttpPost
424+
global static void doPost() {
425+
libak_RestFramework.handleRequest(CustomerRestRouter.class, RestLogger.class);
426+
}
427+
428+
@HttpPut
429+
global static void doPut() {
430+
libak_RestFramework.handleRequest(CustomerRestRouter.class, RestLogger.class);
431+
}
432+
433+
@HttpDelete
434+
global static void doDelete() {
435+
libak_RestFramework.handleRequest(CustomerRestRouter.class, RestLogger.class);
436+
}
437+
```
438+
439+
---
440+
441+
### How It Works
442+
After completing these steps, the REST Framework will invoke your `RestLogger` implementation for every incoming request. The logger will:
443+
- Record request details during the `initLog` method.
444+
- Append error information in the event of an exception via the `addErrorDetails` method.
445+
- Save the log (e.g., as a Salesforce record) when the `createLog` method is called.
446+
447+
This flexible approach lets you tailor logging to meet your organization’s auditing, debugging, or monitoring requirements.
448+
449+
---
450+
451+
### Conclusion
452+
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.

demo-app/main/default/classes/CustomerWebServiceDemo.cls

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
global with sharing class CustomerWebServiceDemo {
33
@HttpGet
44
global static void doGet() {
5-
libak_RestFramework.handleRequest(CustomerRestRouter.class);
5+
libak_RestFramework.handleRequest(CustomerRestRouter.class, RestLogger.class);
66
}
77
@HttpPost
88
global static void doPost() {
9-
libak_RestFramework.handleRequest(CustomerRestRouter.class);
9+
libak_RestFramework.handleRequest(CustomerRestRouter.class, RestLogger.class);
1010
}
1111
@HttpPut
1212
global static void doPut() {
13-
libak_RestFramework.handleRequest(CustomerRestRouter.class);
13+
libak_RestFramework.handleRequest(CustomerRestRouter.class, RestLogger.class);
1414
}
1515
@HttpDelete
1616
global static void doDelete() {
17-
libak_RestFramework.handleRequest(CustomerRestRouter.class);
17+
libak_RestFramework.handleRequest(CustomerRestRouter.class, RestLogger.class);
1818
}
1919

2020
public class CustomerRestRouter extends libak_RestRouter {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public with sharing class RestLogger implements libak_IRestLogger {
2+
public void initLog(RestRequest request) {
3+
// Initialize a log entry with details from the incoming REST request.
4+
}
5+
6+
public void addErrorDetails(Exception exc) {
7+
// Add error-specific details to the log entry.
8+
}
9+
10+
public void createLog() {
11+
// Finalize and save the log entry.
12+
}
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>62.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>

0 commit comments

Comments
 (0)