wslogger

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2023 License: MIT Imports: 4 Imported by: 0

README

wslogger

Go Report Card GitHub release (latest SemVer) GitHub Workflow Status (with event) GitHub last commit GitHub go.mod Go version

Demo GIF of the wslogger in action.

wslogger is a simple WebSocket-based logging helper for Go.

It offers the following functionality:

  • Real-time log broadcasting: broadcasts log messages to connected clients in real-time
  • Multiple clients support: handles multiple WebSocket clients connected simultaneously
  • Buffering: maintains a buffer of log messages to avoid blocking the application
  • Customizable logging: easily integrates with popular logging libraries like logrus and zap

This library defines two main types:

WSLogger: the core WebSocket-based logger that handles client connections and message broadcasting

WSWriter: an io.Writer implementation that writes log messages to the WSLogger broadcast channel

Installation

Use go get to install wslogger.

go get github.com/bay0/wslogger

Usage

To use the library, import the wslogger package and create a new instance of WSLogger using NewWSLogger().

package main

import (
 "io"
 "log"
 "net/http"
 "os"
 "time"

 "github.com/bay0/wslogger"

 "github.com/sirupsen/logrus"
)

func main() {
 wsLogger := wslogger.NewWSLogger()
 wsLogger.Start()

 http.HandleFunc("/ws", wsLogger.HandleConnections)
 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  http.ServeFile(w, r, "index.html")
 })

 go func() {
  log.Fatal(http.ListenAndServe(":8000", nil))
 }()

 wsWriter := wsLogger.NewWSWriter()

 logrus.SetFormatter(&logrus.JSONFormatter{
  TimestampFormat: time.RFC3339Nano,
 })

 logrus.SetOutput(io.MultiWriter(os.Stdout, wsWriter))

 for i := 0; i < 100000; i++ {
  logrus.Infof("This is log message #%d", i+1)
  logrus.Warnf("This is log message #%d", i+1)
  logrus.Errorf("This is log message #%d", i+1)

  time.Sleep(1 * time.Second)
 }
}

In this example, the wslogger package is integrated with the logrus logging library to broadcast log messages to WebSocket clients.

The WebSocket server listens on port 8000 and serves an index.html file for clients to connect and receive log messages.

Please refer to the provided example HTML and JavaScript code to implement the client-side WebSocket connection and log message rendering.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client added in v1.0.0

type Client struct {
	// contains filtered or unexported fields
}

type WSLogger

type WSLogger struct {
	// contains filtered or unexported fields
}

func NewWSLogger

func NewWSLogger() *WSLogger

func (*WSLogger) HandleConnections

func (wsl *WSLogger) HandleConnections(w http.ResponseWriter, r *http.Request)

func (*WSLogger) NewWSWriter

func (wsl *WSLogger) NewWSWriter() *WSWriter

func (*WSLogger) Start

func (wsl *WSLogger) Start()

type WSWriter

type WSWriter struct {
	// contains filtered or unexported fields
}

func (*WSWriter) Close added in v1.0.0

func (wsw *WSWriter) Close() error

func (*WSWriter) Write

func (wsw *WSWriter) Write(p []byte) (n int, err error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL