Skip to content

Commit a31904a

Browse files
redpinecubeblobcode
authored andcommitted
implemented multilogger
Signed-off-by: redpinecube <tara.chakkithara@icloud.com>
1 parent f51ff21 commit a31904a

10 files changed

Lines changed: 700 additions & 146 deletions

File tree

config/log.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ output:
44
level: INFO
55
- type: stdout
66
level: INFO
7-
directory: ./logs/
7+
directory: ../logs/
88

99
components:
1010
- app

src/api.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"log/slog"
9+
log2 "mist/multilogger"
910
"net/http"
1011
"os"
1112
"os/signal"
@@ -114,7 +115,11 @@ func (a *App) Shutdown(ctx context.Context) error {
114115
}
115116

116117
func main() {
117-
log, err := createLogger("app")
118+
cfg, err := log2.GetLogConfig()
119+
if err != nil {
120+
fmt.Fprintf(os.Stderr, "failed to get log config: %v\n", err)
121+
}
122+
log, err := log2.CreateLogger("app", &cfg)
118123
if err != nil {
119124
fmt.Fprintf(os.Stderr, "failed to create logger: %v\n", err)
120125
os.Exit(1)

src/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ require github.com/redis/go-redis/v9 v9.10.0
77
require (
88
github.com/cespare/xxhash/v2 v2.3.0 // indirect
99
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
10+
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
1011
gopkg.in/yaml.v3 v3.0.1 // indirect
1112
)

src/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cu
99
github.com/redis/go-redis/v9 v9.10.0 h1:FxwK3eV8p/CQa0Ch276C7u2d0eNC9kCmAYQ7mCXCzVs=
1010
github.com/redis/go-redis/v9 v9.10.0/go.mod h1:huWgSWd8mW6+m0VPhJjSSQ+d6Nh1VICQ6Q5lHuCH/Iw=
1111
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
12+
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
13+
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
1214
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
1315
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

src/int_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package main
33
import (
44
"context"
55
"fmt"
6+
"log/slog"
7+
"mist/multilogger"
68
"os"
79
"os/signal"
810
"sync"
@@ -54,7 +56,8 @@ func TestIntegration(t *testing.T) {
5456
defer os.Unsetenv("ENV")
5557

5658
redisAddr := "localhost:6379"
57-
schedulerLog, err := createLogger("scheduler")
59+
config, _ := multilogger.GetLogConfig()
60+
schedulerLog, err := multilogger.CreateLogger("scheduler", &config)
5861
if err != nil {
5962
fmt.Fprintf(os.Stderr, "failed to create logger: %v\n", err)
6063
os.Exit(1)
@@ -68,7 +71,7 @@ func TestIntegration(t *testing.T) {
6871
scheduler := NewScheduler(redisAddr, schedulerLog)
6972
defer scheduler.Close()
7073

71-
supervisorLog, err := createLogger("supervisor")
74+
supervisorLog, err := multilogger.CreateLogger("supervisor", &config)
7275
if err != nil {
7376
fmt.Fprintf(os.Stderr, "failed to create logger: %v\n", err)
7477
os.Exit(1)

src/log.go

Lines changed: 0 additions & 141 deletions
This file was deleted.

src/log_test.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/multilogger/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# multilogger
2+
3+
`multilogger` lets you log to multiple io.Writer instances at once, with JSON structured logs, file rotation, and flexible metadata.
4+
5+
---
6+
7+
## Overview
8+
9+
10+
Multilogger allows you to write logs to multiple io.Writer targets (e.g. stdout, files, etc.) simultaneously, each with its own log level.
11+
It’s ideal for tracking runtime activity across components such as schedulers, supervisors, and the main application.
12+
If the configuration fails or a destination cannot be initialized, multilogger automatically falls back to a stderr logger.
13+
File rotation is handled using [lumberjack](https://github.com/natefinch/lumberjack). Environment variables are able to override the log levels configured in the log.yaml file.
14+
15+
---
16+
17+
## Example Usage
18+
```go
19+
// Create Multilogger
20+
config := multilogger.GetLogConfig()
21+
logger, _ := multilogger.CreateLogger("app", &config)
22+
logger.Info("starting app")
23+
```
24+
25+
```go
26+
// Set Global Log Level
27+
os.Setenv("LOG_LEVEL", "DEBUG")
28+
```
29+
30+
```go
31+
// Configure Log Levels By io.Writer
32+
os.Setenv("FILE_LOG_LEVEL", "DEBUG")
33+
os.Setenv("STDOUT_LOG_LEVEL", "INFO")
34+
```
35+
36+
```go
37+
// Add Metadata
38+
config := multilogger.GetLogConfig()
39+
logger, _ := multilogger.CreateLogger("app", &config)
40+
logger = logger.WithGroup("jobInfo")
41+
logger.Info("job started", "job_id", "abc123")
42+
```
43+
44+
---

0 commit comments

Comments
 (0)