Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/erajayatech/go-eraspacelog
Log scalyr, log with trace and log print format
https://github.com/erajayatech/go-eraspacelog
gin log loggers scalyr
Last synced: about 1 month ago
JSON representation
Log scalyr, log with trace and log print format
- Host: GitHub
- URL: https://github.com/erajayatech/go-eraspacelog
- Owner: erajayatech
- License: mit
- Created: 2022-03-16T11:30:07.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2024-02-24T10:11:08.000Z (10 months ago)
- Last Synced: 2024-06-21T13:14:08.871Z (6 months ago)
- Topics: gin, log, loggers, scalyr
- Language: Go
- Homepage:
- Size: 83 KB
- Stars: 0
- Watchers: 6
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-eraspacelog
## Getting Started
eraspacelog is a structured logger for Go (golang), completely API compatible with the standard library logger.
## Dependency
* [Gin Web Framework](https://github.com/gin-gonic/gin)
* [Logrus](https://github.com/sirupsen/logrus)
* [Open Telemetry](https://pkg.go.dev/go.opentelemetry.io/otel)
* [GoDotEnv by joho](https://github.com/joho/godotenv)### Installation
Go Version 1.16+
```shell
go get github.com/erajayatech/go-eraspacelog
```
## Setup Environment
- Set the following environment variables:* `MODE=`
* Example : `prod`## How To Use
- First you need to import go-eraspacelog package for using eraspacelog, one simplest example likes the follow```go
package mainimport (
"github.com/erajayatech/go-eraspacelog"
)func main() {
...
eraspacelog.SetupLogger(helper.GetEnv("MODE"))// your code goes here
}
```
`SetupLogger()` need paramater `mode` for determine formater to print into your terminal.
#### Example local mode
with local mode you'll see nicely color-coded
![Colored](https://i.ibb.co/HCTj2tz/log.png)#### Example development mode
with development mode you'll see json
![raw](https://i.ibb.co/0J1FJCg/log-raw.png)## Set auth-header and request-header
- Before implementation logger to every single function on your application, you must set the auth-header and request-header into a middleware.#### Example set auth-header
to set auth-header, just call `SetAuthHeaderInfoToContext()````go
package middlewareimport (
"github.com/erajayatech/go-eraspacelog"
)func (middleware *Middleware) HeaderValidatorMiddleware() gin.HandlerFunc {
return func(context *gin.Context) {
source := context.Request.Header.Get("Source")
eraspacelog.SetAuthHeaderInfoToContext(context, eraspacelog.AuthHeaderInfo{
"source": source,
})
}
}// your code goes here
```#### Example set request-header
to set request-header, just call `SetAuthHeaderInfoToContext()````go
package middlewareimport (
"github.com/erajayatech/go-eraspacelog"
"github.com/gin-gonic/gin"
)func (middleware *Middleware) TraceMiddleware() gin.HandlerFunc {
return func(context *gin.Context) {
var traceID = helper.GenerateUUID()context.Header("X-Trace-Id", traceID)
context.Set("X-Trace-Id", traceID)
context.Set("traceID", traceID)eraspacelog.SetRequestHeaderInfoToContext(context, eraspacelog.RequestHeaderInfo{
"request_id": traceID,
"path": fmt.Sprintf("%s %s", context.Request.Method, context.Request.URL.Path),
})context.Next()
}
}// your code goes here
```