-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add customizable Logger interface + info,error and debugs (#14)
* Add customizable Logger interface + info,error and debugs * update travis.yml to atest golang versions
- Loading branch information
Showing
11 changed files
with
161 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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,67 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strconv" | ||
|
||
"gopkg.in/go-playground/webhooks.v3" | ||
"gopkg.in/go-playground/webhooks.v3/github" | ||
) | ||
|
||
const ( | ||
path = "/webhooks" | ||
port = 3016 | ||
) | ||
|
||
type myLogger struct { | ||
PrintDebugs bool | ||
} | ||
|
||
func (l *myLogger) Info(msg string) { | ||
log.Println(msg) | ||
} | ||
|
||
func (l *myLogger) Error(msg string) { | ||
log.Println(msg) | ||
} | ||
|
||
func (l *myLogger) Debug(msg string) { | ||
if !l.PrintDebugs { | ||
return | ||
} | ||
log.Println(msg) | ||
} | ||
|
||
func main() { | ||
// webhooks.DefaultLog=webhooks.NewLogger(true) | ||
// | ||
// or override with your own | ||
webhooks.DefaultLog = &myLogger{PrintDebugs: true} | ||
|
||
hook := github.New(&github.Config{Secret: "MyGitHubSuperSecretSecrect...?"}) | ||
hook.RegisterEvents(HandleMultiple, github.ReleaseEvent, github.PullRequestEvent) // Add as many as you want | ||
|
||
err := webhooks.Run(hook, ":"+strconv.Itoa(port), path) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
|
||
// HandleMultiple handles multiple GitHub events | ||
func HandleMultiple(payload interface{}, header webhooks.Header) { | ||
fmt.Println("Handling Payload..") | ||
|
||
switch payload.(type) { | ||
|
||
case github.ReleasePayload: | ||
release := payload.(github.ReleasePayload) | ||
// Do whatever you want from here... | ||
fmt.Printf("%+v", release) | ||
|
||
case github.PullRequestPayload: | ||
pullRequest := payload.(github.PullRequestPayload) | ||
// Do whatever you want from here... | ||
fmt.Printf("%+v", pullRequest) | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,44 @@ | ||
package webhooks | ||
|
||
import "log" | ||
|
||
// DefaultLog contains the default logger for webhooks, and prints only info and error messages by default | ||
// for debugs override DefaultLog or see NewLogger for creating one without debugs. | ||
var DefaultLog Logger = new(logger) | ||
|
||
// Logger allows for customizable logging | ||
type Logger interface { | ||
// Info prints basic information. | ||
Info(string) | ||
// Error prints error information. | ||
Error(string) | ||
// Debug prints information usefull for debugging. | ||
Debug(string) | ||
} | ||
|
||
// NewLogger returns a new logger for use. | ||
func NewLogger(debug bool) Logger { | ||
return &logger{PrintDebugs: debug} | ||
} | ||
|
||
type logger struct { | ||
PrintDebugs bool | ||
} | ||
|
||
// Info prints basic information. | ||
func (l *logger) Info(msg string) { | ||
log.Println("INFO:", msg) | ||
} | ||
|
||
// v prints error information. | ||
func (l *logger) Error(msg string) { | ||
log.Println("ERROR:", msg) | ||
} | ||
|
||
// Debug prints information usefull for debugging. | ||
func (l *logger) Debug(msg string) { | ||
if !l.PrintDebugs { | ||
return | ||
} | ||
log.Println("DEBUG:", msg) | ||
} |
Oops, something went wrong.