Added custom logging support using zerolog
This commit is contained in:
54
main.go
54
main.go
@@ -15,13 +15,14 @@ import (
|
||||
"time"
|
||||
|
||||
"projectreshoot/db"
|
||||
"projectreshoot/logging"
|
||||
"projectreshoot/server"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Initializes and runs the server
|
||||
func run(ctx context.Context, w io.Writer, args []string) error {
|
||||
func run(ctx context.Context, w io.Writer, args map[string]string) error {
|
||||
ctx, cancel := signal.NotifyContext(ctx, os.Interrupt)
|
||||
defer cancel()
|
||||
|
||||
@@ -30,10 +31,35 @@ func run(ctx context.Context, w io.Writer, args []string) error {
|
||||
return errors.Wrap(err, "server.GetConfig")
|
||||
}
|
||||
|
||||
var logfile *os.File = nil
|
||||
if config.LogOutput == "both" || config.LogOutput == "file" {
|
||||
logfile, err = logging.GetLogFile(config.LogDir)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "logging.GetLogFile")
|
||||
}
|
||||
defer logfile.Close()
|
||||
}
|
||||
|
||||
var consoleWriter io.Writer
|
||||
if config.LogOutput == "both" || config.LogOutput == "console" {
|
||||
consoleWriter = w
|
||||
}
|
||||
|
||||
logger, err := logging.GetLogger(
|
||||
config.LogLevel,
|
||||
consoleWriter,
|
||||
logfile,
|
||||
config.LogDir,
|
||||
)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "logging.GetLogger")
|
||||
}
|
||||
|
||||
conn, err := db.ConnectToDatabase(&config.TursoDBName, &config.TursoToken)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "db.ConnectToDatabase")
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
srv := server.NewServer(config, conn)
|
||||
httpServer := &http.Server{
|
||||
@@ -41,9 +67,9 @@ func run(ctx context.Context, w io.Writer, args []string) error {
|
||||
Handler: srv,
|
||||
}
|
||||
|
||||
// TEST: runs function for testing in dev if --test flag true
|
||||
if args[1] == "true" {
|
||||
test(config, conn, httpServer)
|
||||
// Runs function for testing in dev if --test flag true
|
||||
if args["test"] == "true" {
|
||||
test(config, logger, conn, httpServer)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -77,10 +103,24 @@ var static embed.FS
|
||||
// Start of runtime. Parse commandline arguments & flags, Initializes context
|
||||
// and starts the server
|
||||
func main() {
|
||||
port := flag.String("port", "", "Override port")
|
||||
test := flag.Bool("test", false, "Run test function")
|
||||
// Parse commandline args
|
||||
host := flag.String("host", "", "Override host to listen on")
|
||||
port := flag.String("port", "", "Override port to listen on")
|
||||
test := flag.Bool("test", false, "Run test function instead of main program")
|
||||
loglevel := flag.String("loglevel", "", "Set log level")
|
||||
logoutput := flag.String("logoutput", "", "Set log destination (file, console or both)")
|
||||
flag.Parse()
|
||||
args := []string{*port, strconv.FormatBool(*test)}
|
||||
|
||||
// Map the args for easy access
|
||||
args := map[string]string{
|
||||
"host": *host,
|
||||
"port": *port,
|
||||
"test": strconv.FormatBool(*test),
|
||||
"loglevel": *loglevel,
|
||||
"logoutput": *logoutput,
|
||||
}
|
||||
|
||||
// Start the server
|
||||
ctx := context.Background()
|
||||
if err := run(ctx, os.Stdout, args); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%s\n", err)
|
||||
|
||||
Reference in New Issue
Block a user