@@ -3,13 +3,13 @@ package main
3
3
import (
4
4
"context"
5
5
"fmt"
6
- "log"
7
6
"strings"
8
7
"time"
9
8
10
9
"github.com/clems4ever/go-graphkb/internal/database"
11
10
"github.com/clems4ever/go-graphkb/internal/knowledge"
12
11
"github.com/clems4ever/go-graphkb/internal/server"
12
+ "github.com/sirupsen/logrus"
13
13
"github.com/spf13/cobra"
14
14
"github.com/spf13/viper"
15
15
)
@@ -20,10 +20,10 @@ var Database *database.MariaDB
20
20
// ConfigPath string
21
21
var ConfigPath string
22
22
23
- func main () {
24
- // Display the code line where log.Fatal appeared for troubleshooting
25
- log .SetFlags (log .LstdFlags | log .Lshortfile )
23
+ // LogLevel the log level
24
+ var LogLevel string
26
25
26
+ func main () {
27
27
// Also read env variables prefixed with GRAPHKB_.
28
28
viper .SetEnvPrefix ("GRAPHKB" )
29
29
viper .SetEnvKeyReplacer (strings .NewReplacer ("." , "_" ))
@@ -61,15 +61,32 @@ func main() {
61
61
}
62
62
63
63
rootCmd .PersistentFlags ().StringVar (& ConfigPath , "config" , "config.yml" , "Provide the path to the configuration file (required)" )
64
+ rootCmd .PersistentFlags ().StringVar (& LogLevel , "log-level" , "info" , "The log level among 'debug', 'info', 'warn', 'error'" )
64
65
65
66
cobra .OnInitialize (onInit )
66
67
67
68
rootCmd .AddCommand (cleanCmd , listenCmd , countCmd , readCmd , queryCmd )
68
69
if err := rootCmd .Execute (); err != nil {
69
- log .Fatal (err )
70
+ logrus .Fatal (err )
70
71
}
71
72
}
72
73
74
+ func logLevelParamToSeverity (level string ) logrus.Level {
75
+ switch level {
76
+ case "debug" :
77
+ return logrus .DebugLevel
78
+ case "info" :
79
+ return logrus .InfoLevel
80
+ case "warn" :
81
+ return logrus .WarnLevel
82
+ case "error" :
83
+ return logrus .ErrorLevel
84
+ }
85
+ logrus .Fatal ("Provided level %s is not a valid option" )
86
+ // This should never be reached but needed by the compiler
87
+ return logrus .InfoLevel
88
+ }
89
+
73
90
func onInit () {
74
91
viper .SetConfigFile (ConfigPath )
75
92
viper .SetConfigType ("yaml" )
@@ -78,11 +95,13 @@ func onInit() {
78
95
panic (fmt .Errorf ("Cannot read configuration file from %s" , ConfigPath ))
79
96
}
80
97
81
- fmt .Println ("Using config file:" , viper .ConfigFileUsed ())
98
+ logrus .Info ("Using config file: " , viper .ConfigFileUsed ())
99
+ logrus .SetLevel (logLevelParamToSeverity (LogLevel ))
100
+ logrus .Info ("Using log severity: " , LogLevel )
82
101
83
102
dbName := viper .GetString ("mariadb_database" )
84
103
if dbName == "" {
85
- log .Fatal ("Please provide database_name option in your configuration file" )
104
+ logrus .Fatal ("Please provide database_name option in your configuration file" )
86
105
}
87
106
Database = database .NewMariaDB (
88
107
viper .GetString ("mariadb_username" ),
@@ -95,26 +114,26 @@ func onInit() {
95
114
func count (cmd * cobra.Command , args []string ) {
96
115
countAssets , err := Database .CountAssets ()
97
116
if err != nil {
98
- log .Fatal (err )
117
+ logrus .Fatal (err )
99
118
}
100
119
101
120
countRelations , err := Database .CountRelations ()
102
121
if err != nil {
103
- log .Fatal (err )
122
+ logrus .Fatal (err )
104
123
}
105
124
fmt .Printf ("%d assets\n %d relations\n " , countAssets , countRelations )
106
125
}
107
126
108
127
func flush (cmd * cobra.Command , args []string ) {
109
128
if err := Database .FlushAll (); err != nil {
110
- log .Fatal (err )
129
+ logrus .Fatal (err )
111
130
}
112
- fmt . Println ("Successul flush" )
131
+ logrus . Info ("Successul flush" )
113
132
}
114
133
115
134
func listen (cmd * cobra.Command , args []string ) {
116
135
if err := Database .InitializeSchema (); err != nil {
117
- log .Fatal (err )
136
+ logrus .Fatal (err )
118
137
}
119
138
120
139
listenInterface := viper .GetString ("server_listen" )
@@ -130,7 +149,7 @@ func read(cmd *cobra.Command, args []string) {
130
149
g := knowledge .NewGraph ()
131
150
err := Database .ReadGraph (args [0 ], g )
132
151
if err != nil {
133
- log .Fatal (err )
152
+ logrus .Fatal (err )
134
153
}
135
154
136
155
fmt .Printf ("assets = %d\n relations = %d\n " , len (g .Assets ()), len (g .Relations ()))
@@ -144,15 +163,15 @@ func queryFunc(cmd *cobra.Command, args []string) {
144
163
145
164
r , err := q .Query (ctx , args [0 ])
146
165
if err != nil {
147
- log .Fatal (err )
166
+ logrus .Fatal (err )
148
167
}
149
168
150
169
resultsCount := 0
151
170
for r .Cursor .HasMore () {
152
171
var m interface {}
153
172
err := r .Cursor .Read (context .Background (), & m )
154
173
if err != nil {
155
- log .Fatal (err )
174
+ logrus .Fatal (err )
156
175
}
157
176
158
177
doc := m .([]interface {})
0 commit comments