Skip to content
This repository was archived by the owner on May 11, 2025. It is now read-only.

Commit d446adc

Browse files
committed
update colors
1 parent b77ddf5 commit d446adc

3 files changed

Lines changed: 29 additions & 6 deletions

File tree

cache.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,29 @@ import (
44
"context"
55
"fmt"
66
"github.com/go-redis/redis/v8"
7+
"github.com/kataras/iris/v12"
78
)
89

9-
var client *redis.Client
10+
var cache *redis.Client
1011

1112
func SetupCache() {
12-
client = redis.NewClient(&redis.Options{
13+
cache = redis.NewClient(&redis.Options{
1314
Addr: fmt.Sprintf("%s:%d", conf.Redis.Host, conf.Redis.Port),
1415
Password: conf.Redis.Password,
1516
DB: conf.Redis.DB,
1617
})
17-
_, err := client.Ping(context.Background()).Result()
18+
_, err := cache.Ping(context.Background()).Result()
1819
if err != nil {
1920
logger.Fatal("Failed to connect to Redis server: ", err)
2021
} else {
2122
logger.Debug("Connected to Redis server successfully.")
2223
}
2324
}
25+
26+
func SetCache(ctx iris.Context, key string, value interface{}) error {
27+
return cache.Set(ctx.Request().Context(), key, value, conf.Redis.Expiration).Err()
28+
}
29+
30+
func GetCache(ctx iris.Context, key string) (string, error) {
31+
return cache.Get(ctx.Request().Context(), key).Result()
32+
}

conf.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type Config struct {
1919
Port int
2020
Password string
2121
DB int
22-
Expiration int
22+
Expiration time.Duration
2323
}
2424
}
2525

logger.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,25 @@ type Formatter struct {
1313
}
1414

1515
func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error) {
16-
timestamp := time.Now().Format("15:04")
16+
timestamp := time.Now().Format("2006/01/02 15:04")
1717
level := strings.ToUpper(entry.Level.String())
1818
message := entry.Message
1919

20-
return []byte(fmt.Sprintf("[%s] %s - %s\n", level, timestamp, message)), nil
20+
var color string
21+
switch entry.Level {
22+
case logrus.DebugLevel:
23+
color = "\u001B[33m"
24+
case logrus.InfoLevel:
25+
color = "\033[32m"
26+
case logrus.WarnLevel:
27+
color = "\033[33m"
28+
case logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel:
29+
color = "\033[31m"
30+
default:
31+
color = "\033[0m"
32+
}
33+
34+
return []byte(fmt.Sprintf("%s[%s]\u001B[0m %s - %s\n", color, level, timestamp, message)), nil
2135
}
2236

2337
func SetupLogger() {

0 commit comments

Comments
 (0)