mirror of
https://github.com/lightninglabs/aperture.git
synced 2025-12-19 01:54:20 +01:00
multi: add persistent logger
This commit is contained in:
29
auth/log.go
Normal file
29
auth/log.go
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/btcsuite/btclog"
|
||||||
|
"github.com/lightningnetwork/lnd/build"
|
||||||
|
)
|
||||||
|
|
||||||
|
// log is a logger that is initialized with no output filters. This
|
||||||
|
// means the package will not perform any logging by default until the caller
|
||||||
|
// requests it.
|
||||||
|
var log btclog.Logger
|
||||||
|
|
||||||
|
// The default amount of logging is none.
|
||||||
|
func init() {
|
||||||
|
UseLogger(build.NewSubLogger("AUTH", nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
// DisableLog disables all library log output. Logging output is disabled
|
||||||
|
// by default until UseLogger is called.
|
||||||
|
func DisableLog() {
|
||||||
|
UseLogger(btclog.Disabled)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UseLogger uses a specified Logger to output package logging info.
|
||||||
|
// This should be used in preference to SetLogWriter if the caller is also
|
||||||
|
// using btclog.
|
||||||
|
func UseLogger(logger btclog.Logger) {
|
||||||
|
log = logger
|
||||||
|
}
|
||||||
@@ -11,6 +11,10 @@ var (
|
|||||||
defaultConfigFilename = "kirin.yaml"
|
defaultConfigFilename = "kirin.yaml"
|
||||||
defaultTLSKeyFilename = "tls.key"
|
defaultTLSKeyFilename = "tls.key"
|
||||||
defaultTLSCertFilename = "tls.cert"
|
defaultTLSCertFilename = "tls.cert"
|
||||||
|
defaultLogLevel = "info"
|
||||||
|
defaultLogFilename = "kirin.log"
|
||||||
|
defaultMaxLogFiles = 3
|
||||||
|
defaultMaxLogFileSize = 10
|
||||||
)
|
)
|
||||||
|
|
||||||
type config struct {
|
type config struct {
|
||||||
@@ -23,4 +27,8 @@ type config struct {
|
|||||||
// Services is a list of JSON objects in string format, which specify
|
// Services is a list of JSON objects in string format, which specify
|
||||||
// each backend service to Kirin.
|
// each backend service to Kirin.
|
||||||
Services []*proxy.Service `long:"service" description:"Configurations for each Kirin backend service."`
|
Services []*proxy.Service `long:"service" description:"Configurations for each Kirin backend service."`
|
||||||
|
|
||||||
|
// DebugLevel is a string defining the log level for the service either
|
||||||
|
// for all subsystems the same or individual level by subsystem.
|
||||||
|
DebugLevel string `long:"debuglevel" description:"Debug level for the Kirin application and its subsystems."`
|
||||||
}
|
}
|
||||||
|
|||||||
1
go.mod
1
go.mod
@@ -3,6 +3,7 @@ module github.com/lightninglabs/kirin
|
|||||||
go 1.13
|
go 1.13
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f
|
||||||
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d
|
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d
|
||||||
github.com/lightninglabs/loop v0.2.3-alpha
|
github.com/lightninglabs/loop v0.2.3-alpha
|
||||||
github.com/lightningnetwork/lnd v0.8.0-beta-rc3.0.20191029004703-c069bdd4c7c1
|
github.com/lightningnetwork/lnd v0.8.0-beta-rc3.0.20191029004703-c069bdd4c7c1
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -28,7 +28,6 @@ github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f h1:bAs4lUbRJpnnkd9
|
|||||||
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA=
|
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA=
|
||||||
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d h1:yJzD/yFppdVCf6ApMkVy8cUxV0XrxdP9rVf6D87/Mng=
|
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d h1:yJzD/yFppdVCf6ApMkVy8cUxV0XrxdP9rVf6D87/Mng=
|
||||||
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg=
|
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg=
|
||||||
github.com/btcsuite/btcwallet v0.0.0-20190911065739-d5cdeb4b91b0 h1:S9+cnZ7N4EvkkOBQ3lUy4p7+XjW4GS81R4QjwuT06Cw=
|
|
||||||
github.com/btcsuite/btcwallet v0.0.0-20190911065739-d5cdeb4b91b0/go.mod h1:ntLqUbZ12G8FmPX1nJj7W83WiAFOLRGiuarH4zDYdlI=
|
github.com/btcsuite/btcwallet v0.0.0-20190911065739-d5cdeb4b91b0/go.mod h1:ntLqUbZ12G8FmPX1nJj7W83WiAFOLRGiuarH4zDYdlI=
|
||||||
github.com/btcsuite/btcwallet v0.10.0 h1:fFZncfYJ7VByePTGttzJc3qfCyDzU95ucZYk0M912lU=
|
github.com/btcsuite/btcwallet v0.10.0 h1:fFZncfYJ7VByePTGttzJc3qfCyDzU95ucZYk0M912lU=
|
||||||
github.com/btcsuite/btcwallet v0.10.0/go.mod h1:4TqBEuceheGNdeLNrelliLHJzmXauMM2vtWfuy1pFiM=
|
github.com/btcsuite/btcwallet v0.10.0/go.mod h1:4TqBEuceheGNdeLNrelliLHJzmXauMM2vtWfuy1pFiM=
|
||||||
@@ -146,7 +145,6 @@ github.com/lightninglabs/neutrino v0.10.0 h1:yWVy2cOCCXbKFdpYCE9vD1fWRJDd9FtGXhU
|
|||||||
github.com/lightninglabs/neutrino v0.10.0/go.mod h1:C3KhCMk1Mcx3j8v0qRVWM1Ow6rIJSvSPnUAq00ZNAfk=
|
github.com/lightninglabs/neutrino v0.10.0/go.mod h1:C3KhCMk1Mcx3j8v0qRVWM1Ow6rIJSvSPnUAq00ZNAfk=
|
||||||
github.com/lightningnetwork/lightning-onion v0.0.0-20190909101754-850081b08b6a h1:GoWPN4i4jTKRxhVNh9a2vvBBO1Y2seiJB+SopUYoKyo=
|
github.com/lightningnetwork/lightning-onion v0.0.0-20190909101754-850081b08b6a h1:GoWPN4i4jTKRxhVNh9a2vvBBO1Y2seiJB+SopUYoKyo=
|
||||||
github.com/lightningnetwork/lightning-onion v0.0.0-20190909101754-850081b08b6a/go.mod h1:rigfi6Af/KqsF7Za0hOgcyq2PNH4AN70AaMRxcJkff4=
|
github.com/lightningnetwork/lightning-onion v0.0.0-20190909101754-850081b08b6a/go.mod h1:rigfi6Af/KqsF7Za0hOgcyq2PNH4AN70AaMRxcJkff4=
|
||||||
github.com/lightningnetwork/lnd v0.7.1-beta-rc2.0.20190914085956-35027e52fc22 h1:PWCIRUyow3Od4TMukVHL5jmNhjUPKhw6OVVruYCCUQ0=
|
|
||||||
github.com/lightningnetwork/lnd v0.7.1-beta-rc2.0.20190914085956-35027e52fc22/go.mod h1:VaY0b5o38keUN3Ga6GVb/Mgta4B/CcCXwNvPAvhbv/A=
|
github.com/lightningnetwork/lnd v0.7.1-beta-rc2.0.20190914085956-35027e52fc22/go.mod h1:VaY0b5o38keUN3Ga6GVb/Mgta4B/CcCXwNvPAvhbv/A=
|
||||||
github.com/lightningnetwork/lnd v0.8.0-beta-rc3.0.20191029004703-c069bdd4c7c1 h1:HZqM9i0znXr+FZAO1Km7bpnlUFt+/qbfFDkfOEDT6Gc=
|
github.com/lightningnetwork/lnd v0.8.0-beta-rc3.0.20191029004703-c069bdd4c7c1 h1:HZqM9i0znXr+FZAO1Km7bpnlUFt+/qbfFDkfOEDT6Gc=
|
||||||
github.com/lightningnetwork/lnd v0.8.0-beta-rc3.0.20191029004703-c069bdd4c7c1/go.mod h1:nq06y2BDv7vwWeMmwgB7P3pT7/Uj7sGf5FzHISVD6t4=
|
github.com/lightningnetwork/lnd v0.8.0-beta-rc3.0.20191029004703-c069bdd4c7c1/go.mod h1:nq06y2BDv7vwWeMmwgB7P3pT7/Uj7sGf5FzHISVD6t4=
|
||||||
|
|||||||
86
kirin.go
86
kirin.go
@@ -6,15 +6,13 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/lightninglabs/kirin/auth"
|
"github.com/lightninglabs/kirin/auth"
|
||||||
"github.com/lightninglabs/kirin/proxy"
|
"github.com/lightninglabs/kirin/proxy"
|
||||||
|
"github.com/lightningnetwork/lnd/build"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
/**
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Main is the true entrypoint of Kirin.
|
// Main is the true entrypoint of Kirin.
|
||||||
func Main() {
|
func Main() {
|
||||||
// TODO: Prevent from running twice.
|
// TODO: Prevent from running twice.
|
||||||
@@ -25,22 +23,21 @@ func Main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// start sets up the proxy server and runs it. This function blocks until a
|
||||||
|
// shutdown signal is received.
|
||||||
func start() error {
|
func start() error {
|
||||||
|
// First, parse configuration file and set up logging.
|
||||||
configFile := filepath.Join(kirinDataDir, defaultConfigFilename)
|
configFile := filepath.Join(kirinDataDir, defaultConfigFilename)
|
||||||
var cfg config
|
cfg, err := getConfig(configFile)
|
||||||
b, err := ioutil.ReadFile(configFile)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("unable to parse config file: %v", err)
|
||||||
}
|
}
|
||||||
|
err = setupLogging(cfg)
|
||||||
err = yaml.Unmarshal(b, &cfg)
|
if err != nil {
|
||||||
if err != nil {
|
return fmt.Errorf("unable to set up logging: %v", err)
|
||||||
return err
|
|
||||||
}
|
|
||||||
if cfg.ListenAddr == "" {
|
|
||||||
return fmt.Errorf("missing listen address for server")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create the auxiliary services the proxy needs to work.
|
||||||
authenticator, err := auth.NewLndAuthenticator(cfg.Authenticator)
|
authenticator, err := auth.NewLndAuthenticator(cfg.Authenticator)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -50,13 +47,68 @@ func start() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start the reverse proxy.
|
// Finally start the reverse proxy.
|
||||||
server := &http.Server{
|
server := &http.Server{
|
||||||
Addr: cfg.ListenAddr,
|
Addr: cfg.ListenAddr,
|
||||||
Handler: http.HandlerFunc(servicesProxy.ServeHTTP),
|
Handler: http.HandlerFunc(servicesProxy.ServeHTTP),
|
||||||
}
|
}
|
||||||
|
|
||||||
tlsKeyFile := filepath.Join(kirinDataDir, defaultTLSKeyFilename)
|
tlsKeyFile := filepath.Join(kirinDataDir, defaultTLSKeyFilename)
|
||||||
tlsCertFile := filepath.Join(kirinDataDir, defaultTLSCertFilename)
|
tlsCertFile := filepath.Join(kirinDataDir, defaultTLSCertFilename)
|
||||||
|
|
||||||
|
// The ListenAndServeTLS below will block until shut down or an error
|
||||||
|
// occurs. So we can just defer a cleanup function here that will close
|
||||||
|
// everything on shutdown.
|
||||||
|
defer cleanup(server)
|
||||||
return server.ListenAndServeTLS(tlsCertFile, tlsKeyFile)
|
return server.ListenAndServeTLS(tlsCertFile, tlsKeyFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getConfig loads and parses the configuration file then checks it for valid
|
||||||
|
// content.
|
||||||
|
func getConfig(configFile string) (*config, error) {
|
||||||
|
cfg := &config{}
|
||||||
|
b, err := ioutil.ReadFile(configFile)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
err = yaml.Unmarshal(b, cfg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then check the configuration that we got from the config file, all
|
||||||
|
// required values need to be set at this point.
|
||||||
|
if cfg.ListenAddr == "" {
|
||||||
|
return nil, fmt.Errorf("missing listen address for server")
|
||||||
|
}
|
||||||
|
return cfg, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// setupLogging parses the debug level and initializes the log file rotator.
|
||||||
|
func setupLogging(cfg *config) error {
|
||||||
|
if cfg.DebugLevel == "" {
|
||||||
|
cfg.DebugLevel = defaultLogLevel
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now initialize the logger and set the log level.
|
||||||
|
logFile := filepath.Join(kirinDataDir, defaultLogFilename)
|
||||||
|
err := logWriter.InitLogRotator(
|
||||||
|
logFile, defaultMaxLogFileSize, defaultMaxLogFiles,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return build.ParseAndSetDebugLevels(cfg.DebugLevel, logWriter)
|
||||||
|
}
|
||||||
|
|
||||||
|
// cleanup closes the given server and shuts down the log rotator.
|
||||||
|
func cleanup(server *http.Server) {
|
||||||
|
err := server.Close()
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Error closing server: %v", err)
|
||||||
|
}
|
||||||
|
log.Info("Shutdown complete")
|
||||||
|
err = logWriter.Close()
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Could not close log rotator: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
38
log.go
Normal file
38
log.go
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package kirin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/btcsuite/btclog"
|
||||||
|
"github.com/lightninglabs/kirin/auth"
|
||||||
|
"github.com/lightninglabs/kirin/proxy"
|
||||||
|
"github.com/lightningnetwork/lnd/build"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
logWriter = build.NewRotatingLogWriter()
|
||||||
|
|
||||||
|
log = build.NewSubLogger("MAIN", logWriter.GenSubLogger)
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
setSubLogger("MAIN", log, nil)
|
||||||
|
addSubLogger("AUTH", auth.UseLogger)
|
||||||
|
addSubLogger("PRXY", proxy.UseLogger)
|
||||||
|
}
|
||||||
|
|
||||||
|
// addSubLogger is a helper method to conveniently create and register the
|
||||||
|
// logger of a sub system.
|
||||||
|
func addSubLogger(subsystem string, useLogger func(btclog.Logger)) {
|
||||||
|
logger := build.NewSubLogger(subsystem, logWriter.GenSubLogger)
|
||||||
|
setSubLogger(subsystem, logger, useLogger)
|
||||||
|
}
|
||||||
|
|
||||||
|
// setSubLogger is a helper method to conveniently register the logger of a sub
|
||||||
|
// system.
|
||||||
|
func setSubLogger(subsystem string, logger btclog.Logger,
|
||||||
|
useLogger func(btclog.Logger)) {
|
||||||
|
|
||||||
|
logWriter.RegisterSubLogger(subsystem, logger)
|
||||||
|
if useLogger != nil {
|
||||||
|
useLogger(logger)
|
||||||
|
}
|
||||||
|
}
|
||||||
29
proxy/log.go
Normal file
29
proxy/log.go
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package proxy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/btcsuite/btclog"
|
||||||
|
"github.com/lightningnetwork/lnd/build"
|
||||||
|
)
|
||||||
|
|
||||||
|
// log is a logger that is initialized with no output filters. This
|
||||||
|
// means the package will not perform any logging by default until the caller
|
||||||
|
// requests it.
|
||||||
|
var log btclog.Logger
|
||||||
|
|
||||||
|
// The default amount of logging is none.
|
||||||
|
func init() {
|
||||||
|
UseLogger(build.NewSubLogger("PRXY", nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
// DisableLog disables all library log output. Logging output is disabled
|
||||||
|
// by default until UseLogger is called.
|
||||||
|
func DisableLog() {
|
||||||
|
UseLogger(btclog.Disabled)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UseLogger uses a specified Logger to output package logging info.
|
||||||
|
// This should be used in preference to SetLogWriter if the caller is also
|
||||||
|
// using btclog.
|
||||||
|
func UseLogger(logger btclog.Logger) {
|
||||||
|
log = logger
|
||||||
|
}
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
listenaddr: "localhost:8081"
|
listenaddr: "localhost:8081"
|
||||||
|
debuglevel: "debug"
|
||||||
|
|
||||||
services:
|
services:
|
||||||
# List of services that should be reachable behind the proxy.
|
# List of services that should be reachable behind the proxy.
|
||||||
# Requests will be matched to the services in order, picking the first
|
# Requests will be matched to the services in order, picking the first
|
||||||
|
|||||||
Reference in New Issue
Block a user