You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Middleware logging is a technique used in software development, particularly in web and microservices applications, to log important information about incoming requests, outgoing responses, and the operations performed by the application.
3
+
-[core-go/middleware](https://github.com/core-go/middleware) is designed to integrate with middleware logging seamlessly for existing Go libraries: [Echo](https://github.com/labstack/echo), [Gin](https://github.com/gin-gonic/gin), or net/http ([Gorilla mux](https://github.com/gorilla/mux), [Go-chi](https://github.com/go-chi/chi)), with any logging libraries ([zap](https://pkg.go.dev/go.uber.org/zap), [logrus](https://github.com/sirupsen/logrus)), to log request headers, request body, response status code, body content, response time, and size
4
+
- Especially, [core-go/middleware](https://github.com/core-go/middleware) supported to encrypt sensitive data, which is useful for Financial Transactions (to comply with <b>PCI-DSS</b> standards) and Healthcare (to comply with <b>HIPAA</b> regulations)
5
+
6
+
### A typical micro service
7
+
- When you zoom one micro service, the flow is as below, and you can see "middleware" in the full picture:
- <b>Log Request Method and URL</b>: Log the HTTP method (GET, POST, etc.) and the requested URL.
14
+
- <b>Log Request Headers</b>: Option to log request headers for debugging purposes.
15
+
- <b>Log Request Body</b>: Option to log the request body (with configurable size limits to avoid logging large payloads).
16
+
#### Benefits
17
+
- <b>Debugging</b>: Helps in tracing and debugging issues by providing complete information about incoming requests.
18
+
- <b>Monitoring</b>: Provides visibility into the types of requests being received.
19
+
20
+
### Response
21
+
#### Features
22
+
- <b>Log Response Status Code</b>: Log the HTTP status code of the response.
23
+
- <b>Log Response Headers</b>: Option to log response headers.
24
+
- <b>Log Response Body</b>: Option to log the response body (with configurable size limits to avoid logging large payloads).
25
+
#### Benefits
26
+
- <b>Debugging</b>: Assists in diagnosing issues by providing complete information about the responses sent by the server.
27
+
- <b>Auditing</b>: Helps in auditing and reviewing server responses for compliance and monitoring purposes.
28
+
29
+
### Response Time
30
+
#### Features
31
+
- <b>Log Response Time</b>: Calculate and log the time taken to process each request.
32
+
#### Benefits
33
+
- <b>Performance Monitoring</b>: Helps in identifying slow requests and performance bottlenecks.
34
+
- <b>Optimization</b>: Provides data to optimize and improve server response times.
35
+
36
+
### Response Size
37
+
#### Features
38
+
- <b>Log Response Size</b>: Log the size of the response payload in bytes.
39
+
#### Benefits
40
+
- <b>Bandwidth Monitoring</b>: Helps in monitoring and managing bandwidth usage.
41
+
- <b>Optimization</b>: Provides insights into the response sizes to optimize payloads and improve performance.
42
+
43
+
## Features
44
+
### Middleware Integration
45
+
#### Features
46
+
- <b>Middleware Function</b>: Designed to integrate seamlessly with existing Go libraries: [Echo](https://github.com/labstack/echo), [Gin](https://github.com/gin-gonic/gin), or net/http ([Gorilla mux](https://github.com/gorilla/mux), [Go-chi](https://github.com/go-chi/chi)).
47
+
- Sample for [Echo](https://github.com/labstack/echo) is at [go-sql-echo-sample](https://github.com/go-tutorials/go-sql-echo-sample)
48
+
- Sample for [Gin](https://github.com/gin-gonic/gin) is at [go-sql-gin-sample](https://github.com/go-tutorials/go-sql-gin-sample)
49
+
- Sample for [Gorilla mux](https://github.com/gorilla/mux) is at [go-sql-sample](https://github.com/go-tutorials/go-sql-sample)
50
+
- <b>Context Handling</b>: Pass context to handle request-specific data throughout the middleware chain.
51
+
#### Benefits
52
+
- <b>Ease of Use</b>: Simplifies the integration of logging into existing web applications.
53
+
- <b>Consistency</b>: Ensures consistent logging across different parts of the application.
54
+
55
+
### Logging Libraries Integration
56
+
- Do not depend on any logging libraries.
57
+
- Already supported to integrate with [zap](https://pkg.go.dev/go.uber.org/zap), [logrus](https://github.com/sirupsen/logrus)
58
+
- Can be integrated with any logging library.
59
+
60
+
### Sensitive Data Encryption
61
+
#### Features
62
+
- Mask/Encrypt sensitive data in the request and response bodies.
63
+
- Sensitive Data Identification: identify and encrypt specific fields in JSON payloads.
64
+
65
+
#### Benefits:
66
+
- <b>Security</b>: Protects sensitive information from being exposed in logs.
67
+
- <b>Compliance</b>: Helps meet security and compliance requirements by safeguarding sensitive data.
68
+
- <b>Ease of Use</b>: Simplifies the integration of encryption/masking into any existing applications.
69
+
- <b>Consistency</b>: Ensures that sensitive data is consistently encrypted or masked across all logged requests and responses
70
+
71
+
#### Samples:
72
+
- Sample for [Echo](https://github.com/labstack/echo) is at [go-sql-echo-sample](https://github.com/go-tutorials/go-sql-echo-sample)
73
+
- Sample for [Gin](https://github.com/gin-gonic/gin) is at [go-sql-gin-sample](https://github.com/go-tutorials/go-sql-gin-sample)
74
+
- Sample for [Gorilla mux](https://github.com/gorilla/mux) is at [go-sql-sample](https://github.com/go-tutorials/go-sql-sample)
75
+
76
+
### Enable/Disable Logging
77
+
#### Features
78
+
- <b>Enable/Disable Logging</b>: Allow users to turn on or off logging for requests, responses, headers, and bodies independently.
79
+
- <b>Logging Levels</b>: Support different logging levels (e.g., INFO, DEBUG, ERROR) to control the verbosity of logs.
80
+
#### Benefits
81
+
- <b>Flexibility</b>: Provides users with the flexibility to configure logging based on their needs and environment.
82
+
- <b>Efficiency</b>: Reduces overhead by allowing selective logging, especially in production environments.
83
+
84
+
### Asynchronous Logging
85
+
#### Features
86
+
- <b>Non-Blocking Logs</b>: Implement asynchronous logging to ensure that logging does not block request processing.
87
+
- <b>Log Buffering</b>: Use buffering to improve logging performance and reduce latency.
88
+
#### Benefits:
89
+
- <b>Performance</b>: Improves the overall performance of the application by reducing logging overhead.
90
+
- <b>Scalability</b>: Allows the application to handle high-throughput logging without performance degradation.
91
+
92
+
93
+
## Use Cases of Sensitive Data Encryption
94
+
### Financial Transactions
95
+
- <b>Benefit</b>: Encrypting sensitive financial data, such as credit card numbers and transaction details, helps comply with PCI-DSS standards and secures financial transactions from exposure in logs.
96
+
### Healthcare
97
+
- <b>Benefit</b>: Encrypting patient data such as medical records and health information in logs ensures compliance with HIPAA regulations and protects patient privacy.
98
+
### E-commerce
99
+
- <b>Benefit</b>: Protecting customer information, such as addresses and payment details, enhances customer trust and protects the e-commerce platform from potential data breaches.
100
+
101
+
## Benefits of Middleware Logging
102
+
#### Debugging and Troubleshooting
103
+
- Provides detailed logs that help developers debug and troubleshoot issues in the application by tracing the flow of requests and responses.
104
+
#### Monitoring and Alerting
105
+
- Enables monitoring of application performance and behavior, allowing for real-time alerting on errors, slow responses, and unusual activity.
106
+
#### Performance Optimization
107
+
- Logs performance metrics that can be analyzed to identify bottlenecks, optimize resource usage, and improve overall application performance.
108
+
#### Security and Compliance
109
+
- Helps in tracking access and usage patterns, detecting security incidents, and complying with regulatory requirements by logging relevant information.
110
+
#### Auditing
111
+
- Provides an audit trail of user actions and system operations, which is essential for security audits and forensic analysis.
112
+
113
+
## Conclusion
114
+
Middleware logging is a critical aspect of building robust, maintainable, and secure applications, providing valuable insights and aiding in the continuous improvement of the software.
6
115
7
116
## Installation
8
117
Please make sure to initialize a Go module before installing core-go/middleware:
@@ -16,17 +125,7 @@ Import:
16
125
import"github.com/core-go/middleware"
17
126
```
18
127
19
-
## Features
20
-
### log tracing at middleware
21
-
#### Support to turn on, turn off
22
-
- request
23
-
- response
24
-
- duration
25
-
- http response status code
26
-
- response content length
27
-
#### Support to mask or encrypt fields
28
-
- support to mask or encrypt fields, such as mobileNumber, creditCardNumber
0 commit comments