Skip to content

Commit

Permalink
Merge pull request #8 from projectdiscovery/issue-213/logutils
Browse files Browse the repository at this point in the history
added logutils
  • Loading branch information
Mzack9999 authored Nov 3, 2022
2 parents a7a13ff + a5c7554 commit 9bda3fc
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
2 changes: 2 additions & 0 deletions log/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# logutil
The package contains helpers to interact with logs
19 changes: 19 additions & 0 deletions log/logutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package logutil

import (
"io"
"log"
"os"
)

// DisableDefaultLogger disables the default logger.
func DisableDefaultLogger() {
log.SetFlags(0)
log.SetOutput(io.Discard)
}

// EnableDefaultLogger enables the default logger.
func EnableDefaultLogger() {
log.SetFlags(log.LstdFlags)
log.SetOutput(os.Stderr)
}
39 changes: 39 additions & 0 deletions log/logutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package logutil

import (
"bytes"
"io"
"log"
"os"
"testing"

"github.com/stretchr/testify/require"
)

func TestDisableDefaultLogger(t *testing.T) {
msg := "sample test"
buf := new(bytes.Buffer)
log.SetOutput(buf)
DisableDefaultLogger()
log.Print(msg)
require.Equal(t, "", buf.String())
}

func TestEnableDefaultLogger(t *testing.T) {
msg := "sample test"
buf := new(bytes.Buffer)
var stderr = *os.Stderr
r, w, _ := os.Pipe()
os.Stderr = w
exit := make(chan bool)
go func() {
_, _ = io.Copy(buf, r)
exit <- true
}()
EnableDefaultLogger()
log.Print(msg)
w.Close()
<-exit
os.Stderr = &stderr
require.Contains(t, buf.String(), msg)
}

0 comments on commit 9bda3fc

Please sign in to comment.