-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added version and logging levels
- Loading branch information
1 parent
5c1df6a
commit 88f20d4
Showing
3 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2024 Humanitec | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package logging | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"log/slog" | ||
"sync" | ||
) | ||
|
||
type SimpleHandler struct { | ||
Writer io.Writer | ||
Level slog.Leveler | ||
|
||
mu sync.Mutex | ||
} | ||
|
||
func (h *SimpleHandler) Enabled(ctx context.Context, level slog.Level) bool { | ||
return level >= h.Level.Level() | ||
} | ||
|
||
func (h *SimpleHandler) Handle(ctx context.Context, record slog.Record) error { | ||
h.mu.Lock() | ||
defer h.mu.Unlock() | ||
_, err := h.Writer.Write([]byte(fmt.Sprintf("%s: %s\n", record.Level.String(), record.Message))) | ||
return err | ||
} | ||
|
||
func (h *SimpleHandler) WithAttrs(attrs []slog.Attr) slog.Handler { | ||
// no support for attrs here | ||
return h | ||
} | ||
|
||
func (h *SimpleHandler) WithGroup(name string) slog.Handler { | ||
// no support for attrs here | ||
return h | ||
} | ||
|
||
var _ slog.Handler = (*SimpleHandler)(nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2024 Humanitec | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package version | ||
|
||
import ( | ||
"fmt" | ||
"runtime/debug" | ||
) | ||
|
||
var ( | ||
Version string = "0.0.0" | ||
) | ||
|
||
// BuildVersionString constructs a version string by looking at the build metadata injected at build time. | ||
// This is particularly useful when score-compose is stilled from the go module using go install. | ||
func BuildVersionString() string { | ||
versionNumber, buildTime, gitSha, isDirtySuffix := Version, "local", "unknown", "" | ||
if info, ok := debug.ReadBuildInfo(); ok { | ||
if info.Main.Version != "" && info.Main.Version != "(devel)" { | ||
versionNumber = info.Main.Version | ||
} | ||
for _, setting := range info.Settings { | ||
switch setting.Key { | ||
case "vcs.time": | ||
buildTime = setting.Value | ||
case "vcs.revision": | ||
gitSha = setting.Value | ||
case "vcs.modified": | ||
if setting.Value == "true" { | ||
isDirtySuffix = "-dirty" | ||
} | ||
} | ||
} | ||
} | ||
return fmt.Sprintf("%s (build: %s, sha: %s%s)", versionNumber, buildTime, gitSha, isDirtySuffix) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters