Add logging

This commit is contained in:
Kasper Juul Hermansen 2022-09-10 00:09:09 +02:00
parent 8ceb4452f9
commit af142c4b09
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
9 changed files with 170 additions and 0 deletions

0
LICENSE Normal file
View File

View File

@ -0,0 +1,22 @@
package commands
import "github.com/spf13/cobra"
func CreateKrakenCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "krakenserver",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}
cmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
return cmd
}

18
cmd/kraken/kraken.go Normal file
View File

@ -0,0 +1,18 @@
package main
import (
"os"
"git.front.kjuulh.io/kjuulh/kraken/cmd/server/commands"
)
func main() {
Execute()
}
func Execute() {
err := commands.CreateServerCmd().Execute()
if err != nil {
os.Exit(1)
}
}

View File

@ -0,0 +1,16 @@
package commands
import (
"github.com/spf13/cobra"
"go.uber.org/zap"
)
func CreateServerCmd(logger *zap.Logger) *cobra.Command {
cmd := &cobra.Command{
Use: "krakenserver",
}
cmd.AddCommand(NewStartServerCommand(logger))
return cmd
}

View File

@ -0,0 +1,21 @@
package commands
import (
"errors"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
func NewStartServerCommand(logger *zap.Logger) *cobra.Command {
cmd := &cobra.Command{
Use: "start",
Short: "Start the kraken server",
RunE: func(cmd *cobra.Command, args []string) error {
return errors.New("some error")
},
}
return cmd
}

27
cmd/server/server.go Normal file
View File

@ -0,0 +1,27 @@
package main
import (
"os"
"git.front.kjuulh.io/kjuulh/kraken/cmd/server/commands"
"git.front.kjuulh.io/kjuulh/kraken/internal/logger"
"go.uber.org/zap"
)
func main() {
logger, err := logger.New()
if err != nil {
panic(err)
}
_ = logger.Sync()
Execute(logger)
}
func Execute(logger *zap.Logger) {
err := commands.CreateServerCmd(logger).Execute()
if err != nil {
logger.Error("execution failed", zap.Error(err))
os.Exit(1)
}
}

9
go.mod
View File

@ -1,3 +1,12 @@
module git.front.kjuulh.io/kjuulh/kraken module git.front.kjuulh.io/kjuulh/kraken
go 1.19 go 1.19
require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/spf13/cobra v1.5.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.23.0 // indirect
)

25
go.sum Normal file
View File

@ -0,0 +1,25 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU=
github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8=
go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
go.uber.org/zap v1.23.0 h1:OjGQ5KQDEUawVHxNwQgPpiypGHOxo2mNZsOqTak4fFY=
go.uber.org/zap v1.23.0/go.mod h1:D+nX8jyLsMHMYrln8A0rJjFt/T/9/bGgIhAqxv5URuY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

32
internal/logger/zap.go Normal file
View File

@ -0,0 +1,32 @@
package logger
import (
"os"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func New() (*zap.Logger, error) {
highPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= zapcore.ErrorLevel
})
lowPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl < zapcore.ErrorLevel
})
config := zap.NewDevelopmentEncoderConfig()
config.EncodeTime = nil
config.EncodeLevel = zapcore.CapitalColorLevelEncoder
consoleEncoder := zapcore.NewConsoleEncoder(config)
consoleDebugging := zapcore.Lock(os.Stdout)
consoleErrors := zapcore.Lock(os.Stderr)
core := zapcore.NewTee(
zapcore.NewCore(consoleEncoder, consoleErrors, highPriority),
zapcore.NewCore(consoleEncoder, consoleDebugging, lowPriority),
)
logger := zap.New(core)
return logger, nil
}