Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
40 changes: 40 additions & 0 deletions .github/workflows/pr-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
licenses: ${{ github.event_name != 'pull_request' || steps.filter.outputs.full == 'true' || steps.filter.outputs.licenses == 'true' }}
gh_credentials: ${{ github.event_name != 'pull_request' || steps.filter.outputs.full == 'true' || steps.filter.outputs.gh_credentials == 'true' }}
provider_update: ${{ github.event_name != 'pull_request' || steps.filter.outputs.full == 'true' || steps.filter.outputs.provider_update == 'true' }}
shared_logging: ${{ github.event_name != 'pull_request' || steps.filter.outputs.shared_logging == 'true' }}
mode: ${{ github.event_name == 'pull_request' && 'pr' || 'full' }}
changed_files: ${{ steps.filter.outputs.all_files || '[]' }}
steps:
Expand Down Expand Up @@ -62,6 +63,10 @@ jobs:
- 'hack/licenses/**'
- 'hack/podman_ci/**'
- '.golangci.yaml'
shared_logging:
- 'logging/**'
- '.github/workflows/pr-ci.yml'
- '.golangci.yaml'
agent_skill:
- '.agents/skills/devsy/**'
- 'hack/validate_agent_skill/**'
Expand Down Expand Up @@ -171,6 +176,40 @@ jobs:
- name: Validate skill and installer compatibility
run: go run ./hack/validate_agent_skill

shared-logging:
name: Shared Logging
needs: [changes]
if: needs.changes.outputs.shared_logging == 'true'
runs-on: ubuntu-latest
timeout-minutes: 15
permissions:
contents: read
defaults:
run:
working-directory: logging
env:
GOWORK: "off"
GOTOOLCHAIN: local
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1
- uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e
with:
go-version: 1.23.x
cache-dependency-path: logging/go.mod
- name: Test Go floor
run: go test -race ./... && go vet ./... && test -z "$(gofmt -l .)"
- uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e
with:
go-version-file: go.mod
cache-dependency-path: logging/go.mod
- uses: golangci/golangci-lint-action@ba0d7d2ec06a0ea1cb5fa41b2e4a3ab91d21278a
with:
version: v2.14.0
working-directory: logging
args: --config ../.golangci.yaml
- name: Check Go formatting
run: golangci-lint fmt --diff --config ../.golangci.yaml

lint:
name: Lint
needs: [changes]
Expand Down Expand Up @@ -979,6 +1018,7 @@ jobs:
- plan
- precommit
- lint
- shared-logging
- agent-skill
- unit-tests
- github-credential-host-tests
Expand Down
2 changes: 2 additions & 0 deletions hack/ci/success.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ func domainOutputForJob(job string) (string, bool) {
return "gh_credentials", true
case "provider-update-windows":
return "provider_update", true
case "shared-logging":
return "shared_logging", true
default:
return "", false
}
Expand Down
7 changes: 7 additions & 0 deletions hack/ci/success_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ func (s *successSuite) TestPerDomainSkipsRequireExactFalseOutput() {
{job: "licenses", field: "licenses"},
{job: "github-credential-host-tests", field: "gh_credentials"},
{job: "provider-update-windows", field: "provider_update"},
{job: "shared-logging", field: "shared_logging"},
}
for _, tt := range tests {
s.Run(tt.job, func() {
Expand All @@ -281,6 +282,12 @@ func (s *successSuite) TestPerDomainSkipsRequireExactFalseOutput() {
}
})
}
s.Run("shared-logging missing output", func() {
needs := needsObject(entry("changes", goTrue), entry("shared-logging", skippedJob))
res := s.runSuccess(needs)
s.Equal(1, res.code)
s.Contains(res.stdout, "::error::shared-logging ended with result=skipped unexpectedly\n")
})
}

func (s *successSuite) TestMatrixSkipsUseTheirOwnPlanOutput() {
Expand Down
44 changes: 44 additions & 0 deletions logging/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Shared logging

This module provides structured logging and credential masking for Go producers. It uses only the standard library and supports Go 1.23. It can be used independently of the Core and Runtime SDK modules. Use an approved merged commit or published submodule version when adding a dependency. Do not depend on an unpublished local checkout.

## Configure a producer

Inject the diagnostic writer, environment, and known secret values. The caller owns configuration errors, warnings, output streams, and logger lifetime.

```go
import (
"errors"
"io"
"log/slog"

"github.com/devsy-org/devsy/logging"
"github.com/devsy-org/devsy/logging/redact"
)

func newLogger(out io.Writer, level, format string, environment, secrets []string) (*slog.Logger, error) {
config, invalid := logging.ResolveProvider(level, format)
if invalid.Level || invalid.Format {
return nil, errors.New("invalid logging configuration")
}
redactor := redact.New(redact.Options{Environment: environment, Values: secrets})
handler := logging.NewHandler(out, logging.HandlerOptions{
Level: config.Level,
Format: config.Format,
Redactor: redactor,
})
return slog.New(handler), nil
}
```

`ResolveProvider` returns info and JSON for empty or invalid settings and reports invalid fields separately. The caller decides whether to reject invalid settings or warn and use the fallback. Values are case insensitive and are not trimmed. Accepted formats are `auto`, `json`, `text`, `plain`, and `logfmt`. `auto` resolves to JSON and `plain` selects text. The caller chooses any terminal-specific format or presentation before constructing the handler.

JSON records use `ts`, `level`, and `msg` with structured attributes. The handler writes only to the supplied writer. Send diagnostics to stderr or another dedicated sink. Keep stdout and transported command bytes reserved for their protocols. The handler does not configure process globals or terminate the process for fatal severity.

## Protect diagnostic data

The redactor masks supplied secret values, values from credential-like environment names, URL userinfo, and Authorization header credentials. It also processes diagnostic messages, keys, groups, errors, and nested values. Supply secrets the environment policy cannot identify. A nil handler redactor still protects recognized credential formats. Redaction cannot discover every secret. Avoid logging sensitive input and retain host-side protection where needed.

`redact.NewStream` supports chunked diagnostic streams. Call `RedactChunk` for each chunk and `Flush` at the end. An unresolved generic URL authority has a 512-byte lookahead including its scheme. If it remains ambiguous beyond that limit, the stream conservatively masks the authority and suppresses it until `@`, a slash, `?`, `#`, or whitespace. This can mask an overlong ordinary authority split across calls. A complete ordinary URL remains unchanged when its delimiter makes it unambiguous in the supplied input.

Known secrets across chunks use an append-backed pending buffer. Long unresolved match churn can defer safe output until later input or `Flush`. Complete unambiguous diagnostics are emitted immediately. There is no wall-clock emission bound. After each `RedactChunk`, pending byte length is bounded by `max(N + floor(N/2), max(N, 512) + 9)`, where `N` is the longest possible proper prefix of a known secret, or zero if there are none. Do not use this stream to rewrite protocol or raw business output.
113 changes: 113 additions & 0 deletions logging/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package logging

import (
"log/slog"
"strings"
)

const (
LevelTrace slog.Level = -8
LevelFatal slog.Level = 12
)

const (
levelTraceName = "trace"
levelDebugName = "debug"
levelInfoName = "info"
levelWarnName = "warn"
levelErrorName = "error"
)

type Format string

const (
FormatAuto Format = "auto"
FormatJSON Format = "json"
FormatText Format = "text"
FormatLogfmt Format = "logfmt"
)

type ProviderConfig struct {
Level slog.Level
Format Format
}

type InvalidConfig struct {
Level bool
Format bool
}

func ParseLevel(value string) (slog.Level, bool) {
switch value {
case levelTraceName:
return LevelTrace, true
case levelDebugName:
return slog.LevelDebug, true
case levelInfoName:
return slog.LevelInfo, true
case levelWarnName:
return slog.LevelWarn, true
case levelErrorName:
return slog.LevelError, true
default:
return slog.LevelInfo, false
}
}

func LevelNames() []string {
return []string{levelErrorName, levelWarnName, levelInfoName, levelDebugName, levelTraceName}
}

func LevelName(level slog.Level) string {
switch {
case level < slog.LevelDebug:
return levelTraceName
case level < slog.LevelInfo:
return levelDebugName
case level < slog.LevelWarn:
return levelInfoName
case level < slog.LevelError:
return levelWarnName
case level < LevelFatal:
return levelErrorName
default:
return "fatal"
}
}

func ParseFormat(value string) (Format, bool) {
switch Format(value) {
case FormatAuto, FormatJSON, FormatText, FormatLogfmt:
return Format(value), true
case "plain":
return FormatText, true
default:
return "", false
}
}

func ResolveProvider(level, format string) (ProviderConfig, InvalidConfig) {
level = strings.ToLower(level)
format = strings.ToLower(format)
if level == "" {
level = levelInfoName
}
parsedLevel, levelOK := ParseLevel(level)
parsedFormat, formatOK := resolveProviderFormat(format)
return ProviderConfig{Level: parsedLevel, Format: parsedFormat},
InvalidConfig{Level: !levelOK, Format: !formatOK}
}

func resolveProviderFormat(value string) (Format, bool) {
if value == "" {
return FormatJSON, true
}
format, ok := ParseFormat(value)
if !ok {
return FormatJSON, false
}
if format == FormatAuto {
return FormatJSON, true
}
return format, true
}
3 changes: 3 additions & 0 deletions logging/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/devsy-org/devsy/logging

go 1.23.0
Loading
Loading