Skip to content

Commit

Permalink
Fixed logger
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzalezkrause committed Apr 16, 2019
1 parent a58813f commit a16188e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 33 deletions.
4 changes: 1 addition & 3 deletions dorker/scrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,4 @@ import (
"testing"
)

func TestScrapper(t *testing.T) {

}
func TestScrapper(t *testing.T) {}
28 changes: 0 additions & 28 deletions goca.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package goca

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

Expand All @@ -34,14 +33,10 @@ var (
AppName string
// AppVersion is the Application version
AppVersion string
// LogLevel sets the log level for Goca
LogLevel string
)

// StartTerm is the Goca library entrypoint for terms
func StartTerm(input Input) {
setLogLevel()

if input.ListURL {
fmt.Println("Goca has got the following URLs for you")
}
Expand Down Expand Up @@ -78,7 +73,6 @@ func StartTerm(input Input) {

// StartFolder is the Goca library entrypoint for folders
func StartFolder(input Input) {
setLogLevel()
if !validFolder(input.Folder) {
log.Fatalf("The folder %s is not valid\n", input.Folder)
}
Expand Down Expand Up @@ -118,8 +112,6 @@ func StartScrapper(input Input) {
// FIXME: This should be passed with the input config and set with a flag
var depth = 3

setLogLevel()

for _, plugType := range input.FileType {
// Initialize context or controller per each type
ctx := NewController(input)
Expand Down Expand Up @@ -197,26 +189,6 @@ func getDorkers(typ string, input Input) *dorker.Dorker {
return d
}

func setLogLevel() {
switch LogLevel {
case "info":
case "1":
log.SetLevel(log.InfoLevel)
case "warn":
case "2":
log.SetLevel(log.WarnLevel)
case "error":
case "3":
log.SetLevel(log.ErrorLevel)
case "debug":
case "4":
log.SetLevel(log.DebugLevel)
default:
log.SetOutput(ioutil.Discard)
log.SetLevel(log.InfoLevel)
}
}

func validFolder(folder string) bool {
_, err := os.Stat(folder)
return !os.IsNotExist(err)
Expand Down
31 changes: 29 additions & 2 deletions goca/goca.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var (
ua string
listPlugins bool
filetype = "*"
loglevel string
loglevel = "info"
scrapper string
projectName string
printProject string
Expand Down Expand Up @@ -73,11 +73,12 @@ func main() {
var err error
goca.AppName = appName
goca.AppVersion = gitTag
goca.LogLevel = loglevel
if len(gitTag) == 0 {
goca.AppVersion = "(dev)"
}

setLogLevel()

if listPlugins {
plugins := goca.ListPlugins()
for typ, plugs := range plugins {
Expand Down Expand Up @@ -180,3 +181,29 @@ func main() {
}
}
}

func setLogLevel() {
log.SetOutput(os.Stdout)

switch loglevel {
case "3":
log.SetLevel(log.DebugLevel)
case "debug":
log.SetLevel(log.DebugLevel)
case "2":
log.SetLevel(log.InfoLevel)
case "info":
log.SetLevel(log.InfoLevel)
case "1":
log.SetLevel(log.WarnLevel)
case "warn":
log.SetLevel(log.WarnLevel)
case "0":
log.SetLevel(log.ErrorLevel)
case "error":
log.SetLevel(log.ErrorLevel)
default:
log.SetLevel(log.InfoLevel)
log.Error("No valid loglevel, falling back to info level")
}
}

0 comments on commit a16188e

Please sign in to comment.