forked from Tinsane/tracelog
-
Notifications
You must be signed in to change notification settings - Fork 9
Add postgres format logs #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
deffer1337
wants to merge
6
commits into
wal-g:master
Choose a base branch
from
deffer1337:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,36 @@ | ||
| package tracelog | ||
|
|
||
| import ( | ||
| "encoding/csv" | ||
| "fmt" | ||
| "io" | ||
| "sync" | ||
| ) | ||
|
|
||
| type csvWriter struct { | ||
| lock sync.Mutex | ||
| out io.Writer | ||
| fields []string | ||
| } | ||
|
|
||
| func (csvWriter *csvWriter) Log(fields Fields) { | ||
| vals := getValuesFromMap(fields, csvWriter.fields) | ||
| stringVals := make([]string, len(vals)) | ||
| for i := range vals { | ||
| stringVals[i] = fmt.Sprint(vals[i]) | ||
| } | ||
| csvWriter.lock.Lock() | ||
| defer csvWriter.lock.Unlock() | ||
| w := csv.NewWriter(csvWriter.out) | ||
| err := w.Write(stringVals) | ||
| if err == nil { | ||
| w.Flush() | ||
| } | ||
| } | ||
|
|
||
| func NewCsvWriter(out io.Writer, fields []string) LoggerWriter { | ||
| return &csvWriter{ | ||
| out: out, | ||
| fields: fields, | ||
| } | ||
| } |
This file contains hidden or 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,28 @@ | ||
| package tracelog | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "sync" | ||
| ) | ||
|
|
||
| type jsonWriter struct { | ||
| lock sync.Mutex | ||
| out io.Writer | ||
| } | ||
|
|
||
| func (jsonWriter *jsonWriter) Log(fields Fields) { | ||
| jsonWriter.lock.Lock() | ||
| defer jsonWriter.lock.Unlock() | ||
| data, err := json.MarshalIndent(fields, "", " ") | ||
| if err == nil { | ||
| _, _ = fmt.Fprint(jsonWriter.out, string(data)) | ||
| } | ||
| } | ||
|
|
||
| func NewJsonWriter(out io.Writer) LoggerWriter { | ||
| return &jsonWriter{ | ||
| out: out, | ||
| } | ||
| } |
This file contains hidden or 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,87 @@ | ||
| package tracelog | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| ) | ||
|
|
||
| type LoggerType string | ||
|
|
||
| const ( | ||
| InfoLoggerType LoggerType = "INFO" | ||
| WarningLoggerType LoggerType = "WARNING" | ||
| ErrorLoggerType LoggerType = "ERROR" | ||
| DebugLoggerType LoggerType = "DEBUG" | ||
| ) | ||
|
|
||
| type Fields map[string]interface{} | ||
| type FieldValues func() Fields | ||
|
|
||
| type Logger struct { | ||
| loggerWriters []LoggerWriter | ||
| fieldValues FieldValues | ||
| } | ||
|
|
||
| func NewLogger(fieldValues FieldValues, loggerWriters ...LoggerWriter) *Logger { | ||
| return &Logger{ | ||
| loggerWriters: loggerWriters, | ||
| fieldValues: fieldValues, | ||
| } | ||
| } | ||
|
|
||
| func (logger *Logger) Log(v ...interface{}) { | ||
| fields := logger.fieldValues() | ||
| fields["message"] = fmt.Sprint(v...) | ||
| for _, loggerWriter := range logger.loggerWriters { | ||
| loggerWriter.Log(fields) | ||
| } | ||
| } | ||
|
|
||
| func (logger *Logger) Logf(format string, v ...interface{}) { | ||
| logger.Log(fmt.Sprintf(format, v...)) | ||
| } | ||
|
|
||
| func (logger *Logger) Fatalln(v ...interface{}) { | ||
| logger.Log(fmt.Sprintln(v...)) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| func (logger *Logger) Fatalf(format string, v ...interface{}) { | ||
| logger.Logf(format, v...) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| func (logger *Logger) Fatal(v ...interface{}) { | ||
| logger.Log(v...) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| func (logger *Logger) Panicln(v ...interface{}) { | ||
| s := fmt.Sprintln(v...) | ||
| logger.Log(s) | ||
| panic(s) | ||
| } | ||
|
|
||
| func (logger *Logger) Panicf(format string, v ...interface{}) { | ||
| s := fmt.Sprintf(format, v...) | ||
| logger.Log(s) | ||
| panic(s) | ||
| } | ||
|
|
||
| func (logger *Logger) Panic(v ...interface{}) { | ||
| s := fmt.Sprint(v...) | ||
| logger.Log(s) | ||
| panic(s) | ||
| } | ||
|
|
||
| func (logger *Logger) Println(v ...interface{}) { | ||
| logger.Log(fmt.Sprintln(v...)) | ||
| } | ||
|
|
||
| func (logger *Logger) Printf(format string, v ...interface{}) { | ||
| logger.Logf(format, v...) | ||
| } | ||
|
|
||
| func (logger *Logger) Print(v ...interface{}) { | ||
| logger.Log(v...) | ||
| } | ||
This file contains hidden or 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,139 @@ | ||
| package tracelog | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "os" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| var TestBasicFormat = "%s [%d] %s: %s" | ||
| var TestBasicFields = []string{"time", "pid", "level", "message"} | ||
|
|
||
| func GetFieldValuesForTest(loggerType LoggerType) func() Fields { | ||
| return func() Fields { | ||
| now := time.Now().UTC() | ||
| fields := Fields{ | ||
| "time": now.Format("2006-01-02 03:04:05 UTC"), | ||
| "pid": os.Getpid(), | ||
| "level": loggerType, | ||
| } | ||
|
|
||
| return fields | ||
| } | ||
| } | ||
|
|
||
| func TestLogger_PrintWithTextWriter(t *testing.T) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Many tests with only little details different, extract the separate function with common code and provide the input / expected output. |
||
| message := "test" | ||
| logLevel := InfoLoggerType | ||
| now := time.Now().UTC().Format("2006-01-02 03:04:05 UTC") | ||
| expectedOutput := fmt.Sprintf("%s [%d] %s: %s", now, os.Getpid(), logLevel, message) | ||
| buffer := bytes.Buffer{} | ||
| infoPostgresLogger := NewPostgresLogger(GetFieldValuesForTest(logLevel), NewTextWriter(&buffer, TestBasicFormat, TestBasicFields)) | ||
| infoPostgresLogger.Print(message) | ||
| if expectedOutput != buffer.String() { | ||
| t.Errorf("\nOutput should be: %s, got: %s", expectedOutput, buffer.String()) | ||
| } | ||
| } | ||
|
|
||
| func TestLogger_PrintlnWithTextWriter(t *testing.T) { | ||
| message := "test" | ||
| logLevel := InfoLoggerType | ||
| now := time.Now().UTC().Format("2006-01-02 03:04:05 UTC") | ||
| expectedOutput := fmt.Sprintf("%s [%d] %s: %s%s", now, os.Getpid(), logLevel, message, "\n") | ||
| buffer := bytes.Buffer{} | ||
| infoPostgresLogger := NewPostgresLogger(GetFieldValuesForTest(logLevel), NewTextWriter(&buffer, TestBasicFormat, TestBasicFields)) | ||
| infoPostgresLogger.Println(message) | ||
| if expectedOutput != buffer.String() { | ||
| t.Errorf("\nOutput should be: %v, got: %v", expectedOutput, buffer.String()) | ||
| } | ||
| } | ||
|
|
||
| func TestLogger_PrintfWithTextWriter(t *testing.T) { | ||
| logLevel := InfoLoggerType | ||
| now := time.Now().UTC().Format("2006-01-02 03:04:05 UTC") | ||
| expectedOutput := fmt.Sprintf("%s [%d] %s: %s %s", now, os.Getpid(), logLevel, "kek", "test") | ||
| buffer := bytes.Buffer{} | ||
| infoPostgresLogger := NewPostgresLogger(GetFieldValuesForTest(logLevel), NewTextWriter(&buffer, TestBasicFormat, TestBasicFields)) | ||
| infoPostgresLogger.Printf("%s %s", "kek", "test") | ||
| if expectedOutput != buffer.String() { | ||
| t.Errorf("\nOutput should be: %v, got: %v", expectedOutput, buffer.String()) | ||
| } | ||
| } | ||
|
|
||
| func TestLogger_PrintWithJsonWriter(t *testing.T) { | ||
| message := "test" | ||
| logLevel := InfoLoggerType | ||
| now := time.Now().UTC().Format("2006-01-02 03:04:05 UTC") | ||
| expectedOutput := fmt.Sprintf("{\n \"level\": \"%s\",\n \"message\": \"%s\",\n \"pid\": %d,\n \"time\": \"%s\"\n}", logLevel, message, os.Getpid(), now) | ||
| buffer := bytes.Buffer{} | ||
| infoPostgresLogger := NewPostgresLogger(GetFieldValuesForTest(logLevel), NewJsonWriter(&buffer)) | ||
| infoPostgresLogger.Print(message) | ||
| if expectedOutput != buffer.String() { | ||
| t.Errorf("\nOutput should be:\n %s, \ngot: \n%s", expectedOutput, buffer.String()) | ||
| } | ||
| } | ||
|
|
||
| func TestLogger_PrintlnWithJsonWriter(t *testing.T) { | ||
| message := "test" | ||
| logLevel := InfoLoggerType | ||
| now := time.Now().UTC().Format("2006-01-02 03:04:05 UTC") | ||
| expectedOutput := fmt.Sprintf("{\n \"level\": \"%s\",\n \"message\": \"%s\\n\",\n \"pid\": %d,\n \"time\": \"%s\"\n}", logLevel, message, os.Getpid(), now) | ||
| buffer := bytes.Buffer{} | ||
| infoPostgresLogger := NewPostgresLogger(GetFieldValuesForTest(logLevel), NewJsonWriter(&buffer)) | ||
| infoPostgresLogger.Println(message) | ||
| if expectedOutput != buffer.String() { | ||
| t.Errorf("\nOutput should be:\n %s, \ngot: \n%s", expectedOutput, buffer.String()) | ||
| } | ||
| } | ||
|
|
||
| func TestLogger_PrintfWithJsonWriter(t *testing.T) { | ||
| logLevel := InfoLoggerType | ||
| now := time.Now().UTC().Format("2006-01-02 03:04:05 UTC") | ||
| expectedOutput := fmt.Sprintf("{\n \"level\": \"%s\",\n \"message\": \"%s\",\n \"pid\": %d,\n \"time\": \"%s\"\n}", logLevel, "kek test", os.Getpid(), now) | ||
| buffer := bytes.Buffer{} | ||
| infoPostgresLogger := NewPostgresLogger(GetFieldValuesForTest(logLevel), NewJsonWriter(&buffer)) | ||
| infoPostgresLogger.Printf("%s %s", "kek", "test") | ||
| if expectedOutput != buffer.String() { | ||
| t.Errorf("\nOutput should be:\n %s, \ngot: \n%s", expectedOutput, buffer.String()) | ||
| } | ||
| } | ||
|
|
||
| func TestLogger_PrintWithCsvWriter(t *testing.T) { | ||
| message := "test" | ||
| logLevel := InfoLoggerType | ||
| now := time.Now().UTC().Format("2006-01-02 03:04:05 UTC") | ||
| expectedOutput := fmt.Sprintf("%s,%d,%s,%s\n", now, os.Getpid(), logLevel, message) | ||
| buffer := bytes.Buffer{} | ||
| infoPostgresLogger := NewPostgresLogger(GetFieldValuesForTest(logLevel), NewCsvWriter(&buffer, TestBasicFields)) | ||
| infoPostgresLogger.Print(message) | ||
| if expectedOutput != buffer.String() { | ||
| t.Errorf("\nOutput should be:\n %s \ngot:\n %s", expectedOutput, buffer.String()) | ||
| } | ||
| } | ||
|
|
||
| func TestLogger_PrintlnWithCsvWriter(t *testing.T) { | ||
| message := "test" | ||
| logLevel := InfoLoggerType | ||
| now := time.Now().UTC().Format("2006-01-02 03:04:05 UTC") | ||
| expectedOutput := fmt.Sprintf("%s,%d,%s,\"%s\n\"\n", now, os.Getpid(), logLevel, message) | ||
| buffer := bytes.Buffer{} | ||
| infoPostgresLogger := NewPostgresLogger(GetFieldValuesForTest(logLevel), NewCsvWriter(&buffer, TestBasicFields)) | ||
| infoPostgresLogger.Println(message) | ||
| if expectedOutput != buffer.String() { | ||
| t.Errorf("\nOutput should be:\n %s \ngot:\n %s", expectedOutput, buffer.String()) | ||
| } | ||
| } | ||
|
|
||
| func TestLogger_PrintfWithCsvWriter(t *testing.T) { | ||
| logLevel := InfoLoggerType | ||
| now := time.Now().UTC().Format("2006-01-02 03:04:05 UTC") | ||
| expectedOutput := fmt.Sprintf("%s,%d,%s,%s\n", now, os.Getpid(), logLevel, "kek test") | ||
| buffer := bytes.Buffer{} | ||
| infoPostgresLogger := NewPostgresLogger(GetFieldValuesForTest(logLevel), NewCsvWriter(&buffer, TestBasicFields)) | ||
| infoPostgresLogger.Printf("%s %s", "kek", "test") | ||
| if expectedOutput != buffer.String() { | ||
| t.Errorf("\nOutput should be:\n %s \ngot:\n %s", expectedOutput, buffer.String()) | ||
| } | ||
| } | ||
This file contains hidden or 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,5 @@ | ||
| package tracelog | ||
|
|
||
| type LoggerWriter interface { | ||
| Log(Fields) | ||
| } |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need many writers at the same time, so I suggest to change it to a single writer for simplicity.