Skip to content

Commit

Permalink
feat: add internal logger package
Browse files Browse the repository at this point in the history
  • Loading branch information
torives committed Oct 4, 2023
1 parent b359f5d commit fe320ad
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 10 deletions.
4 changes: 2 additions & 2 deletions cmd/cartesi-rollups-node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
package main

import (
"os"
"github.com/cartesi/rollups-node/internal/pkg/logger"
)

func main() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
logger.Error.Fatal(err)
}
}
52 changes: 52 additions & 0 deletions internal/pkg/logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

package logger

import (
"io"
"log"
"os"
"strings"
)

var (
Error *log.Logger
Warning *log.Logger
Info *log.Logger
Debug *log.Logger
)

func init() {
//FIXME: There might be a problem if this runs before the `config` package
// has altered the env vars
logLevel := os.Getenv("CARTESI_LOG_LEVEL")
_, enableTimestamp := os.LookupEnv("CARTESI_LOG_ENABLE_TIMESTAMP") //LOG_ENABLE_TIMESTAMP
var flags int
if enableTimestamp {
flags |= log.Ldate | log.Ltime
}

Error = log.New(os.Stderr, "ERROR ", flags)
Warning = log.New(os.Stderr, "WARN ", flags)
Info = log.New(os.Stdout, "INFO ", flags)
Debug = log.New(os.Stdout, "DEBUG ", flags)

if strings.EqualFold(logLevel, "error") {
Warning.SetOutput(io.Discard)
Info.SetOutput(io.Discard)
Debug.SetOutput(io.Discard)
} else if strings.EqualFold(logLevel, "warn") {
Info.SetOutput(io.Discard)
Debug.SetOutput(io.Discard)
} else if strings.EqualFold(logLevel, "debug") {
flags |= log.Llongfile
Error.SetFlags(flags)
Warning.SetFlags(flags)
Info.SetFlags(flags)
Debug.SetFlags(flags)
} else {
// INFO is the default option
Debug.SetOutput(io.Discard)
}
}
7 changes: 4 additions & 3 deletions internal/pkg/services/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ package services

import (
"context"
"fmt"
"os"
"os/exec"
"syscall"

"github.com/cartesi/rollups-node/internal/pkg/logger"
)

const (
Expand All @@ -29,10 +30,10 @@ func (g GraphQLService) Start(ctx context.Context) error {

go func() {
<-ctx.Done()
fmt.Printf("%v: %v\n", g.String(), ctx.Err())
logger.Debug.Printf("%v: %v\n", g.String(), ctx.Err())
if err := cmd.Process.Signal(syscall.SIGTERM); err != nil {
msg := "%v: failed to send SIGTERM to %v\n"
fmt.Printf(msg, g.String(), binaryName)
logger.Error.Printf(msg, g.String(), binaryName)
}
}()

Expand Down
12 changes: 7 additions & 5 deletions internal/pkg/services/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"context"
"fmt"
"time"

"github.com/cartesi/rollups-node/internal/pkg/logger"
)

// A service that runs in the background endlessly until the context is canceled
Expand All @@ -26,7 +28,7 @@ const DefaultServiceTimeout = 15 * time.Second
// it will try to stop the remaining services or timeout if they take too long
func Run(services []Service) {
if len(services) == 0 {
panic("there are no services to run")
logger.Error.Panic("there are no services to run")
}

// start services
Expand All @@ -37,10 +39,10 @@ func Run(services []Service) {
go func() {
if err := service.Start(ctx); err != nil {
msg := "main: service '%v' exited with error: %v\n"
fmt.Printf(msg, service.String(), err)
logger.Error.Printf(msg, service.String(), err)
} else {
msg := "main: service '%v' exited successfully\n"
fmt.Printf(msg, service.String())
logger.Info.Printf(msg, service.String())
}
exit <- struct{}{}
}()
Expand All @@ -62,8 +64,8 @@ func Run(services []Service) {

select {
case <-wait:
fmt.Println("main: all services exited")
logger.Info.Println("main: all services exited")
case <-time.After(DefaultServiceTimeout):
fmt.Println("main: exited after timeout")
logger.Info.Println("main: exited after timeout")
}
}

0 comments on commit fe320ad

Please sign in to comment.