Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a logging policy using sync/atomic load and store value #153

Merged
merged 25 commits into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
09232ff
Add logging policy
codegold79 Apr 11, 2021
f0b0167
Add debug and logging libraries
codegold79 May 31, 2021
2f45ea5
Sync/atomic version of debug log
codegold79 Jun 1, 2021
cdd91d8
Remote logging interface from main package
codegold79 Jun 1, 2021
98664ee
Make dbg package a singleton
codegold79 Jun 7, 2021
265fb75
Make log package more configurable
codegold79 Jun 7, 2021
37ecd01
Update main.go with log and dbg package changes
codegold79 Jun 7, 2021
6bbaba2
Remove Flag output option for logs
codegold79 Jun 19, 2021
914cc23
Add documentation to log and dbg packages
codegold79 Jun 19, 2021
94c5a78
instantiate log in run, not main; fmt.print errors and not log
codegold79 Jun 23, 2021
e7b82d8
rename print{f,ln} methods to log{f,ln}; use interface, not string args
codegold79 Jun 29, 2021
ef29245
Make atomic.Value not a global
codegold79 Jun 29, 2021
316b47b
undo using log and return to printing directly to stderr
codegold79 Jun 29, 2021
001cd5c
Rename dbgLog struct to dbg
codegold79 Jun 29, 2021
38f2a65
Undo change to error server stop message
codegold79 Jun 29, 2021
644e17e
Changes per PR: Rename, remove param, change output
codegold79 Jul 5, 2021
27dce31
Guard dbg methods scope concurrent processes
codegold79 Jul 5, 2021
b563c5d
use dbg object's methods over global's; move var closer to use
codegold79 Jul 10, 2021
85d5bbe
Add benchmark test
codegold79 Jul 10, 2021
9303f1b
improve benchmark test
codegold79 Jul 10, 2021
1007345
Use b.RunParallel in benchmark tests
codegold79 Jul 20, 2021
08aab25
store and load log.Logger in sync/atomic value
codegold79 Jul 27, 2021
050f097
Add tests ensuring debug setting works as intended
codegold79 Aug 5, 2021
2b92b15
Test Logf after second SetOut
codegold79 Aug 5, 2021
3c42fd6
Update main.go to use dbg.SetOut
codegold79 Aug 5, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion back/cmd/openbsrv/httpsrv.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package main

import (
"github.com/OpenEugene/openboard/back/internal/httpsrv"
"github.com/codemodus/hedrs"

"github.com/OpenEugene/openboard/back/internal/httpsrv"
)

type httpSrv struct {
Expand Down
26 changes: 24 additions & 2 deletions back/cmd/openbsrv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"path"

"github.com/codemodus/sigmon/v2"

"github.com/OpenEugene/openboard/back/internal/dbg"
"github.com/OpenEugene/openboard/back/internal/log"
)

func main() {
Expand All @@ -28,6 +31,7 @@ func run() error {
migrate bool
rollback bool
skipsrv bool
debug bool
codegold79 marked this conversation as resolved.
Show resolved Hide resolved
frontDir = "../../../front/public"
migTblPfx = "mig_"
)
Expand All @@ -40,13 +44,31 @@ func run() error {
flag.BoolVar(&migrate, "migrate", migrate, "migrate up")
flag.BoolVar(&rollback, "rollback", rollback, "migrate dn")
flag.BoolVar(&skipsrv, "skipsrv", skipsrv, "skip server run")
flag.BoolVar(&debug, "debug", debug, "debug true or false")
flag.StringVar(&frontDir, "frontdir", frontDir, "front public assets directory")
flag.Parse()

sm := sigmon.New(nil)
sm.Start()
defer sm.Stop()

logCfg := log.Config{
Err: log.Output{
Out: os.Stdout,
Prefix: "[ERROR] ",
},
Inf: log.Output{
Out: os.Stdout,
Prefix: "[INFO] ",
},
}
log := log.New(logCfg)

if debug {
dbg.SetOut(os.Stdout)
}

dbg.Logf("set up SQL database at %s:%s.", dbaddr, dbport)
db, err := newSQLDB(dbdrvr, dbCreds(dbname, dbuser, dbpass, dbaddr, dbport))
if err != nil {
return err
Expand All @@ -67,7 +89,7 @@ func run() error {
if mres.HasError() {
return mres.ErrsErr()
}
fmt.Println(migType+":", mres)
log.Info("%s: %s", migType, mres)
}

if skipsrv {
Expand All @@ -93,7 +115,7 @@ func run() error {
}
})

fmt.Println("to gracefully stop the application, send signal like TERM (CTRL-C) or HUP")
log.Info("to gracefully stop the application, send signal like TERM (CTRL-C) or HUP")

return m.serve()
}
4 changes: 2 additions & 2 deletions back/internal/authsvc/authsvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package authsvc
import (
"context"

"github.com/OpenEugene/openboard/back/internal/pb"
"google.golang.org/grpc"

"github.com/OpenEugene/openboard/back/internal/pb"
)

var _ pb.AuthServer = &AuthSvc{}
Expand All @@ -30,7 +31,6 @@ func (s *AuthSvc) RegisterWithGRPCServer(g *grpc.Server) error {
// AddAuth implements part of the pb.AuthServer interface.
func (s *AuthSvc) AddAuth(ctx context.Context, req *pb.AddAuthReq) (*pb.AuthResp, error) {
// TODO: implement AddAuth

return nil, nil
}

Expand Down
64 changes: 64 additions & 0 deletions back/internal/dbg/dbg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Package dbg allows for outputting information that can help with debugging
// the application.
package dbg

import (
"io"
"log"
"sync/atomic"
)

// dbg is thread-safe.
type dbg struct {
atomVal atomic.Value
codegold79 marked this conversation as resolved.
Show resolved Hide resolved
}

func new() *dbg {
var d dbg
var logr *log.Logger
d.atomVal.Store(logr)
return &d
}

func (d *dbg) logln(as ...interface{}) {
logr := d.atomVal.Load().(*log.Logger)
if logr != nil {
logr.Println(as...)
}
}

func (d *dbg) logf(format string, as ...interface{}) {
logr := d.atomVal.Load().(*log.Logger)
if logr != nil {
logr.Printf(format+"\n", as...)
}
}

func (d *dbg) setOut(out io.Writer) {
var logr *log.Logger

if out != nil {
logr = log.New(out, "", 0)
d.atomVal.Store(logr)
return
}

d.atomVal.Store(logr)
}

var debug = new()

// Log outputs information to help with application debugging.
func Log(as ...interface{}) {
debug.logln(as...)
}

// Logf outputs debugging information and is able to interpret formatting verbs.
func Logf(format string, as ...interface{}) {
debug.logf(format, as...)
}

// SetDebugOut allows for choosing where debug information will be written to.
func SetOut(out io.Writer) {
debug.setOut(out)
}
74 changes: 74 additions & 0 deletions back/internal/dbg/dbg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package dbg

import (
"bytes"
"fmt"
"io/ioutil"
"testing"
)

codegold79 marked this conversation as resolved.
Show resolved Hide resolved
func BenchmarkDbgUse(b *testing.B) {
SetOut(ioutil.Discard)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
Log("")
}
})
}
func BenchmarkDbgUseNil(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
Log("")
}
})
}
func BenchmarkDbgSetAndUse(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
SetOut(ioutil.Discard)
Log("")
}
})
}
func BenchmarkDbgSetAndUseNil(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
SetOut(nil)
Log("")
}
})
}

func TestSetOut(t *testing.T) {
Log("debug not set")

buff := bytes.NewBuffer([]byte{})
SetOut(buff)
msg := "debug set to bytes buffer"
Log(msg)
got := buff.String()

want := msg + "\n"
if got != want {
t.Errorf("want: %s, got: %s", want, got)
}

SetOut(nil)
buff.Reset()
want = ""
Log("debug set to nil")
got = buff.String()
if got != want {
t.Errorf("want: nothing, got: %s", got)
}

SetOut(buff)
buff.Reset()
msg = "debug set to bytes buffer, %s time"
Logf(msg, "second")
got = buff.String()
want = fmt.Sprintf(msg+"\n", "second")
if got != want {
t.Errorf("want: %s, got: %s", want, got)
}
}
45 changes: 45 additions & 0 deletions back/internal/log/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Package log allows for outputting information regarding application errors
// and events important to know for proper operation.
package log
codegold79 marked this conversation as resolved.
Show resolved Hide resolved

import (
"io"
"log"
)

// Output is where logger will write to. Prefix is what will precede log text.
type Output struct {
Out io.Writer
Prefix string
}

// Config allows for configuring the logger outputs for errors and information
// type log messages.
type Config struct {
Err Output
Inf Output
}

// Log is able to write information to a chosen output.
type Log struct {
err *log.Logger
inf *log.Logger
}

// New provides a pointer to a new instance of the Log object.
func New(c Config) *Log {
return &Log{
inf: log.New(c.Inf.Out, c.Inf.Prefix, 0),
err: log.New(c.Err.Out, c.Err.Prefix, 0),
}
}

// Info outputs information from the application.
func (log *Log) Info(format string, as ...interface{}) {
log.inf.Printf(format+"\n", as...)
}

// Error outputs error information from the application.
func (log *Log) Error(format string, as ...interface{}) {
log.err.Printf(format+"\n", as...)
}
3 changes: 2 additions & 1 deletion back/internal/postsvc/postsvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"context"
"database/sql"

"google.golang.org/grpc"

"github.com/OpenEugene/openboard/back/internal/pb"
"github.com/OpenEugene/openboard/back/internal/postsvc/internal/postdb"
"github.com/OpenEugene/openboard/back/internal/postsvc/internal/postdb/mysqlmig"
"google.golang.org/grpc"
)

var _ pb.PostServer = &PostSvc{}
Expand Down