logdyhq / logdy-core

Web based real-time log viewer. Stream ANY content to a web UI with autogenerated filters. Parse any format with TypeScript.
https://logdy.dev
Apache License 2.0
1.24k stars 30 forks source link

InitializeLogdy should return the servemux, not accept it #60

Open CAFxX opened 4 days ago

CAFxX commented 4 days ago

There is currently no way to initialize logdy before the mux is created. When logdy is embedded in existing applications, it is not a given that the servemux is created before everything else: important logic (that requires logging infrastructure to be already up and running, e.g. loading configuration data, opening/testing database connections, loading/initializing static data or caches) can and does run before the servemux creation. If InitializeLogdy returned the servemux, logdy could be initialized ahead of time, solving the "logging before servemux exists" requirement, and then when the application servemux is ready the logdy servemux could be easily added to it.

PeterOsinski commented 4 days ago

Hey, wouldn't this code from the examples work for you? Docs example as well.

mux := http.NewServeMux()
logger := logdy.InitializeLogdy(logdy.Config{
   HttpPathPrefix: "/_logdy-ui",
   LogLevel:       logdy.LOG_LEVEL_NORMAL,
}, mux)

// startup logic here, setting up DB connections, etc
// logger.log()

addr := ":8082"
log.Printf("server is listening at %s", addr)
log.Fatal(http.ListenAndServe(addr, &Logger{logdy: logger, handler: mux}))

This example still requires you to create mux before initializing Logdy, but you can control when it starts. Creating mux is not a heavy operation therefore changing your application logic to create mux first shouldn't be a problem. I have not tested it but, it should be possible to pass an empty mux

mux := http.NewServeMux()
logger := logdy.InitializeLogdy(logdy.Config{}, mux)

// logic using logger.log()

// add handler functions later
mux.HandleFunc("/v1/hello", func(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Hello, World!"))
}))