Skip to content

Commit

Permalink
logging: Added debug level logging
Browse files Browse the repository at this point in the history
Added debug option to the config, and added debug level logs around the
program
  • Loading branch information
kofoworola committed May 8, 2021
1 parent 01546ab commit 3a05aa2
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ change to upper case.
### Available config values
| Name | Type | Description | Default |
|----------|------|-----------------------| ----- |
| `debug` | boolean | If set to true, debug log will be sent along side warning and error logs | `false` |
| `server.port`| string | Port the proxy's server will listen on | null|
| `server.auth`| []string| Array of allowed [Basic](https://tools.ietf.org/html/rfc7617) authorization strings in the form `user-id:password`| [] |
| `server.health.status` | int | Status code to respond with when liveness checks (Get requests to `server.host`) are made, an empty status code means tunnelify will not respond to liveness checks | nil |
Expand Down
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Config struct {
LivenessStatus int
LivenessBody string
LivenessPath string
Debug bool
}

var defaults = map[string]interface{}{
Expand Down Expand Up @@ -54,6 +55,7 @@ func LoadConfig(path string) (*Config, error) {
LivenessStatus: viper.GetInt("server.health.status"),
LivenessBody: viper.GetString("server.health.body"),
LivenessPath: viper.GetString("server.health.path"),
Debug: viper.GetBool("debug"),
}

if err := cfg.Validate(); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (l *listenerGateway) Accept() (net.Conn, error) {
return nil, l.listenerErr
}
c := <-l.connChan
l.logger.Debug("connection forwarded to liveness server")
return c, nil
}

Expand All @@ -62,6 +63,7 @@ func (l *listenerGateway) Start() error {
l.logger.LogError("error accepting a new connection", err)
break
}
l.logger.Debug("received new connection")
var h handler.ConnectionHandler

// read first line of the connection and use an appropriate handler
Expand Down
2 changes: 2 additions & 0 deletions handler/proxyhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (p *ProxyHandler) Handle(logger *logging.Logger) {
logger.Warn("error parsing request", nil)
return
}
logger.Debug("received new request")

// setup outgoing connection if it hasn't been setUp
if p.outgoing == nil {
Expand All @@ -82,6 +83,7 @@ func (p *ProxyHandler) Handle(logger *logging.Logger) {

// check the authorization
if !checkAuthorization(p.cfg, req) {
logger.Debug("request not authorized")
if err := WriteResponse(
p.incoming,
req.Proto,
Expand Down
1 change: 1 addition & 0 deletions handler/tunnelhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func (h *TunnelHandler) Handle(logger *logging.Logger) {
}
// check the authorization
if !checkAuthorization(h.cfg, req) {
logger.Debug("connection not authorized")
if err := WriteResponse(
h.incoming,
req.Proto,
Expand Down
13 changes: 8 additions & 5 deletions logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

type Logger struct {
logger *zap.Logger
*zap.Logger
}

func NewLogger(cfg *config.Config) (*Logger, error) {
Expand All @@ -24,6 +24,9 @@ func NewLogger(cfg *config.Config) (*Logger, error) {
EncoderConfig: prodEncoderConfig,
OutputPaths: logPaths,
}
if cfg.Debug {
config.Level = zap.NewAtomicLevelAt(zapcore.DebugLevel)
}

logger, err := config.Build()
if err != nil {
Expand All @@ -34,7 +37,7 @@ func NewLogger(cfg *config.Config) (*Logger, error) {

func (l *Logger) LogError(msg string, err error) {
if err != nil {
l.logger.Error(
l.Logger.Error(
msg,
zapcore.Field{
Key: "error",
Expand All @@ -46,7 +49,7 @@ func (l *Logger) LogError(msg string, err error) {

func (l *Logger) With(key, val string) *Logger {
return &Logger{
l.logger.With(zapcore.Field{
l.Logger.With(zapcore.Field{
Key: key,
String: val,
Type: zapcore.StringType,
Expand All @@ -56,10 +59,10 @@ func (l *Logger) With(key, val string) *Logger {

func (l *Logger) Warn(msg string, err error) {
if err == nil {
l.logger.Warn(msg)
l.Logger.Warn(msg)
return
}
l.logger.Warn(
l.Logger.Warn(
msg,
zapcore.Field{
Key: "error",
Expand Down

0 comments on commit 3a05aa2

Please sign in to comment.