Skip to content

Commit ceb54f1

Browse files
feat: add logging (#3)
1 parent c57e8f9 commit ceb54f1

File tree

9 files changed

+48
-2
lines changed

9 files changed

+48
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Quickly get started with a minimal Golang starter template!
66

77
## Features
88

9-
- Bare Golang application
9+
- Bare Golang application with logging ( pointing to the standard output )
1010
- Release management using [GoReleaser](https://goreleaser.com/)
1111
- Github container registry for [Docker](https://www.docker.com/)
1212
- Updates using [Dependabot](https://github.com/dependabot)

logging/debug.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package logging
2+
3+
func Debug(message string, args ...any) {
4+
logger.Debug(message, args...)
5+
}

logging/documentation.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Package logging provides logging functionalities.
2+
package logging

logging/error.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package logging
2+
3+
func Error(message string, args ...any) {
4+
logger.Error(message, args...)
5+
}

logging/info.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package logging
2+
3+
func Info(message string, args ...any) {
4+
logger.Info(message, args...)
5+
}

logging/logging.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package logging
2+
3+
import (
4+
"log/slog"
5+
"os"
6+
)
7+
8+
var (
9+
logger *slog.Logger
10+
logLevel = new(slog.LevelVar)
11+
)
12+
13+
func init() {
14+
logHandler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: logLevel})
15+
logger = slog.New(logHandler)
16+
}

logging/set_level.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package logging
2+
3+
import "log/slog"
4+
5+
func SetLevel(level slog.Level) {
6+
logLevel.Set(level)
7+
}

logging/warn.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package logging
2+
3+
func Warn(message string, args ...any) {
4+
logger.Warn(message, args...)
5+
}

main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import (
44
"fmt"
55

66
"github.com/matthiashermsen/go-starter/app"
7+
"github.com/matthiashermsen/go-starter/logging"
78
)
89

910
func main() {
10-
fmt.Printf("Current app version is: %s", app.Version)
11+
logging.Info(fmt.Sprintf("Running on version %s", app.Version))
1112
}

0 commit comments

Comments
 (0)