Skip to content

Commit

Permalink
Logging improvements: write to console and file
Browse files Browse the repository at this point in the history
  • Loading branch information
drichelson committed May 26, 2024
1 parent 51b1da9 commit ca80f68
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ temp/
# example: *tfplan*

# Ignore CLI configuration files!/temp/


dorkly.log
4 changes: 2 additions & 2 deletions cmd/dorkly/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func main() {
ctx := context.Background()
dorklyYamlInputPath := os.Getenv(dorklyYamlEnvVar)
if dorklyYamlInputPath == "" {
logger.Infof("Env var [%s] not set. Using default: %s", dorklyYamlEnvVar, defaultDorklyYamlInputPath)
logger.Debugf("Env var [%s] not set. Using default: %s", dorklyYamlEnvVar, defaultDorklyYamlInputPath)
dorklyYamlInputPath = defaultDorklyYamlInputPath
}

Expand Down Expand Up @@ -68,5 +68,5 @@ func logAwsCallerIdentity(awsConfig aws.Config, ctx context.Context) {
if err != nil {
logger.Fatal(err)
}
logger.Infof("AWS Identity: %v", string(jsonBytes))
logger.Debugf("AWS Identity: %v", string(jsonBytes))
}
26 changes: 24 additions & 2 deletions internal/dorkly/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,41 @@ package dorkly
import (
"fmt"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
)

var (
logger = newLogger()
)

func newLogger() *zap.SugaredLogger {
logger, err := zap.NewDevelopment()
fileLoggerConfig := zap.NewDevelopmentConfig()
fileLoggerConfig.OutputPaths = []string{"dorkly.log"}
fileLogger, err := fileLoggerConfig.Build()
if err != nil {
fmt.Println("failed to create logger: ", err)
panic(err)
}
return logger.Sugar()

consoleLoggerEncoderConfig := zapcore.EncoderConfig{
TimeKey: "",
LevelKey: "",
NameKey: "",
CallerKey: "",
FunctionKey: zapcore.OmitKey,
MessageKey: "msg",
StacktraceKey: "",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.EpochTimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
consoleLogger := zapcore.NewCore(zapcore.NewConsoleEncoder(consoleLoggerEncoderConfig), zapcore.AddSync(os.Stdout), zapcore.InfoLevel)

tee := zapcore.NewTee(fileLogger.Core(), consoleLogger)
return zap.New(tee).Sugar()
}

func GetLogger() *zap.SugaredLogger {
Expand Down
6 changes: 5 additions & 1 deletion internal/dorkly/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (r *Reconciler) Reconcile(ctx context.Context) error {
}

var reconciledArchive RelayArchive
err = runStep("Reconcile existing archive and local yaml project files into reconciled archive", func() error {
err = runStep("Merge existing archive and local yaml project files into reconciled archive", func() error {
var err error
reconciledArchive, err = reconcile(*existingArchive, *newArchive)
return err
Expand Down Expand Up @@ -155,8 +155,12 @@ func reconcile(old, new RelayArchive) (RelayArchive, error) {

// runStep utilizes GitHub Actions' log grouping feature:
// https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#grouping-log-lines
// Ideally these would be collapsed by default, but that's not possible with the current implementation:
// https://www.google.com/search?q=github+grouping+log+lines+collapse+by+default
func runStep(step string, f func() error) error {
//fmt.Printf("\n[%s] BEGIN\n", step)
// For now we use the logger because it is the only way to guarantee that the log lines are grouped:
// fmt.Printf() produces out of order log lines causing malformed groups.
logger.Infof("\n::group::%s", step)
logger.Infof("Starting step: %s", step)
err := f()
Expand Down

0 comments on commit ca80f68

Please sign in to comment.