Skip to content

Commit 90feab0

Browse files
authored
backport: catch up the changes from v0.11 to v0.12 (#636)
chore: backport the changes from v0.11-dev to v0.12-dev
1 parent a28e426 commit 90feab0

File tree

5 files changed

+61
-4
lines changed

5 files changed

+61
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5783,7 +5783,7 @@
57835783
- Only run when applicable (#4752)
57845784
- Check git diff on each job (#4770)
57855785
- Checkout code before git diff check (#4779)
5786-
- Add paths
5786+
- Add paths
57875787
- Bump the timeout for test_coverage (#4864)
57885788
- Migrate localnet to github actions (#4878)
57895789
- Add timeouts (#4912)
@@ -6066,7 +6066,7 @@
60666066

60676067
### Swagger
60686068

6069-
- Remove duplicate blockID
6069+
- Remove duplicate blockID
60706070
- Define version (#4952)
60716071

60726072
### Template

cmd/tenderdash/main.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package main
22

33
import (
44
"context"
5+
"fmt"
6+
"io"
57
"os"
68

79
"github.com/tendermint/tendermint/cmd/tenderdash/commands"
@@ -21,10 +23,11 @@ func main() {
2123
panic(err)
2224
}
2325

24-
logger, err := log.NewDefaultLogger(conf.LogFormat, conf.LogLevel)
26+
logger, stopFn, err := newLoggerFromConfig(conf)
2527
if err != nil {
2628
panic(err)
2729
}
30+
defer stopFn()
2831

2932
rcmd := commands.RootCommand(conf, logger)
3033
rcmd.AddCommand(
@@ -64,3 +67,30 @@ func main() {
6467
os.Exit(2)
6568
}
6669
}
70+
71+
func newLoggerFromConfig(conf *config.Config) (log.Logger, func(), error) {
72+
var (
73+
writer io.Writer = os.Stderr
74+
closeFunc = func() {}
75+
err error
76+
)
77+
if conf.LogFilePath != "" {
78+
file, err := os.OpenFile(conf.LogFilePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
79+
if err != nil {
80+
return nil, nil, fmt.Errorf("failed to create log writer: %w", err)
81+
}
82+
closeFunc = func() {
83+
_ = file.Close()
84+
}
85+
writer = io.MultiWriter(writer, file)
86+
}
87+
writer, err = log.NewFormatter(conf.LogFormat, writer)
88+
if err != nil {
89+
return nil, nil, fmt.Errorf("failed to create log formatter: %w", err)
90+
}
91+
logger, err := log.NewLogger(conf.LogLevel, writer)
92+
if err != nil {
93+
return nil, nil, err
94+
}
95+
return logger, closeFunc, nil
96+
}

config/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,9 @@ type BaseConfig struct { //nolint: maligned
214214
// Output format: 'plain' (colored text) or 'json'
215215
LogFormat string `mapstructure:"log-format"`
216216

217+
// Path to the log file. This parameter is an additional option to the existing stderr output
218+
LogFilePath string `mapstructure:"log-file-path"`
219+
217220
// Path to the JSON file containing the initial validator set and other meta data
218221
Genesis string `mapstructure:"genesis-file"`
219222

config/toml.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ log-level = "{{ .BaseConfig.LogLevel }}"
135135
# Output format: 'plain' (colored text) or 'json'
136136
log-format = "{{ .BaseConfig.LogFormat }}"
137137
138+
# Path to the log file. This parameter is an additional option to the existing stderr output
139+
log-file-path = "{{ .BaseConfig.LogFilePath }}"
140+
138141
##### additional base config options #####
139142
140143
# Path to the JSON file containing the initial validator set and other meta data
@@ -468,7 +471,7 @@ fetchers = "{{ .StateSync.Fetchers }}"
468471
[consensus]
469472
470473
wal-file = "{{ js .Consensus.WalPath }}"
471-
# wal-skip-rounds-to-last set to true will skip replaying all non-committed rounds stored in
474+
# wal-skip-rounds-to-last set to true will skip replaying all non-committed rounds stored in
472475
# WAL, increasing performance in a significant way. It should be set to false by default, as it
473476
# can have security side-effects.
474477
wal-skip-rounds-to-last = "{{ .Consensus.WalSkipRoundsToLast }}"

libs/log/default.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,27 @@ func OverrideWithNewLogger(logger Logger, format, level string) error {
104104
return nil
105105
}
106106

107+
// NewFormatter creates a new formatter for the given format. If the format is empty or unsupported then returns error.
108+
func NewFormatter(format string, w io.Writer) (io.Writer, error) {
109+
switch strings.ToLower(format) {
110+
case LogFormatPlain, LogFormatText:
111+
return zerolog.ConsoleWriter{
112+
Out: w,
113+
NoColor: true,
114+
TimeFormat: time.RFC3339Nano,
115+
FormatLevel: func(i interface{}) string {
116+
if ll, ok := i.(string); ok {
117+
return strings.ToUpper(ll)
118+
}
119+
return "????"
120+
},
121+
}, nil
122+
case LogFormatJSON:
123+
return w, nil
124+
}
125+
return nil, fmt.Errorf("unsupported log format: %s", format)
126+
}
127+
107128
func getLogFields(keyVals ...interface{}) map[string]interface{} {
108129
if len(keyVals)%2 != 0 {
109130
return nil

0 commit comments

Comments
 (0)