Skip to content

Commit

Permalink
feat: implement default logging to file with fallback to console
Browse files Browse the repository at this point in the history
  • Loading branch information
simlarsen committed Jan 3, 2025
1 parent d755c62 commit a0c0041
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions InfrastructureAgent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ type Agent struct {
}

func NewAgent(secretKey string, url string) *Agent {

ag := &Agent{
SecretKey: secretKey,
OneUptimeURL: url,
}

slog.Info("Starting agent...")
slog.Info("Agent configuration:")
slog.Info("Secret key: " + ag.SecretKey)
Expand Down
18 changes: 18 additions & 0 deletions InfrastructureAgent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ func (p *program) Start(s service.Service) error {
return nil
}

func SetDefaultLogger() {
logFile, err := os.OpenFile("output.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)

if err != nil {
slog.Default().Error("Failed to open log file", "error", err)
// If we can't open the log file, we'll log to the console instead
logFile = os.Stdout
}

//defer logFile.Close()

logger := slog.New(slog.NewTextHandler(logFile, nil))
slog.SetDefault(logger)
}

func (p *program) run() {
p.agent = NewAgent(p.config.SecretKey, p.config.OneUptimeURL)
p.agent.Start()
Expand Down Expand Up @@ -58,6 +73,9 @@ func (p *program) Stop(s service.Service) error {
}

func main() {

SetDefaultLogger()
slog.Info("Starting OneUptime Infrastructure Agent")
// Set up the configuration
config.WithOptions(config.WithTagName("json"))
cfg := newConfigFile()
Expand Down

0 comments on commit a0c0041

Please sign in to comment.