Skip to content
This repository has been archived by the owner on May 1, 2020. It is now read-only.

Commit

Permalink
Use log.Logger instead of io.Writers for logging
Browse files Browse the repository at this point in the history
Use log.Logger instead of io.Writers for logging
to improve log customization.
  • Loading branch information
romshark committed May 9, 2018
1 parent 216d118 commit af872e5
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 42 deletions.
12 changes: 2 additions & 10 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,8 @@ func NewClient(

reqman.NewRequestManager(),

log.New(
opts.WarnLog,
"WARNING: ",
log.Ldate|log.Ltime|log.Lshortfile,
),
log.New(
opts.ErrorLog,
"ERROR: ",
log.Ldate|log.Ltime|log.Lshortfile,
),
opts.WarnLog,
opts.ErrorLog,
}

if autoconnect == AutoconnectEnabled {
Expand Down
20 changes: 14 additions & 6 deletions client/options.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package client

import (
"io"
"log"
"os"
"time"

Expand Down Expand Up @@ -48,10 +48,10 @@ type Options struct {
ReconnectionInterval time.Duration

// WarnLog defines the warn logging output target
WarnLog io.Writer
WarnLog *log.Logger

// ErrorLog defines the error logging output target
ErrorLog io.Writer
ErrorLog *log.Logger
}

// SetDefaults sets default values for undefined required options
Expand All @@ -72,11 +72,19 @@ func (opts *Options) SetDefaults() {
opts.ReconnectionInterval = 2 * time.Second
}

// Create default loggers to std-out/err when no loggers are specified
if opts.WarnLog == nil {
opts.WarnLog = os.Stdout
opts.WarnLog = log.New(
os.Stdout,
"WEBWIRE_CLT_WARN: ",
log.Ldate|log.Ltime|log.Lshortfile,
)
}

if opts.ErrorLog == nil {
opts.ErrorLog = os.Stderr
opts.ErrorLog = log.New(
os.Stderr,
"WEBWIRE_CLT_ERR: ",
log.Ldate|log.Ltime|log.Lshortfile,
)
}
}
14 changes: 14 additions & 0 deletions examples/chatroom/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"flag"
"log"
"os"
"time"

"github.com/qbeon/webwire-go/examples/chatroom/shared"
Expand Down Expand Up @@ -30,6 +32,18 @@ func NewChatroomClient(serverAddr string) *ChatroomClient {
// Session info parser function must override the default one
// for the session info object to be typed as shared.SessionInfo
SessionInfoParser: shared.SessionInfoParser,

// Custom loggers
WarnLog: log.New(
os.Stdout,
"WARN: ",
log.Ldate|log.Ltime|log.Lshortfile,
),
ErrorLog: log.New(
os.Stderr,
"ERR: ",
log.Ldate|log.Ltime|log.Lshortfile,
),
},
)

Expand Down
12 changes: 10 additions & 2 deletions examples/chatroom/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,16 @@ func main() {
ServerAddress: *serverAddr,
ServerOptions: wwr.ServerOptions{
SessionsEnabled: true,
WarnLog: os.Stdout,
ErrorLog: os.Stderr,
WarnLog: log.New(
os.Stdout,
"WARN: ",
log.Ldate|log.Ltime|log.Lshortfile,
),
ErrorLog: log.New(
os.Stderr,
"ERR: ",
log.Ldate|log.Ltime|log.Lshortfile,
),
},
},
)
Expand Down
5 changes: 1 addition & 4 deletions examples/echo/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,7 @@ func main() {
&EchoServer{},
wwr.HeadedServerOptions{
ServerAddress: *serverAddr,
ServerOptions: wwr.ServerOptions{
WarnLog: os.Stdout,
ErrorLog: os.Stderr,
},
ServerOptions: wwr.ServerOptions{},
},
)
if err != nil {
Expand Down
5 changes: 1 addition & 4 deletions examples/pubsub/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,7 @@ func main() {
serverImpl,
wwr.HeadedServerOptions{
ServerAddress: *serverAddr,
ServerOptions: wwr.ServerOptions{
WarnLog: os.Stdout,
ErrorLog: os.Stderr,
},
ServerOptions: wwr.ServerOptions{},
},
)
if err != nil {
Expand Down
20 changes: 14 additions & 6 deletions options.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package webwire

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

Expand All @@ -12,8 +12,8 @@ type ServerOptions struct {
SessionKeyGenerator SessionKeyGenerator
SessionInfoParser SessionInfoParser
MaxSessionConnections uint
WarnLog io.Writer
ErrorLog io.Writer
WarnLog *log.Logger
ErrorLog *log.Logger
}

// SetDefaults sets the defaults for undefined required values
Expand All @@ -31,12 +31,20 @@ func (srvOpt *ServerOptions) SetDefaults() {
srvOpt.SessionInfoParser = GenericSessionInfoParser
}

// Create default loggers to std-out/err when no loggers are specified
if srvOpt.WarnLog == nil {
srvOpt.WarnLog = os.Stdout
srvOpt.WarnLog = log.New(
os.Stdout,
"WEBWIRE_WARN: ",
log.Ldate|log.Ltime|log.Lshortfile,
)
}

if srvOpt.ErrorLog == nil {
srvOpt.ErrorLog = os.Stderr
srvOpt.ErrorLog = log.New(
os.Stderr,
"WEBWIRE_ERR: ",
log.Ldate|log.Ltime|log.Lshortfile,
)
}
}

Expand Down
12 changes: 2 additions & 10 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,8 @@ func NewServer(implementation ServerImplementation, opts ServerOptions) *Server

// Internals
connUpgrader: newConnUpgrader(),
warnLog: log.New(
opts.WarnLog,
"WARNING: ",
log.Ldate|log.Ltime|log.Lshortfile,
),
errorLog: log.New(
opts.ErrorLog,
"ERROR: ",
log.Ldate|log.Ltime|log.Lshortfile,
),
warnLog: opts.WarnLog,
errorLog: opts.ErrorLog,
}

return &srv
Expand Down

0 comments on commit af872e5

Please sign in to comment.