Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a2a4ec8

Browse files
Merge branch '115-tune-logger' into 'master'
Improves "Logging improvements" See merge request postgres-ai/database-lab!301
2 parents d38b21b + 9b46536 commit a2a4ec8

File tree

5 files changed

+39
-13
lines changed

5 files changed

+39
-13
lines changed

‎cmd/cli/main.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func main() {
9393

9494
// loadEnvironmentParams loads environment params from a Database Lab's config file.
9595
func loadEnvironmentParams(c *cli.Context) error {
96-
dblabLog.DEBUG=c.IsSet("debug")
96+
dblabLog.SetDebug(c.IsSet("debug"))
9797

9898
filename, err := config.GetFilename()
9999
if err != nil {

‎cmd/database-lab/main.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ func loadConfiguration(instanceID string) (*config.Config, error) {
226226
return nil, errors.Wrap(err, "failed to parse config")
227227
}
228228

229-
log.DEBUG=cfg.Global.Debug
229+
log.SetDebug(cfg.Global.Debug)
230230
log.Dbg("Config loaded", cfg)
231231

232232
cfg.Global.InstanceID = instanceID

‎cmd/runci/main.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func loadConfiguration() (*runci.Config, error) {
147147
return nil, errors.WithMessagef(err, "error parsing %s config", configPath)
148148
}
149149

150-
log.DEBUG=cfg.App.Debug
150+
log.SetDebug(cfg.App.Debug)
151151
log.Dbg("Config loaded: ", cfg)
152152

153153
return cfg, nil

‎pkg/log/log.go‎

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ import (
88
"encoding/json"
99
"fmt"
1010
"log"
11+
"os"
1112
)
1213

13-
var DEBUG bool = true
14+
var debugMode bool = true
15+
16+
var std = log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile)
1417

1518
const (
1619
WHITE = "\x1b[1;37m"
@@ -22,6 +25,10 @@ const (
2225
FAIL = RED + "Fail" + END
2326
)
2427

28+
const (
29+
calldepth = 3
30+
)
31+
2532
func toString(i1 interface{}) string {
2633
if i1 == nil {
2734
return ""
@@ -66,31 +73,50 @@ func prepareMessage(v ...interface{}) string {
6673
return message
6774
}
6875

76+
func println(v ...interface{}) {
77+
_ = std.Output(calldepth, fmt.Sprintln(v...))
78+
}
79+
80+
func printf(format string, v ...interface{}) {
81+
_ = std.Output(calldepth, fmt.Sprintf(format, v...))
82+
}
83+
84+
// SetDebug enables debug logs.
85+
func SetDebug(enable bool) {
86+
debugMode = enable
87+
88+
if debugMode {
89+
std.SetFlags(log.LstdFlags | log.Lshortfile)
90+
} else {
91+
std.SetFlags(log.LstdFlags)
92+
}
93+
}
94+
6995
// Output message.
7096
func Msg(v ...interface{}) {
71-
log.Println("[INFO] " + prepareMessage(v...))
97+
println("[INFO] " + prepareMessage(v...))
7298
}
7399

74100
// Output debug message.
75101
func Dbg(v ...interface{}) {
76-
if DEBUG {
77-
log.Println("[DEBUG] " + prepareMessage(v...))
102+
if debugMode {
103+
println("[DEBUG] " + prepareMessage(v...))
78104
}
79105
}
80106

81107
// Output error message.
82108
func Err(v ...interface{}) {
83-
log.Println("[ERROR] " + prepareMessage(v...))
109+
println("[ERROR] " + prepareMessage(v...))
84110
}
85111

86112
// Errf outputs formatted log.
87113
func Errf(format string, v ...interface{}) {
88-
log.Printf("[ERROR] "+format, v...)
114+
printf("[ERROR] "+format, v...)
89115
}
90116

91117
// Messages for security audit.
92118
func Audit(v ...interface{}) {
93-
log.Println("[AUDIT] " + prepareMessage(v...))
119+
println("[AUDIT] " + prepareMessage(v...))
94120
}
95121

96122
func Fatal(v ...interface{}) {

‎pkg/retrieval/engine/postgres/snapshot/physical_test.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Minimum recovery ending location: 0/0
4949
}
5050

5151
func TestWalDir(t *testing.T) {
52-
log.DEBUG=false
52+
log.SetDebug(false)
5353

5454
testCases := []struct {
5555
pgVersion float64
@@ -90,7 +90,7 @@ func TestWalDir(t *testing.T) {
9090
}
9191

9292
func TestWalCommand(t *testing.T) {
93-
log.DEBUG=false
93+
log.SetDebug(false)
9494

9595
testCases := []struct {
9696
pgVersion float64
@@ -131,7 +131,7 @@ func TestWalCommand(t *testing.T) {
131131
}
132132

133133
func TestParsingWalLine(t *testing.T) {
134-
log.DEBUG=false
134+
log.SetDebug(false)
135135

136136
testCases := []struct {
137137
line string

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /