Skip to content

Golang logs viewer

You can embed Logdy directly into any Golang codebase and extend the application with logs being streamed to a self-hosted UI interface. It doesn't matter if it's a web app, a worker process or a terminal app, Logdy can be embedded into any Go code.

Prerequisite

Logdy will either bind a server from the current net/http package used in the code (if you already using one) or will set up its own webserver on a specified port. More examples below.

Install

Run the go get command in your project's root directory:

bash
$ go get github.com/logdyhq/logdy-core/logdy

A minimal setup

The below code represents a minimal Golang application setup.

go
package main

import (
	"time"
	"github.com/logdyhq/logdy-core/logdy"
)

func main() {

    // This will start a webserver 
    // with a Logdy UI on 127.0.0.1:8080
	logdyLogger := logdy.InitializeLogdy(logdy.Config{
		ServerIp:       "127.0.0.1",
		ServerPort:     "8080",
	}, nil)

    // Run in the loop forever
    for {
        // Produce structured message that will be translated to JSON
        logdyLogger.Log(logdy.Fields{"foo": "bar"})

        // Produce a string
        logdyLogger.LogString("This is just a string " + time.Now().String())

        time.Sleep(1 * time.Second)
    }

}

Use existing webserver

Logdy is able to bind to an existing webserver, this mode could be especially useful if you don't want to open another port for security reasons. Just omit the ServerIp and ServerPort options in the config. The code below represents a minimal setup required to run Logdy from within a Go application.

go
http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
    logger.Log(logdy.Fields{"url": r.URL.String()})
})

logger = logdy.InitializeLogdy(logdy.Config{HttpPathPrefix: "/_logdy-ui"}, nil)

log.Fatal(http.ListenAndServe(":8080", nil))

The above expression will bind to the existing net/http server running. Make sure to include this line before starting the server.

Use existing multiplexer

You can also provide mux instance and extend its path definition.

go
mux := http.NewServeMux()
mux.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
    logger.Log(logdy.Fields{"url": r.URL.String()})
    w.Write([]byte("Hello, World!"))
})

logger := logdy.InitializeLogdy(
    logdy.Config{HttpPathPrefix: "/_logdy-ui"}, 
    mux, // mux provided as an argument
)

log.Fatal(http.ListenAndServe(addr, &Logger{logdy: logger, handler: mux}))

Extensive example

The example below presents multiple options used along with log intercepter

go
logdyLogger := logdy.InitializeLogdy(logdy.Config{

    // Setting up own webserver
    ServerIp:       "127.0.0.1",
    ServerPort:     "8080",

    // Access to the UI will be restricted
    UiPass:         "foobar12345",

    // UI will be served at: 127.0.0.1:8080/_logdy-ui
    HttpPathPrefix: "_logdy-ui",

    // We'll intercept all internal Logdy log messages
    // and print them in a custom format
    LogInterceptor: func(entry *logdy.LogEntry) {
        log.Println("Logdy internal log message intercepted", entry.Message, entry.Data, entry.Time)
    },
}, nil)