-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Send Gorm logs directly to datadog (#5)
* add gorm logger * fix query time * fix rows_affected * remove errors, fix messages * change custom fields type when sending logs * add offline logs * return error and change function name * fix send * add Offline log prefix * improve offline logs * change execution time field name * send logs into the channels wrapped in go routines * improve gorm logger * fix check v * fix err overwrite * fix * send first the log * don't send fatal log in a goroutine, wait instead
- Loading branch information
Showing
4 changed files
with
258 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package logpet | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
func CreateGormLogger(logger *StandardLogger) *GormLogger { | ||
return &GormLogger{Logger: logger} | ||
} | ||
|
||
type GormLogger struct { | ||
Logger *StandardLogger | ||
} | ||
|
||
// Print handles log events from Gorm for the custom logger. | ||
func (gLogger *GormLogger) Print(v ...interface{}) { | ||
fields := map[string]interface{}{ | ||
"section": "database", | ||
} | ||
|
||
if nil == v { | ||
gLogger.Logger.SendDebugLog("v database arguments are empty", fields) | ||
return | ||
} | ||
|
||
switch v[0] { | ||
case "sql": | ||
|
||
if len(v) != 6 { | ||
gLogger.Logger.SendErrfLog("wrong db log format, %v", fields, v) | ||
return | ||
} | ||
|
||
queryfields := map[string]string{ | ||
"execution_time": v[2].(time.Duration).String(), | ||
"arguments": fmt.Sprintf("%v", v[4]), | ||
"rows_affected": strconv.FormatInt(v[5].(int64), 10), | ||
"function_line": v[1].(string), | ||
} | ||
|
||
fields["query"] = queryfields | ||
|
||
gLogger.Logger.SendDebugfLog(v[3].(string), fields) | ||
case "log": | ||
if len(v) != 3 { | ||
gLogger.Logger.SendErrfLog("wrong db log format, %v", fields, v) | ||
return | ||
} | ||
|
||
queryfields := map[string]string{ | ||
"function_line": v[1].(string), | ||
} | ||
|
||
fields["query"] = queryfields | ||
gLogger.Logger.SendWarnfLog("%v", fields, v[2]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package logpet | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
func (l *StandardLogger) EnableOfflineLogs(enable bool) { | ||
l.saveOfflineLogs = enable | ||
} | ||
|
||
func (l *StandardLogger) saveLogToFile(toSave []byte, filename string) error { | ||
|
||
_, err := os.Stat(l.offlineLogsPath) | ||
if os.IsNotExist(err) { | ||
err = os.Mkdir(l.offlineLogsPath, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
filename = strings.ReplaceAll(filename, ":", "-") | ||
|
||
filename = filepath.Join(l.offlineLogsPath, filename) | ||
|
||
_, err = os.Stat(filename) | ||
if os.IsNotExist(err) { | ||
_, err := os.Create(filename) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
err = ioutil.WriteFile(filename, toSave, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (l *StandardLogger) SendOfflineLogs() error { | ||
|
||
dir, err := ioutil.ReadDir(l.offlineLogsPath) | ||
if err != nil { | ||
return errors.New(fmt.Sprintf("unable to open directory %s, %v", l.offlineLogsPath, err)) | ||
} | ||
|
||
for _, logfile := range dir { | ||
if strings.HasPrefix(logfile.Name(), "log-") { | ||
|
||
filename := filepath.Join(l.offlineLogsPath, logfile.Name()) | ||
|
||
// read and send | ||
file, err := os.Open(filename) | ||
if err != nil { | ||
l.SendErrfLog("unable to open file %s", nil, logfile.Name()) | ||
continue | ||
} | ||
|
||
var logs ClientLog | ||
|
||
err = json.NewDecoder(file).Decode(&logs) | ||
if err != nil { | ||
l.SendErrfLog("error decoding json log %s, due to %v", nil, file.Name(), err) | ||
|
||
// Close file and handle err | ||
err = file.Close() | ||
if err != nil { | ||
l.SendErrfLog("error closing json log %s, due to %v", nil, file.Name(), err) | ||
} | ||
continue | ||
} | ||
|
||
switch logs.Level { | ||
case "info": | ||
l.SendInfoLog(logs.Message, nil) | ||
case "warning": | ||
l.SendWarnLog(logs.Message, nil) | ||
case "error": | ||
l.SendErrLog(logs.Message, nil) | ||
case "debug": | ||
l.SendDebugLog(logs.Message, nil) | ||
case "fatal": | ||
l.SendErrfLog("EX FATAL | %s", nil, logs.Message) | ||
} | ||
|
||
// Close file and handle err | ||
err = file.Close() | ||
if err != nil { | ||
l.SendErrfLog("unable to close file %s due to %v", nil, file.Name(), err) | ||
} | ||
|
||
err = os.Remove(filename) | ||
if err != nil { | ||
l.SendErrfLog("unable to remove file %s due to %v", nil, file.Name(), err) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters