Skip to content
This repository has been archived by the owner on Dec 25, 2024. It is now read-only.

Commit

Permalink
feat(log): added saving logs into file
Browse files Browse the repository at this point in the history
  • Loading branch information
Wittano committed Nov 4, 2023
1 parent 3fca82c commit 2b84841
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ in `/opt/komputer` directory.

Bot is configurable via a few environment variables:

- DISCORD_BOT_TOKEN= # Discord Bot Token (required)
- APPLICATION_ID= # Your bot application id (required)
- SERVER_GUID= # Your server ID, where bot will register commands (required)
- MONGODB_URI= # URI to connect MongoDB database (required)
- MONGODB_DB_NAME= # Name of main collection for komputer bot (required)
- RAPID_API_KEY= # API key for RapidAPIs. In project is use: HumorAP (optional)
- DISCORD_BOT_TOKEN= Discord Bot Token (required)
- APPLICATION_ID= Your bot application id (required)
- SERVER_GUID= Your server ID, where bot will register commands (required)
- MONGODB_URI= URI to connect MongoDB database (required)
- MONGODB_DB_NAME= Name of main collection for komputer bot (required)
- RAPID_API_KEY= API key for RapidAPIs. In project is use: HumorAP (optional)
- LOG_FILE = Localization of your log file (optional). If you don't use this variable, logs will print on STDOUT
1 change: 1 addition & 0 deletions cmd/komputer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func main() {
defer bot.Close()
defer schedule.Scheduler.Stop()
defer mongo.CloseDb()
defer log.FileLog.Close()

stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
Expand Down
24 changes: 22 additions & 2 deletions internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,32 @@ package log

import (
"context"
"fmt"
"github.com/rs/zerolog"
"io"
"os"
)

// TODO Add saving logs into file
var logger = zerolog.New(os.Stdout).With().Ctx(context.Background()).Timestamp().Logger()
var (
FileLog io.WriteCloser = os.Stdout
)

var logger zerolog.Logger

func init() {
logFilePath := os.Getenv("LOG_FILE")
if logFilePath != "" {
var err error

FileLog, err = os.Open(logFilePath)
if err != nil {
Fatal(context.Background(), fmt.Sprintf("Failed to open file '%s'", logFilePath), err)
return
}
}

logger = zerolog.New(FileLog).With().Ctx(context.Background()).Timestamp().Logger()
}

func Info(ctx context.Context, msg string) {
logger.Info().Ctx(ctx).Str("traceID", getTraceID(ctx)).Msg(msg)
Expand Down

0 comments on commit 2b84841

Please sign in to comment.