Skip to content

Commit d4b737e

Browse files
authored
Merge pull request #7 from SenseUnit/log_prefix
Customizable log prefix
2 parents 9a3b226 + aa0c26f commit d4b737e

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

cmd/rgap/main.go

-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
package main
22

3-
import (
4-
"log"
5-
"strings"
6-
)
7-
83
const (
94
progName = "rgap"
105
)
@@ -14,7 +9,5 @@ var (
149
)
1510

1611
func main() {
17-
log.Default().SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.Lshortfile)
18-
log.Default().SetPrefix(strings.ToUpper(progName) + ": ")
1912
Execute()
2013
}

cmd/rgap/root.go

+46-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,56 @@ package main
22

33
import (
44
"context"
5+
"log"
56
"os"
67
"os/signal"
8+
"strings"
79
"syscall"
810

911
"github.com/spf13/cobra"
1012
)
1113

14+
const (
15+
envLogPrefix = "RGAP_LOG_PREFIX"
16+
)
17+
18+
var (
19+
logPrefix logPrefixValue = newLogPrefixValue(defaultLogPrefix())
20+
)
21+
22+
type logPrefixValue struct {
23+
value *string
24+
}
25+
26+
func newLogPrefixValue(s string) logPrefixValue {
27+
return logPrefixValue{
28+
value: &s,
29+
}
30+
}
31+
32+
func (v *logPrefixValue) String() string {
33+
if v == nil || v.value == nil {
34+
return defaultLogPrefix()
35+
}
36+
return *v.value
37+
}
38+
39+
func (v *logPrefixValue) Type() string {
40+
return "string"
41+
}
42+
43+
func (v *logPrefixValue) Set(s string) error {
44+
v.value = &s
45+
return nil
46+
}
47+
48+
func defaultLogPrefix() string {
49+
if envLogPrefixValue, ok := os.LookupEnv(envLogPrefix); ok {
50+
return envLogPrefixValue
51+
}
52+
return strings.ToUpper(progName) + ": "
53+
}
54+
1255
// rootCmd represents the base command when called without any subcommands
1356
var rootCmd = &cobra.Command{
1457
Use: progName,
@@ -25,6 +68,8 @@ var rootCmd = &cobra.Command{
2568
func Execute() {
2669
ctx, done := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
2770
defer done()
71+
log.Default().SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.Lshortfile)
72+
log.Default().SetPrefix(logPrefix.String())
2873
err := rootCmd.ExecuteContext(ctx)
2974
if err != nil {
3075
os.Exit(1)
@@ -36,7 +81,7 @@ func init() {
3681
// Cobra supports persistent flags, which, if defined here,
3782
// will be global for your application.
3883

39-
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cmd.yaml)")
84+
rootCmd.PersistentFlags().Var(&logPrefix, "log-prefix", "log prefix")
4085

4186
// Cobra also supports local flags, which will only run
4287
// when this action is called directly.

output/eventlog.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ type EventLogConfig struct {
1414
}
1515

1616
type EventLog struct {
17-
bridge iface.GroupBridge
18-
groups []uint64
17+
bridge iface.GroupBridge
18+
groups []uint64
1919
unsubFns []func()
2020
}
2121

0 commit comments

Comments
 (0)