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 7 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
36 changes: 29 additions & 7 deletions back/cmd/openbsrv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,32 @@ import (
"path"

"github.com/codemodus/sigmon/v2"

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

func main() {
if err := run(); err != nil {
config := log.Config{
Err: log.Output{
Out: os.Stderr,
codegold79 marked this conversation as resolved.
Show resolved Hide resolved
Prefix: "[ERROR] ",
},
Inf: log.Output{
Out: os.Stdout,
Prefix: "[INFO] ",
},
}
log := log.New(config)

if err := run(log); err != nil {
cmd := path.Base(os.Args[0])
fmt.Fprintf(os.Stderr, "%s: %s\n", cmd, err)
log.Error("%s: %s", cmd, err)
codegold79 marked this conversation as resolved.
Show resolved Hide resolved
os.Exit(1)
}
}

func run() error {
func run(log *log.Log) error {
var (
dbdrvr = "mysql"
dbname = "openeug_openb_dev"
Expand All @@ -28,6 +43,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 +56,19 @@ 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()

if debug {
dbg.SetDebugOut(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 @@ -85,15 +107,15 @@ func run() error {
return err
}

m := newServerMgmt(gsrv, hsrv, fsrv)
m := newServerMgmt(log, gsrv, hsrv, fsrv)

sm.Set(func(s *sigmon.State) {
if err := m.stop(); err != nil {
fmt.Fprintln(os.Stderr, err)
log.Error(err.Error())
codegold79 marked this conversation as resolved.
Show resolved Hide resolved
}
})

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()
}
16 changes: 9 additions & 7 deletions back/cmd/openbsrv/srvmgmt.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package main

import (
"fmt"
"os"
"sync"
"time"

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

type server interface {
Expand All @@ -13,12 +13,14 @@ type server interface {
}

type serverMgmt struct {
ss []server
ss []server
log *log.Log
codegold79 marked this conversation as resolved.
Show resolved Hide resolved
}

func newServerMgmt(ss ...server) *serverMgmt {
func newServerMgmt(log *log.Log, ss ...server) *serverMgmt {
return &serverMgmt{
ss: ss,
ss: ss,
log: log,
}
}

Expand All @@ -32,7 +34,7 @@ func (m *serverMgmt) serve() error {

// TODO: gather returned errors
if err := s.Serve(); err != nil {
fmt.Fprintln(os.Stderr, "server error:", err)
m.log.Error("server error: %v", err)
codegold79 marked this conversation as resolved.
Show resolved Hide resolved
}
}(s)

Expand All @@ -49,7 +51,7 @@ func (m *serverMgmt) stop() error {
go func(s server) {
// TODO: gather returned errors
if err := s.Stop(); err != nil {
fmt.Fprintln(os.Stderr, "stop error:", err)
m.log.Error("stop error: %v", err)
}
}(s)
}
Expand Down
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
59 changes: 59 additions & 0 deletions back/internal/dbg/dbg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package dbg

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

// dbgLog is thread-safe.
type dbgLog struct {
log *log.Logger
out io.Writer
}

var atomicV atomic.Value
codegold79 marked this conversation as resolved.
Show resolved Hide resolved
var dbg = new()

func new() *dbgLog {
var dl dbgLog
atomicV.Store(&dl)
return &dl
}

func (dl *dbgLog) println(text string) {
dbg.log.Println(text)
}

func (dl *dbgLog) printf(format string, as ...interface{}) {
dbg.log.Printf(format+"\n", as...)
}
codegold79 marked this conversation as resolved.
Show resolved Hide resolved

func Log(text string) {
dbgLoad := atomicV.Load().(*dbgLog)
if dbgLoad.out == nil {
return
}
dbgLoad.println(text)
}

func Logf(format string, as ...interface{}) {
dbgLoad := atomicV.Load().(*dbgLog)
if dbgLoad.out == nil {
return
}
dbgLoad.printf(format, as...)
}

func SetDebugOut(out io.Writer) {
codegold79 marked this conversation as resolved.
Show resolved Hide resolved
dbgLoad := atomicV.Load().(*dbgLog)
dbgLoad.out = out

if out != nil {
dbgLoad.log = log.New(out, "", 0)
} else {
dbgLoad.log = log.Default()
}

atomicV.Store(dbgLoad)
}
37 changes: 37 additions & 0 deletions back/internal/log/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package log
codegold79 marked this conversation as resolved.
Show resolved Hide resolved

import (
"io"
"log"
)

type Output struct {
Out io.Writer
Prefix string
Flag int
codegold79 marked this conversation as resolved.
Show resolved Hide resolved
}

type Config struct {
Err Output
Inf Output
}

type Log struct {
err *log.Logger
inf *log.Logger
}

func New(c Config) *Log {
return &Log{
inf: log.New(c.Inf.Out, c.Inf.Prefix, c.Inf.Flag),
err: log.New(c.Err.Out, c.Err.Prefix, c.Inf.Flag),
}
}

func (log *Log) Info(format string, as ...interface{}) {
log.inf.Printf(format+"\n", as...)
}

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