Skip to content
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

Add postgres format logs #5

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions csv_writer.go
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,
}
}
56 changes: 0 additions & 56 deletions error_logger.go

This file was deleted.

28 changes: 28 additions & 0 deletions json_writer.go
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,
}
}
127 changes: 127 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
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 {
loggerWriter LoggerWriter
fieldValues FieldValues
}

func NewLogger(fieldValues FieldValues, loggerWriter LoggerWriter) *Logger {
return &Logger{
loggerWriter: loggerWriter,
fieldValues: fieldValues,
}
}

func (logger *Logger) Log(v ...interface{}) {
fields := logger.fieldValues()
fields["message"] = fmt.Sprint(v...)
logger.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...)
}

func (logger *Logger) PanicError(err error) {
logger.Panic(err)
}

func (logger *Logger) PanicfOnError(format string, err error) {
if err != nil {
logger.Panicf(format, err)
}
}

func (logger *Logger) PanicOnError(err error) {
if err != nil {
logger.PanicError(err)
}
}

func (logger *Logger) FatalError(err error) {
logger.Fatal(err)
}

func (logger *Logger) FatalfOnError(format string, err error) {
if err != nil {
logger.Fatalf(format, err)
}
}

func (logger *Logger) FatalOnError(err error) {
if err != nil {
logger.FatalError(err)
}
}

func (logger *Logger) PrintError(err error) {
logger.Println(err)
}

func (logger *Logger) PrintOnError(err error) {
if err != nil {
logger.PrintError(err)
}
}
Loading