Skip to content

Commit

Permalink
update gorm log adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
vuduongtp committed Mar 13, 2023
1 parent b083090 commit bab5bd2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
48 changes: 38 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,54 @@ import "github.com/vuduongtp/go-logadapter"
## Basic Example
View full example [here](https://github.com/vuduongtp/go-logadapter/blob/main/test/test.go)
### Create new simple logger
**Log with JSON format**
```go
logger := logadapter.NewLogger()
logger.Debug("test")
```
```
{"level":"info","msg":"Logger instance has been successfully initialized","time":"2023-03-05T20:47:28.369102+07:00"}
{"level":"debug","msg":"test","time":"2023-03-05T20:47:28.369163+07:00"}
{"level":"info","msg":"Logger instance has been successfully initialized","time":"2023-03-05 20:47:28.369102"}
{"level":"debug","msg":"test","time":"2023-03-05 20:47:28.369163"}
```
### Create new logger with config
**Log with pertty JSON format**
```go
config := &logadapter.Config{
LogLevel: logadapter.DebugLevel,
LogFormat: logadapter.JSONFormat,
LogFormat: logadapter.PrettyJSONFormat,
TimestampFormat: time.RFC3339Nano,
IsUseLogFile: true,
IsUseLogFile: false,
}
logger := logadapter.NewLoggerWithConfig(config)
logger.Debug("test")
```
```
{
"level": "info",
"msg": "Logger instance has been successfully initialized",
"time": "2023-03-13T15:10:57.02729+07:00"
}
{
"level": "debug",
"msg": "test",
"time": "2023-03-13T15:10:57.027391+07:00"
}
```
**Log with text format**
```go
config := &logadapter.Config{
LogLevel: logadapter.DebugLevel,
LogFormat: logadapter.TextFormat,
TimestampFormat: time.RFC3339Nano,
IsUseLogFile: false,
}
logger := logadapter.NewLoggerWithConfig(config)
logger.Debug("test")
```
```
time="2023-03-13T15:12:06.700689+07:00" level=info msg="Logger instance has been successfully initialized"
time="2023-03-13T15:12:06.700763+07:00" level=debug msg=test
```
### Set logadapter to gorm logger
```go
config := &logadapter.Config{
Expand All @@ -65,12 +94,11 @@ config := &logadapter.Config{
logger := logadapter.NewLoggerWithConfig(config)

// * set log adapter for gorm logging
gormConfig := new(gorm.Config)
gormConfig.PrepareStmt = true
gormLogAdapter := logadapter.NewGormLogAdapter(logger)
gormLogAdapter.SlowThreshold = time.Second
gormLogAdapter.SourceField = logadapter.DefaultGormSourceField
gormConfig.Logger = gormLogAdapter
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
db.Logger := logadapter.NewGormLogAdapter(logger)
```
### Set logadapter to Echo
```go
Expand Down
2 changes: 2 additions & 0 deletions gorm_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func NewGormLogAdapter(log *Logger) *GormLogAdapter {
SkipErrRecordNotFound: true,
Debug: true,
Logger: log,
SlowThreshold: time.Second,
SourceField: DefaultGormSourceField,
}
}

Expand Down

0 comments on commit bab5bd2

Please sign in to comment.