Skip to content

Commit 7e0c1dd

Browse files
committed
multi: add persistent logger
1 parent 5a3b8b7 commit 7e0c1dd

File tree

8 files changed

+174
-17
lines changed

8 files changed

+174
-17
lines changed

auth/log.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package auth
2+
3+
import (
4+
"github.com/btcsuite/btclog"
5+
"github.com/lightningnetwork/lnd/build"
6+
)
7+
8+
// log is a logger that is initialized with no output filters. This
9+
// means the package will not perform any logging by default until the caller
10+
// requests it.
11+
var log btclog.Logger
12+
13+
// The default amount of logging is none.
14+
func init() {
15+
UseLogger(build.NewSubLogger("AUTH", nil))
16+
}
17+
18+
// DisableLog disables all library log output. Logging output is disabled
19+
// by default until UseLogger is called.
20+
func DisableLog() {
21+
UseLogger(btclog.Disabled)
22+
}
23+
24+
// UseLogger uses a specified Logger to output package logging info.
25+
// This should be used in preference to SetLogWriter if the caller is also
26+
// using btclog.
27+
func UseLogger(logger btclog.Logger) {
28+
log = logger
29+
}

config.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ var (
1111
defaultConfigFilename = "kirin.yaml"
1212
defaultTLSKeyFilename = "tls.key"
1313
defaultTLSCertFilename = "tls.cert"
14+
defaultLogLevel = "info"
15+
defaultLogFilename = "kirin.log"
16+
defaultMaxLogFiles = 3
17+
defaultMaxLogFileSize = 10
1418
)
1519

1620
type config struct {
@@ -23,4 +27,8 @@ type config struct {
2327
// Services is a list of JSON objects in string format, which specify
2428
// each backend service to Kirin.
2529
Services []*proxy.Service `long:"service" description:"Configurations for each Kirin backend service."`
30+
31+
// DebugLevel is a string defining the log level for the service either
32+
// for all subsystems the same or individual level by subsystem.
33+
DebugLevel string `long:"debuglevel" description:"Debug level for the Kirin application and its subsystems."`
2634
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/lightninglabs/kirin
33
go 1.13
44

55
require (
6+
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f
67
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d
78
github.com/lightninglabs/loop v0.2.3-alpha
89
github.com/lightningnetwork/lnd v0.8.0-beta-rc3.0.20191029004703-c069bdd4c7c1

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f h1:bAs4lUbRJpnnkd9
2828
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA=
2929
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d h1:yJzD/yFppdVCf6ApMkVy8cUxV0XrxdP9rVf6D87/Mng=
3030
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg=
31-
github.com/btcsuite/btcwallet v0.0.0-20190911065739-d5cdeb4b91b0 h1:S9+cnZ7N4EvkkOBQ3lUy4p7+XjW4GS81R4QjwuT06Cw=
3231
github.com/btcsuite/btcwallet v0.0.0-20190911065739-d5cdeb4b91b0/go.mod h1:ntLqUbZ12G8FmPX1nJj7W83WiAFOLRGiuarH4zDYdlI=
3332
github.com/btcsuite/btcwallet v0.10.0 h1:fFZncfYJ7VByePTGttzJc3qfCyDzU95ucZYk0M912lU=
3433
github.com/btcsuite/btcwallet v0.10.0/go.mod h1:4TqBEuceheGNdeLNrelliLHJzmXauMM2vtWfuy1pFiM=
@@ -146,7 +145,6 @@ github.com/lightninglabs/neutrino v0.10.0 h1:yWVy2cOCCXbKFdpYCE9vD1fWRJDd9FtGXhU
146145
github.com/lightninglabs/neutrino v0.10.0/go.mod h1:C3KhCMk1Mcx3j8v0qRVWM1Ow6rIJSvSPnUAq00ZNAfk=
147146
github.com/lightningnetwork/lightning-onion v0.0.0-20190909101754-850081b08b6a h1:GoWPN4i4jTKRxhVNh9a2vvBBO1Y2seiJB+SopUYoKyo=
148147
github.com/lightningnetwork/lightning-onion v0.0.0-20190909101754-850081b08b6a/go.mod h1:rigfi6Af/KqsF7Za0hOgcyq2PNH4AN70AaMRxcJkff4=
149-
github.com/lightningnetwork/lnd v0.7.1-beta-rc2.0.20190914085956-35027e52fc22 h1:PWCIRUyow3Od4TMukVHL5jmNhjUPKhw6OVVruYCCUQ0=
150148
github.com/lightningnetwork/lnd v0.7.1-beta-rc2.0.20190914085956-35027e52fc22/go.mod h1:VaY0b5o38keUN3Ga6GVb/Mgta4B/CcCXwNvPAvhbv/A=
151149
github.com/lightningnetwork/lnd v0.8.0-beta-rc3.0.20191029004703-c069bdd4c7c1 h1:HZqM9i0znXr+FZAO1Km7bpnlUFt+/qbfFDkfOEDT6Gc=
152150
github.com/lightningnetwork/lnd v0.8.0-beta-rc3.0.20191029004703-c069bdd4c7c1/go.mod h1:nq06y2BDv7vwWeMmwgB7P3pT7/Uj7sGf5FzHISVD6t4=

kirin.go

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ import (
66
"net/http"
77
"os"
88
"path/filepath"
9-
9+
1010
"github.com/lightninglabs/kirin/auth"
1111
"github.com/lightninglabs/kirin/proxy"
12+
"github.com/lightningnetwork/lnd/build"
1213
"gopkg.in/yaml.v2"
1314
)
1415

15-
/**
16-
*/
17-
1816
// Main is the true entrypoint of Kirin.
1917
func Main() {
2018
// TODO: Prevent from running twice.
@@ -25,22 +23,21 @@ func Main() {
2523
}
2624
}
2725

26+
// start sets up the proxy server and runs it. This function blocks until a
27+
// shutdown signal is received.
2828
func start() error {
29+
// First, parse configuration file and set up logging.
2930
configFile := filepath.Join(kirinDataDir, defaultConfigFilename)
30-
var cfg config
31-
b, err := ioutil.ReadFile(configFile)
31+
cfg, err := getConfig(configFile)
3232
if err != nil {
33-
return err
33+
return fmt.Errorf("unable to parse config file: %v", err)
3434
}
35-
36-
err = yaml.Unmarshal(b, &cfg)
35+
err = setupLogging(cfg)
3736
if err != nil {
38-
return err
39-
}
40-
if cfg.ListenAddr == "" {
41-
return fmt.Errorf("missing listen address for server")
37+
return fmt.Errorf("unable to set up logging: %v", err)
4238
}
4339

40+
// Create the auxiliary services the proxy needs to work.
4441
authenticator, err := auth.NewLndAuthenticator(cfg.Authenticator)
4542
if err != nil {
4643
return err
@@ -50,13 +47,68 @@ func start() error {
5047
return err
5148
}
5249

53-
// Start the reverse proxy.
50+
// Finally start the reverse proxy.
5451
server := &http.Server{
5552
Addr: cfg.ListenAddr,
5653
Handler: http.HandlerFunc(servicesProxy.ServeHTTP),
5754
}
58-
5955
tlsKeyFile := filepath.Join(kirinDataDir, defaultTLSKeyFilename)
6056
tlsCertFile := filepath.Join(kirinDataDir, defaultTLSCertFilename)
57+
58+
// The ListenAndServeTLS below will block until shut down or an error
59+
// occurs. So we can just defer a cleanup function here that will close
60+
// everything on shutdown.
61+
defer cleanup(server)
6162
return server.ListenAndServeTLS(tlsCertFile, tlsKeyFile)
6263
}
64+
65+
// getConfig loads and parses the configuration file then checks it for valid
66+
// content.
67+
func getConfig(configFile string) (*config, error) {
68+
cfg := &config{}
69+
b, err := ioutil.ReadFile(configFile)
70+
if err != nil {
71+
return nil, err
72+
}
73+
err = yaml.Unmarshal(b, cfg)
74+
if err != nil {
75+
return nil, err
76+
}
77+
78+
// Then check the configuration that we got from the config file, all
79+
// required values need to be set at this point.
80+
if cfg.ListenAddr == "" {
81+
return nil, fmt.Errorf("missing listen address for server")
82+
}
83+
return cfg, nil
84+
}
85+
86+
// setupLogging parses the debug level and initializes the log file rotator.
87+
func setupLogging(cfg *config) error {
88+
if cfg.DebugLevel == "" {
89+
cfg.DebugLevel = defaultLogLevel
90+
}
91+
92+
// Now initialize the logger and set the log level.
93+
logFile := filepath.Join(kirinDataDir, defaultLogFilename)
94+
err := logWriter.InitLogRotator(
95+
logFile, defaultMaxLogFileSize, defaultMaxLogFiles,
96+
)
97+
if err != nil {
98+
return err
99+
}
100+
return build.ParseAndSetDebugLevels(cfg.DebugLevel, logWriter)
101+
}
102+
103+
// cleanup closes the given server and shuts down the log rotator.
104+
func cleanup(server *http.Server) {
105+
err := server.Close()
106+
if err != nil {
107+
log.Errorf("Error closing server: %v", err)
108+
}
109+
log.Info("Shutdown complete")
110+
err = logWriter.Close()
111+
if err != nil {
112+
log.Errorf("Could not close log rotator: %v", err)
113+
}
114+
}

log.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package kirin
2+
3+
import (
4+
"github.com/btcsuite/btclog"
5+
"github.com/lightninglabs/kirin/auth"
6+
"github.com/lightninglabs/kirin/proxy"
7+
"github.com/lightningnetwork/lnd/build"
8+
)
9+
10+
var (
11+
logWriter = build.NewRotatingLogWriter()
12+
13+
log = build.NewSubLogger("MAIN", logWriter.GenSubLogger)
14+
)
15+
16+
func init() {
17+
setSubLogger("MAIN", log, nil)
18+
addSubLogger("AUTH", auth.UseLogger)
19+
addSubLogger("PRXY", proxy.UseLogger)
20+
}
21+
22+
// addSubLogger is a helper method to conveniently create and register the
23+
// logger of a sub system.
24+
func addSubLogger(subsystem string, useLogger func(btclog.Logger)) {
25+
logger := build.NewSubLogger(subsystem, logWriter.GenSubLogger)
26+
setSubLogger(subsystem, logger, useLogger)
27+
}
28+
29+
// setSubLogger is a helper method to conveniently register the logger of a sub
30+
// system.
31+
func setSubLogger(subsystem string, logger btclog.Logger,
32+
useLogger func(btclog.Logger)) {
33+
34+
logWriter.RegisterSubLogger(subsystem, logger)
35+
if useLogger != nil {
36+
useLogger(logger)
37+
}
38+
}

proxy/log.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package proxy
2+
3+
import (
4+
"github.com/btcsuite/btclog"
5+
"github.com/lightningnetwork/lnd/build"
6+
)
7+
8+
// log is a logger that is initialized with no output filters. This
9+
// means the package will not perform any logging by default until the caller
10+
// requests it.
11+
var log btclog.Logger
12+
13+
// The default amount of logging is none.
14+
func init() {
15+
UseLogger(build.NewSubLogger("PRXY", nil))
16+
}
17+
18+
// DisableLog disables all library log output. Logging output is disabled
19+
// by default until UseLogger is called.
20+
func DisableLog() {
21+
UseLogger(btclog.Disabled)
22+
}
23+
24+
// UseLogger uses a specified Logger to output package logging info.
25+
// This should be used in preference to SetLogWriter if the caller is also
26+
// using btclog.
27+
func UseLogger(logger btclog.Logger) {
28+
log = logger
29+
}

sample-conf.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
listenaddr: "localhost:8081"
2+
debuglevel: "debug"
3+
24
services:
35
# List of services that should be reachable behind the proxy.
46
# Requests will be matched to the services in order, picking the first

0 commit comments

Comments
 (0)