Skip to content

Commit

Permalink
pass slog/clog logs to tflog (#224)
Browse files Browse the repository at this point in the history
Depends on chainguard-dev/clog#5

Signed-off-by: Jason Hall <[email protected]>
  • Loading branch information
imjasonh authored Feb 6, 2024
1 parent 16376b6 commit 4303850
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ toolchain go1.21.5

require (
chainguard.dev/apko v0.14.0
github.com/chainguard-dev/clog v1.3.1
github.com/chainguard-dev/terraform-provider-oci v0.0.10
github.com/google/go-cmp v0.6.0
github.com/google/go-containerregistry v0.19.0
Expand Down Expand Up @@ -36,7 +37,6 @@ require (
github.com/armon/go-radix v1.0.0 // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/chainguard-dev/clog v1.3.0 // indirect
github.com/chainguard-dev/go-apk v0.0.0-20240130195846-91a06ffe6715 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyY
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chainguard-dev/clog v1.3.0 h1:L/ey0VNH958YpzQa5OO2e2q+iOENxtLAhqkmgzh03e0=
github.com/chainguard-dev/clog v1.3.0/go.mod h1:cV516KZWqYc/phZsCNwF36u/KMGS+Gj5Uqeb8Hlp95Y=
github.com/chainguard-dev/clog v1.3.1 h1:CDNCty5WKQhJzoOPubk0GdXt+bPQyargmfClqebrpaQ=
github.com/chainguard-dev/clog v1.3.1/go.mod h1:cV516KZWqYc/phZsCNwF36u/KMGS+Gj5Uqeb8Hlp95Y=
github.com/chainguard-dev/go-apk v0.0.0-20240130195846-91a06ffe6715 h1:riuOFg3Ay1Js10GQtCAsCL2Hp2DJweUlYjKaxXteYV8=
github.com/chainguard-dev/go-apk v0.0.0-20240130195846-91a06ffe6715/go.mod h1:OdsmvVJb8RNVcTVQ7x07L319LLeiRaRnnsmj8qBBgb4=
github.com/chainguard-dev/terraform-provider-oci v0.0.10 h1:DNt2ZMcl/pCCctbc67a4GhTKu9f4KU5hxipvIq1pBs4=
Expand Down
5 changes: 5 additions & 0 deletions internal/provider/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
"log/slog"
"os"
"strings"
"sync"
Expand All @@ -14,6 +15,7 @@ import (
"chainguard.dev/apko/pkg/build/types"
"chainguard.dev/apko/pkg/options"
"chainguard.dev/apko/pkg/tarfs"
"github.com/chainguard-dev/clog"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/hashicorp/terraform-plugin-log/tflog"
coci "github.com/sigstore/cosign/v2/pkg/oci"
Expand Down Expand Up @@ -114,6 +116,9 @@ func doBuild(ctx context.Context, data BuildResourceModel) (v1.Hash, coci.Signed
for _, arch := range ic2.Archs {
arch := arch

log := clog.New(slog.Default().Handler()).With("arch", arch.ToAPK())
ctx := clog.WithLogger(ctx, log)

errg.Go(func() error {
bc, err := build.New(ctx, tarfs.New(),
build.WithImageConfiguration(*ic2),
Expand Down
50 changes: 50 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"context"
"flag"
"log"
"log/slog"

"github.com/chainguard-dev/terraform-provider-apko/internal/provider"
"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-log/tflog"
)

//go:generate terraform fmt -recursive ./examples/
Expand All @@ -28,3 +30,51 @@ func main() {
log.Fatal(err.Error())
}
}

func init() {
slog.SetDefault(slog.New(tfhandler{}))
}

type tfhandler struct{ attrs []slog.Attr }

const subsystem = "apko"

func (h tfhandler) Handle(ctx context.Context, r slog.Record) error {
// This is a bit of a hack, but it's the only way to get the correct
// source location for the log message.
//
// This creates a new tflog subsystem for logging, with the location
// offset set to 3, which is the number of frames between this function
// and the actual logging call site. Then we use this subsystem below to log
// the message to TF's logger.
ctx = tflog.NewSubsystem(ctx, subsystem, tflog.WithAdditionalLocationOffset(3))

addl := make(map[string]interface{})
r.Attrs(func(s slog.Attr) bool {
addl[s.Key] = s.Value.String()
return true
})
for _, a := range h.attrs {
addl[a.Key] = a.Value.String()
}

switch r.Level {
case slog.LevelDebug:
tflog.SubsystemDebug(ctx, subsystem, r.Message, addl)
case slog.LevelInfo:
tflog.SubsystemInfo(ctx, subsystem, r.Message, addl)
case slog.LevelWarn:
tflog.SubsystemWarn(ctx, subsystem, r.Message, addl)
case slog.LevelError:
tflog.SubsystemError(ctx, subsystem, r.Message, addl)
default:
tflog.SubsystemInfo(ctx, subsystem, r.Message, addl)
}
return nil
}

func (_ tfhandler) Enabled(context.Context, slog.Level) bool { return true }
func (h tfhandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return tfhandler{attrs: append(h.attrs, attrs...)}
}
func (_ tfhandler) WithGroup(name string) slog.Handler { panic("unimplemented") }

0 comments on commit 4303850

Please sign in to comment.