forked from project-illium/ilxd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
74 lines (65 loc) · 1.83 KB
/
log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (c) 2024 Project Illium
// Use of this source code is governed by an MIT
// license that can be found in the LICENSE file.
package main
import (
"errors"
"github.com/project-illium/ilxd/blockchain"
"github.com/project-illium/ilxd/blockchain/indexers"
"github.com/project-illium/ilxd/consensus"
"github.com/project-illium/ilxd/gen"
"github.com/project-illium/ilxd/mempool"
"github.com/project-illium/ilxd/sync"
"github.com/project-illium/walletlib"
"github.com/pterm/pterm"
"path"
"strings"
"github.com/project-illium/ilxd/net"
"github.com/project-illium/ilxd/repo"
"gopkg.in/natefinch/lumberjack.v2"
)
var LogLevelMap = map[string]pterm.LogLevel{
"trace": pterm.LogLevelTrace,
"debug": pterm.LogLevelDebug,
"info": pterm.LogLevelInfo,
"warning": pterm.LogLevelWarn,
"error": pterm.LogLevelError,
"fatal": pterm.LogLevelFatal,
}
func setupLogging(logDir, level string, testnet bool) error {
logLevel, ok := LogLevelMap[strings.ToLower(level)]
if !ok {
return errors.New("invalid log level")
}
log = log.WithCustomLogger(pterm.DefaultLogger.WithLevel(logLevel))
if logDir != "" {
logRotator := &lumberjack.Logger{
Filename: path.Join(logDir, repo.DefaultLogFilename),
MaxSize: 10, // Megabytes
MaxAge: 30, // Days
MaxBackups: 3,
}
log = log.WithCustomLogger(pterm.DefaultLogger.
WithLevel(logLevel).
WithFormatter(pterm.LogFormatterJSON).
WithWriter(logRotator))
}
repo.UseLogger(log)
net.UseLogger(log)
blockchain.UseLogger(log)
consensus.UseLogger(log)
gen.UseLogger(log)
sync.UseLogger(log)
mempool.UseLogger(log)
walletlib.UseLogger(log)
indexers.UseLogger(log)
return nil
}
func UpdateLogLevel(level string) error {
logLevel, ok := LogLevelMap[strings.ToLower(level)]
if !ok {
return errors.New("invalid log level")
}
log = log.WithLevel(logLevel)
return nil
}