8000 Refactor WebSocket message logging from #81 by h0ru5 · Pull Request #251 · lorenzodonini/ocpp-go · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Refactor WebSocket message logging from #81 #251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions ws/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ const (
// The internal verbose logger
var log logging.Logger

// The internal message logger
var msgLog logging.Logger

// Sets a custom Logger implementation, allowing the package to log events.
// By default, a VoidLogger is used, so no logs will be sent to any output.
//
Expand All @@ -63,6 +66,28 @@ func SetLogger(logger logging.Logger) {
log = logger
}

// Sets a custom Logger implementation for messages, allowing the package to log ws messages seperately.
// By default, a VoidLogger is used, so no logs will be sent to any output.
//
// The function panics, if a nil logger is passed.
func SetMessageLogger(logger logging.Logger) {
if logger == nil {
panic("cannot set a nil logger")
}
msgLog = logger
}

// Logs a websocket message to the internal logger.
// introducing seperate function to have the opportunity for a dedicated message logger
func logMessage(args ...interface{}) {
if msgLog != nil {
msgLog.Debug(args...)
}
if log != nil {
log.Info(args...)
}
}

// Config contains optional configuration parameters for a websocket server.
// Setting the parameter allows to define custom timeout intervals for websocket network operations.
//
Expand Down Expand Up @@ -457,6 +482,7 @@ func (server *Server) Write(webSocketId string, data []byte) error {
return fmt.Errorf("couldn't write to websocket. No socket with id %v is open", webSocketId)
}
log.Debugf("queuing data for websocket %s", webSocketId)
logMessage("> ", webSocketId, string(data))
ws.outQueue <- data
return nil
}
Expand Down Expand Up @@ -593,6 +619,7 @@ func (server *Server) readPump(ws *WebSocket) {

if server.messageHandler != nil {
var channel Channel = ws
logMessage("< ", ws.ID(), string(message))
err = server.messageHandler(channel, message)
if err != nil {
server.error(fmt.Errorf("handling failed for %s: %w", ws.ID(), err))
Expand Down Expand Up @@ -964,6 +991,7 @@ func (client *Client) readPump() {

log.Debugf("received %v bytes", len(message))
if client.messageHandler != nil {
logMessage("< ", client.webSocket.ID(), string(message))
err = client.messageHandler(message)
if err != nil {
client.error(fmt.Errorf("handle failed: %w", err))
Expand Down Expand Up @@ -1034,6 +1062,7 @@ func (client *Client) Write(data []byte) error {
return fmt.Errorf("client is currently not connected, cannot send data")
}
log.Debugf("queuing data for server")
logMessage("> ", client.webSocket.ID(), string(data))
client.webSocket.outQueue <- data
return nil
}
Expand Down Expand Up @@ -1129,4 +1158,5 @@ func (client *Client) Errors() <-chan error {

func init() {
log = &logging.VoidLogger{}
msgLog = &logging.VoidLogger{}
}
0