Skip to content

Commit

Permalink
feat(logger): add colored outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
bastean committed Jul 22, 2024
1 parent 6d58885 commit e0697ef
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 40 deletions.
28 changes: 16 additions & 12 deletions cmd/dsgo/ds.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ func usage() {
}

func main() {
log.Info("dsGO")

flag.StringVar(&Port, "port", env.Server.Fiber.Port, "Fiber Server Port")

flag.StringVar(&AppId, "app", env.Bot.Discord.AppId, "Discord App Id Token")
Expand All @@ -48,9 +46,11 @@ func main() {

flag.Parse()

log.Logo()

log.Starting(Services)

if err := service.Run(); err != nil {
if err := service.Up(); err != nil {
log.Fatal(err.Error())
}

Expand All @@ -59,13 +59,13 @@ func main() {
log.Starting(Apps)

go func() {
if err := server.Run(Port); err != nil {
if err := server.Up(Port); err != nil {
log.Fatal(err.Error())
}
}()

go func() {
if err := bot.Run(AppId, BotToken, TestGuildId); err != nil {
if err := bot.Up(AppId, BotToken, TestGuildId); err != nil {
log.Fatal(err.Error())
}
}()
Expand All @@ -86,22 +86,26 @@ func main() {

log.Stopping(Apps)

errServer := server.Stop(ctx)
errServer := server.Down(ctx)

errBot := bot.Down()

errBot := bot.Stop()
if err := errors.Join(errServer, errBot); err != nil {
log.Error(err.Error())
}

log.Stopped(Apps)

log.Stopping(Services)

errService := service.Stop()
errService := service.Down()

log.Stopped(Services)

if err := errors.Join(errServer, errBot, errService); err != nil {
log.Error(err.Error())
if errService != nil {
log.Error(errService.Error())
}

log.Stopped(Services)

<-ctx.Done()

log.Info("exiting...")
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/brianvoe/gofakeit/v7 v7.0.4
github.com/bwmarrin/discordgo v0.28.1
github.com/cucumber/godog v0.14.1
github.com/fatih/color v1.17.0
github.com/go-playground/validator/v10 v10.22.0
github.com/gofiber/fiber/v2 v2.52.5
github.com/playwright-community/playwright-go v0.4501.1
Expand All @@ -19,6 +20,7 @@ require (
require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be // indirect
github.com/cucumber/gherkin/go/v26 v26.2.0 // indirect
github.com/cucumber/messages/go/v21 v21.0.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/brianvoe/gofakeit/v7 v7.0.4 h1:Mkxwz9jYg8Ad8NvT9HA27pCMZGFQo08MK6jD0Q
github.com/brianvoe/gofakeit/v7 v7.0.4/go.mod h1:QXuPeBw164PJCzCUZVmgpgHJ3Llj49jSLVkKPMtxtxA=
github.com/bwmarrin/discordgo v0.28.1 h1:gXsuo2GBO7NbR6uqmrrBDplPUx2T3nzu775q/Rd1aG4=
github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be h1:J5BL2kskAlV9ckgEsNQXscjIaLiOYiZ75d4e94E6dcQ=
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be/go.mod h1:mk5IQ+Y0ZeO87b858TlA645sVcEcbiX6YqP98kt+7+w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/cucumber/gherkin/go/v26 v26.2.0 h1:EgIjePLWiPeslwIWmNQ3XHcypPsWAHoMCz/YEBKP4GI=
Expand All @@ -22,6 +24,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deckarep/golang-set/v2 v2.6.0 h1:XfcQbWM1LlMB8BsJ8N9vW5ehnnPVIw0je80NsVHagjM=
github.com/deckarep/golang-set/v2 v2.6.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/gabriel-vasile/mimetype v1.4.4 h1:QjV6pZ7/XZ7ryI2KuyeEDE8wnh7fHP9YnQy+R0LnH8I=
github.com/gabriel-vasile/mimetype v1.4.4/go.mod h1:JwLei5XPtWdGiMFB5Pjle1oEeoSeEuJfJE+TtfvdB/s=
github.com/go-jose/go-jose/v3 v3.0.3 h1:fFKWeig/irsp7XD2zBxvnmA/XaRWp5V3CBsZXJF7G7k=
Expand Down
16 changes: 10 additions & 6 deletions internal/app/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,41 @@ var (
Session *discordgo.Session
)

func Run(app, token, guild string) error {
func Up(app, token, guild string) error {
log.Starting(Bot.Discord)

Session, err = discordgo.New("Bot " + token)

if err != nil {
return errors.BubbleUp(err, "Run")
log.CannotBeStarted(Bot.Discord)
return errors.BubbleUp(err, "Up")
}

_, err = Session.ApplicationCommandBulkOverwrite(app, guild, command.Commands)

if err != nil {
return errors.BubbleUp(err, "Run")
log.CannotBeStarted(Bot.Discord)
return errors.BubbleUp(err, "Up")
}

handler.Events(Session)

if err := Session.Open(); err != nil {
return errors.BubbleUp(err, "Run")
log.ConnectionFailedWith(Bot.Discord)
return errors.BubbleUp(err, "Up")
}

log.Started(Bot.Discord)

return nil
}

func Stop() error {
func Down() error {
log.Stopping(Bot.Discord)

if err := Session.Close(); err != nil {
return errors.BubbleUp(err, "Stop")
log.DisconnectionFailedWith(Bot.Discord)
return errors.BubbleUp(err, "Down")
}

log.Stopped(Bot.Discord)
Expand Down
10 changes: 6 additions & 4 deletions internal/app/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var Files embed.FS

var App *fiber.App

func Run(port string) error {
func Up(port string) error {
log.Starting(Server.Fiber)

App = fiber.New(fiber.Config{
Expand All @@ -37,7 +37,8 @@ func Run(port string) error {
router.Routing(App, &Files)

if err := App.Listen(":" + port); err != nil {
return errors.BubbleUp(err, "Run")
log.CannotBeStarted(Server.Fiber)
return errors.BubbleUp(err, "Up")
}

log.Started(Server.Fiber)
Expand All @@ -51,11 +52,12 @@ func Run(port string) error {
return nil
}

func Stop(ctx context.Context) error {
func Down(ctx context.Context) error {
log.Stopping(Server.Fiber)

if err := App.ShutdownWithContext(ctx); err != nil {
return errors.BubbleUp(err, "Stop")
log.CannotBeStopped(Server.Fiber)
return errors.BubbleUp(err, "Down")
}

log.Stopped(Server.Fiber)
Expand Down
81 changes: 68 additions & 13 deletions internal/pkg/service/logger/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,61 @@ package log

import (
"fmt"
"strings"

"github.com/bastean/dsgo/pkg/context/infrastructure/record/log"
"github.com/common-nighthawk/go-figure"
"github.com/fatih/color"
)

var (
Blue = color.New(color.FgBlue, color.Bold).Sprint
Cyan = color.New(color.FgCyan, color.Bold).Sprint
Green = color.New(color.FgGreen, color.Bold).Sprint
Magenta = color.New(color.FgMagenta, color.Bold).Sprint
Red = color.New(color.FgRed, color.Bold).Sprint
)

var (
Log = new(log.Log)
Debug = Log.Debug
Error = Log.Error
Fatal = Log.Fatal
Info = Log.Info
Debug = func(message string) { Log.Debug(Cyan(message)) }
Error = func(message string) { Log.Error(Red(message)) }
Fatal = func(message string) { Log.Fatal(Red(message)) }
Info = func(message string) { Log.Info(Blue(message)) }
)

var (
Ok = func(message string) { Log.Info(Green(message)) }
)

func Logo() {
figureDs := figure.NewFigure("ds", "speed", true).Slicify()
figureGo := figure.NewFigure("GO", "speed", true).Slicify()

width := 0
fixedWidth := 0

for _, line := range figureDs {
width = len(line)

if width > fixedWidth {
fixedWidth = width
}
}

for i, line := range figureDs {
width = len(line)

if width < fixedWidth {
line += strings.Repeat(" ", (fixedWidth - width))
}

fmt.Println(Magenta(line), Cyan(figureGo[i]))
}

fmt.Println()
}

func Service(service string) string {
return fmt.Sprintf("service:%s", service)
}
Expand All @@ -22,46 +65,58 @@ func Module(module string) string {
return fmt.Sprintf("module:%s", module)
}

func Server(server string) string {
return fmt.Sprintf("server:%s", server)
func Server(app string) string {
return fmt.Sprintf("server:%s", app)
}

func Bot(bot string) string {
return fmt.Sprintf("bot:%s", bot)
func Bot(app string) string {
return fmt.Sprintf("bot:%s", app)
}

func Starting(service string) {
Info(fmt.Sprintf("starting %s...", service))
}

func Started(service string) {
Info(fmt.Sprintf("%s started", service))
Ok(fmt.Sprintf("%s started", service))
}

func CannotBeStarted(service string) {
Error(fmt.Sprintf("%s cannot be started", service))
}

func Stopping(service string) {
Info(fmt.Sprintf("stopping %s...", service))
}

func Stopped(service string) {
Info(fmt.Sprintf("%s stopped", service))
Ok(fmt.Sprintf("%s stopped", service))
}

func CannotBeStopped(service string) {
Error(fmt.Sprintf("%s cannot be stopped", service))
}

func EstablishingConnectionWith(service string) {
Info(fmt.Sprintf("establishing connection with %s...", service))
}

func ConnectionEstablishedWith(service string) {
Info(fmt.Sprintf("connection established with %s", service))
Ok(fmt.Sprintf("connection established with %s", service))
}

func ConnectionFailedWith(service string) {
Info(fmt.Sprintf("connection failed with %s", service))
Error(fmt.Sprintf("connection failed with %s", service))
}

func ClosingConnectionWith(service string) {
Info(fmt.Sprintf("closing connection with %s...", service))
}

func ConnectionClosedWith(service string) {
Info(fmt.Sprintf("connection closed with %s", service))
Ok(fmt.Sprintf("connection closed with %s", service))
}

func DisconnectionFailedWith(service string) {
Error(fmt.Sprintf("disconnection failed with %s", service))
}
11 changes: 6 additions & 5 deletions internal/pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func StartModuleUser() error {
return nil
}

func Run() error {
func Up() error {
log.EstablishingConnectionWith(Service.MySQL)

if err = OpenMySQL(); err != nil {
Expand All @@ -77,7 +77,7 @@ func Run() error {
log.EstablishingConnectionWith(Service.SQLite)

if err = OpenSQLite(); err != nil {
return errors.BubbleUp(err, "Run")
return errors.BubbleUp(err, "Up")
}

log.ConnectionEstablishedWith(Service.SQLite)
Expand All @@ -93,7 +93,7 @@ func Run() error {
err = StartModuleUser()

if err != nil {
return errors.BubbleUp(err, "Run")
return errors.BubbleUp(err, "Up")
}

log.Started(Module.User)
Expand All @@ -115,9 +115,10 @@ func CloseDatabase() error {
return nil
}

func Stop() error {
func Down() error {
if err := CloseDatabase(); err != nil {
return errors.BubbleUp(err, "Stop")
log.DisconnectionFailedWith(Service.Database)
return errors.BubbleUp(err, "Down")
}

return nil
Expand Down

0 comments on commit e0697ef

Please sign in to comment.