Skip to content

Commit

Permalink
extend example with logger
Browse files Browse the repository at this point in the history
  • Loading branch information
takama committed Jan 31, 2015
1 parent 82d9b2f commit a589360
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions example/myservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const (
port = ":9977"
)

var stdlog, errlog *log.Logger

// Service has embedded daemon
type Service struct {
daemon.Daemon
Expand Down Expand Up @@ -76,8 +78,8 @@ func (service *Service) Manage() (string, error) {
case conn := <-listen:
go handleClient(conn)
case killSignal := <-interrupt:
log.Println("Got signal:", killSignal)
log.Println("Stoping listening on ", listener.Addr())
stdlog.Println("Got signal:", killSignal)
stdlog.Println("Stoping listening on ", listener.Addr())
listener.Close()
if killSignal == os.Interrupt {
return "Daemon was interruped by system signal", nil
Expand Down Expand Up @@ -112,16 +114,21 @@ func handleClient(client net.Conn) {
}
}

func init() {
stdlog = log.New(os.Stdout, "", log.Ldate|log.Ltime)
errlog = log.New(os.Stderr, "", log.Ldate|log.Ltime)
}

func main() {
srv, err := daemon.New(name, description)
if err != nil {
fmt.Println("Error: ", err)
errlog.Println("Error: ", err)
os.Exit(1)
}
service := &Service{srv}
status, err := service.Manage()
if err != nil {
fmt.Println(status, "\nError: ", err)
errlog.Println(status, "\nError: ", err)
os.Exit(1)
}
fmt.Println(status)
Expand Down

0 comments on commit a589360

Please sign in to comment.