-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from ybkuroki/logrotation
Enable log rotation
- Loading branch information
Showing
12 changed files
with
309 additions
and
178 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
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
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,118 @@ | ||
package logger | ||
|
||
import ( | ||
"database/sql/driver" | ||
"fmt" | ||
"reflect" | ||
"regexp" | ||
"time" | ||
"unicode" | ||
) | ||
|
||
// ============================================================== | ||
// Customize SQL Logger for gorm library | ||
// ref: https://github.com/wantedly/gorm-zap | ||
// ref: https://github.com/jinzhu/gorm/blob/master/logger.go | ||
// =============================================================== | ||
|
||
// Print passes arguments to Println | ||
func (l *Logger) Print(values ...interface{}) { | ||
l.Println(values) | ||
} | ||
|
||
// Println format & print log | ||
func (l *Logger) Println(values []interface{}) { | ||
sql := createLog(values) | ||
if sql != "" { | ||
l.zap.Debugf(sql) | ||
} | ||
} | ||
|
||
// createLog returns log for output | ||
func createLog(values []interface{}) string { | ||
ret := "" | ||
|
||
if len(values) > 1 { | ||
var level = values[0] | ||
|
||
if level == "sql" { | ||
ret = "[gorm] : " + createSQL(values[3].(string), getFormattedValues(values)) | ||
} | ||
} | ||
|
||
return ret | ||
} | ||
|
||
func isPrintable(s string) bool { | ||
for _, r := range s { | ||
if !unicode.IsPrint(r) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
// getFormattedValues returns values of a SQL statement. | ||
func getFormattedValues(values []interface{}) []string { | ||
var formattedValues []string | ||
for _, value := range values[4].([]interface{}) { | ||
indirectValue := reflect.Indirect(reflect.ValueOf(value)) | ||
if indirectValue.IsValid() { | ||
value = indirectValue.Interface() | ||
if t, ok := value.(time.Time); ok { | ||
if t.IsZero() { | ||
formattedValues = append(formattedValues, fmt.Sprintf("'%v'", "0000-00-00 00:00:00")) | ||
} else { | ||
formattedValues = append(formattedValues, fmt.Sprintf("'%v'", t.Format("2006-01-02 15:04:05"))) | ||
} | ||
} else if b, ok := value.([]byte); ok { | ||
if str := string(b); isPrintable(str) { | ||
formattedValues = append(formattedValues, fmt.Sprintf("'%v'", str)) | ||
} else { | ||
formattedValues = append(formattedValues, "'<binary>'") | ||
} | ||
} else if r, ok := value.(driver.Valuer); ok { | ||
if value, err := r.Value(); err == nil && value != nil { | ||
formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value)) | ||
} else { | ||
formattedValues = append(formattedValues, "NULL") | ||
} | ||
} else { | ||
switch value.(type) { | ||
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, bool: | ||
formattedValues = append(formattedValues, fmt.Sprintf("%v", value)) | ||
default: | ||
formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value)) | ||
} | ||
} | ||
} else { | ||
formattedValues = append(formattedValues, "NULL") | ||
} | ||
} | ||
return formattedValues | ||
} | ||
|
||
// createSQL returns complete SQL with values bound to a SQL statement. | ||
func createSQL(sql string, values []string) string { | ||
var ( | ||
sqlRegexp = regexp.MustCompile(`\?`) | ||
numericPlaceHolderRegexp = regexp.MustCompile(`\$\d+`) | ||
result = "" | ||
) | ||
// differentiate between $n placeholders or else treat like ? | ||
if numericPlaceHolderRegexp.MatchString(sql) { | ||
for index, value := range values { | ||
placeholder := fmt.Sprintf(`\$%d([^\d]|$)`, index+1) | ||
result = regexp.MustCompile(placeholder).ReplaceAllString(sql, value+"$1") | ||
} | ||
} else { | ||
formattedValuesLength := len(values) | ||
for index, value := range sqlRegexp.Split(sql, -1) { | ||
result += value | ||
if index < formattedValuesLength { | ||
result += values[index] | ||
} | ||
} | ||
} | ||
return result | ||
} |
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
Oops, something went wrong.