Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/monolog-connector/monolog-go
Lightweight SDK for Logging and Monitoring
https://github.com/monolog-connector/monolog-go
gin gin-gonic golang rpm rpm-packages
Last synced: 8 days ago
JSON representation
Lightweight SDK for Logging and Monitoring
- Host: GitHub
- URL: https://github.com/monolog-connector/monolog-go
- Owner: MonoLog-Connector
- License: mit
- Created: 2024-10-02T16:53:32.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2024-10-05T20:41:01.000Z (3 months ago)
- Last Synced: 2024-10-29T13:56:06.788Z (about 2 months ago)
- Topics: gin, gin-gonic, golang, rpm, rpm-packages
- Language: Go
- Homepage:
- Size: 17.6 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Monolog-Go SDK
[![Go Reference](https://pkg.go.dev/badge/github.com/MonoLog-Connector/monolog-go.svg)](https://pkg.go.dev/github.com/MonoLog-Connector/monolog-go/client)
Monolog-Go is a lightweight SDK for logging and monitoring Go applications. It is designed for easy integration with the `gin-gonic` framework. The SDK provides middleware that tracks and logs request details, including CPU and memory usage, latency, request methods, URLs, and status codes, all written to a specified log file. This makes it ideal for monitoring your application's performance and behavior.
## Installation
To use the SDK, you need to first install it using `go get`.
```bash
go get github.com/MonoLog-Connector/monolog-go/[email protected]
```## Usage Example
Below is an example of how you can integrate the Monolog-Go SDK with a Gin router in your Go application.
### Step 1: Import the package
```
import (
"github.com/MonoLog-Connector/monolog-go/client"
"github.com/gin-gonic/gin"
)
```
### Step 2: Initialize the SDKInitialize the SDK by passing the path to the log file where the request details will be stored.
```
func main() {
router := gin.Default()// Initialize SDK with the path to the log file
monologsdk := client.NewSDK("/path/to/your/log/file.log")
// Use the middleware provided by the SDK
router.Use(monologsdk.GinTrackerMiddleware())
router.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
router.Run(":8080")
}
```### Step 3: Now test it
After starting the server, you can test the setup by making a request to the endpoint:
```
curl http://localhost:8080/ping
```### Step 4: View the logs
Once the request is made, the logs will be written to the specified log file. Below is an example of the generated logs:
```
{
"CPU Delta": 74.19527896995683,
"DateTime": "2024-10-06T01:38:19+05:30",
"Latency": "529.292µs",
"Memory Delta (MB)": 0.390625,
"RequestMethod": "GET",
"RequestURL": "/ping",
"Status": 404,
"level": "info",
"msg": "Request details logged",
"time": "2024-10-06T01:38:19+05:30"
}
{
"level": "info",
"msg": "Shutdown signal received, cleaning up...",
"time": "2024-10-06T01:39:06+05:30"
}
{
"CPU Delta": 1.1155807145198469,
"DateTime": "2024-10-06T01:40:53+05:30",
"Latency": "5.702657166s",
"Memory Delta (MB)": 9.84375,
"RequestMethod": "GET",
"RequestURL": "/ping",
"Status": 200,
"level": "info",
"msg": "Request details logged",
"time": "2024-10-06T01:40:59+05:30"
}```