https://github.com/core-go/log
https://github.com/core-go/log
log logging logrus middleware middleware-log zap
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/core-go/log
- Owner: core-go
- Created: 2020-09-25T11:08:58.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2024-08-28T11:18:59.000Z (10 months ago)
- Last Synced: 2025-03-02T21:07:26.428Z (4 months ago)
- Topics: log, logging, logrus, middleware, middleware-log, zap
- Language: Go
- Homepage:
- Size: 95.7 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# log
- wrapper of [zap](https://pkg.go.dev/go.uber.org/zap)
- wrapper of [logrus](https://github.com/sirupsen/logrus)
- middleware tracing log for [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))
- useful for Financial Products (to comply with PCI-DSS standards) and Healthcare (to comply with HIPAA regulations)- You can refer to [middleware-log-tracing](https://www.linkedin.com/pulse/middleware-log-tracing-go-duc-nguyen-uh0pc) at my [Linked In](https://vn.linkedin.com/in/duc-nguyen-437240239?trk=article-ssr-frontend-pulse_publisher-author-card) for more details.
### A typical micro service
- When you zoom one micro service, the flow is as below, and you can see "log (zap, logrus)" and "middleware" in the full picture:
## Content for logging
### Request
#### Features
- Log Request Method and URL: Log the HTTP method (GET, POST, etc.) and the requested URL.
- Log Request Headers: Option to log request headers for debugging purposes.
- Log Request Body: Option to log the request body (with configurable size limits to avoid logging large payloads).
#### Benefits
- Debugging: Helps in tracing and debugging issues by providing complete information about incoming requests.
- Monitoring: Provides visibility into the types of requests being received.### Response
#### Features
- Log Response Status Code: Log the HTTP status code of the response.
- Log Response Headers: Option to log response headers.
- Log Response Body: Option to log the response body (with configurable size limits to avoid logging large payloads).
#### Benefits
- Debugging: Assists in diagnosing issues by providing complete information about the responses sent by the server.
- Auditing: Helps in auditing and reviewing server responses for compliance and monitoring purposes.### Response Time
#### Features
- Log Response Time: Calculate and log the time taken to process each request.
#### Benefits
- Performance Monitoring: Helps in identifying slow requests and performance bottlenecks.
- Optimization: Provides data to optimize and improve server response times.### Response Size
#### Features
- Log Response Size: Log the size of the response payload in bytes.
#### Benefits
- Bandwidth Monitoring: Helps in monitoring and managing bandwidth usage.
- Optimization: Provides insights into the response sizes to optimize payloads and improve performance.## Features
### Middleware Integration
#### Features
- Middleware Function: 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)).
- Sample for [Echo](https://github.com/labstack/echo) is at [go-echo-sql-sample](https://github.com/go-tutorials/go-echo-sql-sample)
- Sample for [Gin](https://github.com/gin-gonic/gin) is at [go-gin-sql-sample](https://github.com/go-tutorials/go-gin-sql-sample)
- Sample for [Gorilla mux](https://github.com/gorilla/mux) is at [go-sql-sample](https://github.com/go-tutorials/go-sql-sample)
- Context Handling: Pass context to handle request-specific data throughout the middleware chain.
#### Benefits
- Ease of Use: Simplifies the integration of logging into existing web applications.
- Consistency: Ensures consistent logging across different parts of the application.### Logging Libraries Integration
- Do not depend on any logging libraries.
- Already supported to integrate with [zap](https://pkg.go.dev/go.uber.org/zap), [logrus](https://github.com/sirupsen/logrus)
- Can be integrated with any logging library.### Enable/Disable Logging
#### Features
- Enable/Disable Logging: Allow users to turn on or off logging for requests, responses, headers, and bodies independently.
- Logging Levels: Support different logging levels (e.g., INFO, DEBUG, ERROR) to control the verbosity of logs.
#### Benefits
- Flexibility: Provides users with the flexibility to configure logging based on their needs and environment.
- Efficiency: Reduces overhead by allowing selective logging, especially in production environments.### Asynchronous Logging
#### Features
- Non-Blocking Logs: Implement asynchronous logging to ensure that logging does not block request processing.
- Log Buffering: Use buffering to improve logging performance and reduce latency.
#### Benefits:
- Performance: Improves the overall performance of the application by reducing logging overhead.
- Scalability: Allows the application to handle high-throughput logging without performance degradation.### Sensitive Data Encryption
#### Features
- Mask/Encrypt sensitive data in the request and response bodies.
- Sample for [Echo](https://github.com/labstack/echo) is at [go-echo-sql-sample](https://github.com/go-tutorials/go-echo-sql-sample)
- Sample for [Gin](https://github.com/gin-gonic/gin) is at [go-gin-sql-sample](https://github.com/go-tutorials/go-gin-sql-sample)
- Sample for [Gorilla mux](https://github.com/gorilla/mux) is at [go-sql-sample](https://github.com/go-tutorials/go-sql-sample)
- Sensitive Data Identification: identify and encrypt specific fields in JSON payloads.#### Benefits:
- Security: Protects sensitive information from being exposed in logs.
- Compliance: Helps meet security and compliance requirements by safeguarding sensitive data.
- Ease of Use: Simplifies the integration of encryption/masking into any existing applications.
- Consistency: Ensures that sensitive data is consistently encrypted or masked across all logged requests and responses## Use Cases of sensitive data masking/encrypting
### Financial Transactions
- Benefit: 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.
### Healthcare
- Benefit: Encrypting patient data such as medical records and health information in logs ensures compliance with HIPAA regulations and protects patient privacy.
### E-commerce
- Benefit: Protecting customer information, such as addresses and payment details, enhances customer trust and protects the e-commerce platform from potential data breaches.## Benefits to Developers
#### Enhanced Debugging
- Provides detailed logs for requests and responses, aiding in troubleshooting and debugging issues.
#### Performance Monitoring
- Logs response times and sizes, allowing developers to monitor and optimize application performance.
#### Flexibility
- Configurable logging settings enable developers to tailor the logging behavior to their needs.
#### Ease of Integration
- Middleware function can be easily integrated into existing web frameworks, simplifying the setup process.
#### Improved Maintainability
- Centralized logging logic ensures consistency and makes the codebase easier to maintain.
#### Security:
- Encrypt or mask sensitive data in logs, reducing the risk of data exposure and meeting compliance requirements.## Conclusion
By implementing these features, you provide a comprehensive logging solution that enhances the visibility, performance, and maintainability of any GO applications.## Installation
Please make sure to initialize a Go module before installing core-go/log:```shell
go get -u github.com/core-go/log
```Import:
```go
import "github.com/core-go/log"
```## Summary of middleware log tracing
#### log tracing at middleware
middleware log for
- http
- echo
- gin
##### Support to turn on, turn off
- request
- response
- duration
- http response status code
- response content length
##### Support to mask or encrypt fields
- support to mask or encrypt fields, such as mobileNumber, creditCardNumber
- useful for Financial Products (to comply with PCI-DSS standards) and Healthcare (to comply with HIPAA regulations)
## Appendix
### Microservice Architect
### Cross-cutting concerns
- "log" in the full picture of cross-cutting concerns
