Skip to content

Commit

Permalink
Working on interface for all log methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Nov 17, 2020
1 parent 7241dcd commit 5733004
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 16 deletions.
45 changes: 45 additions & 0 deletions log_entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,41 @@ func (l *LogClient) write(data string) {
l.messages.Enqueue(&buff)
}

// Panic overloads built-in method
func (l *LogClient) Panic(v ...interface{}) {
var buff bytes.Buffer
buff.WriteString(l.token)
buff.WriteByte(' ')
buff.WriteString(fmt.Sprint(v...))
_ = l.sendOne(&buff)
os.Exit(1)
}

// Panicln overloads built-in method
func (l *LogClient) Panicln(v ...interface{}) {
var buff bytes.Buffer
buff.WriteString(l.token)
buff.WriteByte(' ')
buff.WriteString(fmt.Sprintln(v...))
_ = l.sendOne(&buff)
os.Exit(1)
}

// Panicf overloads built-in method
func (l *LogClient) Panicf(format string, v ...interface{}) {
var buff bytes.Buffer
buff.WriteString(l.token)
buff.WriteByte(' ')
buff.WriteString(fmt.Sprintf(format, v...))
_ = l.sendOne(&buff)
os.Exit(1)
}

// Print overloads built-in method
func (l *LogClient) Print(v ...interface{}) {
l.write(fmt.Sprint(v...))
}

// Println overloads built-in method
func (l *LogClient) Println(v ...interface{}) {
l.write(fmt.Sprintln(v...))
Expand All @@ -140,6 +175,16 @@ func (l *LogClient) Printf(format string, v ...interface{}) {
l.write(fmt.Sprintf(format, v...))
}

// Fatal overloads built-in method
func (l *LogClient) Fatal(v ...interface{}) {
var buff bytes.Buffer
buff.WriteString(l.token)
buff.WriteByte(' ')
buff.WriteString(fmt.Sprint(v...))
_ = l.sendOne(&buff)
os.Exit(1)
}

// Fatalln overloads built-in method
func (l *LogClient) Fatalln(v ...interface{}) {
var buff bytes.Buffer
Expand Down
4 changes: 2 additions & 2 deletions log_entries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ func TestNewLogEntriesClient(t *testing.T) {
assert.Equal(t, LogEntriesURL, client.endpoint)
assert.Equal(t, testToken, client.token)

client, err = NewLogEntriesClient("token", LogEntriesURL, "101010")
client, err = NewLogEntriesClient(testToken, LogEntriesURL, "101010")
assert.Error(t, err)
assert.NotNil(t, client)

client, err = NewLogEntriesClient("token", "http://badurl.com", LogEntriesPort)
client, err = NewLogEntriesClient(testToken, "http://badurl.com", LogEntriesPort)
assert.Error(t, err)
assert.NotNil(t, client)

Expand Down
82 changes: 73 additions & 9 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ import (

// Logger interface describes the functionality that a log service must implement
type Logger interface {
Println(v ...interface{})
Printf(format string, v ...interface{})
Fatalln(v ...interface{})
Fatal(...interface{})
Fatalf(format string, v ...interface{})
Fatalln(v ...interface{})
Panic(...interface{})
Panicf(string, ...interface{})
Panicln(...interface{})
Print(...interface{})
Printf(format string, v ...interface{})
Println(v ...interface{})
}

// KeyValue key value for errors
Expand Down Expand Up @@ -119,6 +124,33 @@ func FileTagComponents(level int) []string {
return []string{strings.Join(path[len(path)-2:], "/"), methodPath[len(methodPath)-1], strconv.Itoa(line)}
}

// Panic is equivalent to Print() followed by a call to os.Exit(1)
func Panic(v ...interface{}) {
values := []interface{}{FileTag(2)}
values = append(values, v...)
implementation.Panic(values...)
}

// Panicln is equivalent to Println() followed by a call to os.Exit(1)
func Panicln(v ...interface{}) {
values := []interface{}{FileTag(2)}
values = append(values, v...)
implementation.Panicln(values...)
}

// Panicf is equivalent to Printf() followed by a call to os.Exit(1)
func Panicf(format string, v ...interface{}) {
implementation.Panicf(FileTag(2)+" "+format, v...)
}

// Print calls Output to print to the connected logger.
// Arguments are handled in the manner of fmt.Print.
func Print(v ...interface{}) {
values := []interface{}{FileTag(2)}
values = append(values, v...)
implementation.Print(values...)
}

// Println calls Output to print to the connected logger.
// Arguments are handled in the manner of fmt.Println.
func Println(v ...interface{}) {
Expand All @@ -127,6 +159,12 @@ func Println(v ...interface{}) {
implementation.Println(values...)
}

// Printf calls Output to print to the connected logger.
// Arguments are handled in the manner of fmt.Printf.
func Printf(format string, v ...interface{}) {
implementation.Printf(FileTag(2)+" "+format, v...)
}

// NoFilePrintln calls Output to print to the connected logger.
// Arguments are handled in the manner of fmt.Println.
func NoFilePrintln(v ...interface{}) {
Expand All @@ -135,18 +173,19 @@ func NoFilePrintln(v ...interface{}) {
implementation.Println(values...)
}

// Printf calls Output to print to the connected logger.
// Arguments are handled in the manner of fmt.Printf.
func Printf(format string, v ...interface{}) {
implementation.Printf(FileTag(2)+" "+format, v...)
}

// NoFilePrintf calls Output to print to the connected logger.
// Arguments are handled in the manner of fmt.Printf.
func NoFilePrintf(format string, v ...interface{}) {
implementation.Printf(format, v...)
}

// Fatal is equivalent to Print() followed by a call to os.Exit(1)
func Fatal(v ...interface{}) {
values := []interface{}{FileTag(2)}
values = append(values, v...)
implementation.Fatal(values...)
}

// Fatalln is equivalent to Println() followed by a call to os.Exit(1)
func Fatalln(v ...interface{}) {
values := []interface{}{FileTag(2)}
Expand Down Expand Up @@ -224,6 +263,26 @@ func NoFileData(logLevel LogLevel, message string, args ...KeyValue) {
NoFilePrintln(buf.String())
}

// Panic is normal panic
func (l *logPkg) Panic(v ...interface{}) {
log.Fatal(v...)
}

// Panicln fatal line
func (l *logPkg) Panicln(v ...interface{}) {
log.Fatalln(v...)
}

// Panicf fatal sprint line
func (l *logPkg) Panicf(format string, v ...interface{}) {
log.Fatalf(format, v...)
}

// Print prints
func (l *logPkg) Print(v ...interface{}) {
log.Print(v...)
}

// Println print line
func (l *logPkg) Println(v ...interface{}) {
log.Println(v...)
Expand All @@ -234,6 +293,11 @@ func (l *logPkg) Printf(format string, v ...interface{}) {
log.Printf(format, v...)
}

// Fatal is normal fatal
func (l *logPkg) Fatal(v ...interface{}) {
log.Fatal(v...)
}

// Fatalln fatal line
func (l *logPkg) Fatalln(v ...interface{}) {
log.Fatalln(v...)
Expand Down
109 changes: 104 additions & 5 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ func TestPrintln(t *testing.T) {
assert.Contains(t, captured, "test this method")
}

// TestPrint test the print method
func TestPrint(t *testing.T) {
captured := captureOutput(func() {
Print("test this method")
})

assert.Contains(t, captured, "go-logger/logger_test.go:go-logger.TestPrint")
assert.Contains(t, captured, "test this method")
}

// TestNoFilePrintln test the print line method
func TestNoFilePrintln(t *testing.T) {
captured := captureOutput(func() {
Expand Down Expand Up @@ -324,8 +334,7 @@ func BenchmarkLogPkg_Println(b *testing.B) {
// TestFatalf will test the Fatalf() method
func TestFatalf(t *testing.T) {

token := "token"
client, err := NewLogEntriesClient(token, LogEntriesURL, LogEntriesPort)
client, err := NewLogEntriesClient(testToken, LogEntriesURL, LogEntriesPort)
assert.NoError(t, err)
assert.NotNil(t, client)

Expand All @@ -347,11 +356,32 @@ func TestFatalf(t *testing.T) {
t.Fatalf("process ran with err %v, want exit status 1", err)
}

// TestFatalf will test the Fatalln() method
// TestFatal will test the Fatal() method
func TestFatal(t *testing.T) {

client, err := NewLogEntriesClient(testToken, LogEntriesURL, LogEntriesPort)
assert.NoError(t, err)
assert.NotNil(t, client)

SetImplementation(client)

if os.Getenv("EXIT_FUNCTION") == "1" {
Fatal("test exit")
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestFatal")
cmd.Env = append(os.Environ(), "EXIT_FUNCTION=1")
err = cmd.Run()
if e, ok := err.(*exec.ExitError); ok && !e.Success() {
return
}
t.Fatalf("process ran with err %v, want exit status 1", err)
}

// TestFatalln will test the Fatalln() method
func TestFatalln(t *testing.T) {

token := "token"
client, err := NewLogEntriesClient(token, LogEntriesURL, LogEntriesPort)
client, err := NewLogEntriesClient(testToken, LogEntriesURL, LogEntriesPort)
assert.NoError(t, err)
assert.NotNil(t, client)

Expand All @@ -369,3 +399,72 @@ func TestFatalln(t *testing.T) {
}
t.Fatalf("process ran with err %v, want exit status 1", err)
}

// TestPanic will test the Panic() method
func TestPanic(t *testing.T) {

client, err := NewLogEntriesClient(testToken, LogEntriesURL, LogEntriesPort)
assert.NoError(t, err)
assert.NotNil(t, client)

SetImplementation(client)

if os.Getenv("EXIT_FUNCTION") == "1" {
Panic("test exit")
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestPanic")
cmd.Env = append(os.Environ(), "EXIT_FUNCTION=1")
err = cmd.Run()
if e, ok := err.(*exec.ExitError); ok && !e.Success() {
return
}
t.Fatalf("process ran with err %v, want exit status 1", err)
}

// TestPanicln will test the Panicln() method
func TestPanicln(t *testing.T) {

client, err := NewLogEntriesClient(testToken, LogEntriesURL, LogEntriesPort)
assert.NoError(t, err)
assert.NotNil(t, client)

SetImplementation(client)

if os.Getenv("EXIT_FUNCTION") == "1" {
Panicln("test exit")
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestPanicln")
cmd.Env = append(os.Environ(), "EXIT_FUNCTION=1")
err = cmd.Run()
if e, ok := err.(*exec.ExitError); ok && !e.Success() {
return
}
t.Fatalf("process ran with err %v, want exit status 1", err)
}

// TestPanicf will test the Panicf() method
func TestPanicf(t *testing.T) {

client, err := NewLogEntriesClient(testToken, LogEntriesURL, LogEntriesPort)
assert.NoError(t, err)
assert.NotNil(t, client)

SetImplementation(client)

theImplementation := GetImplementation()
assert.NotNil(t, theImplementation)

if os.Getenv("EXIT_FUNCTION") == "1" {
Panicf("test %d", 1)
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestPanicf")
cmd.Env = append(os.Environ(), "EXIT_FUNCTION=1")
err = cmd.Run()
if e, ok := err.(*exec.ExitError); ok && !e.Success() {
return
}
t.Fatalf("process ran with err %v, want exit status 1", err)
}

0 comments on commit 5733004

Please sign in to comment.