@@ -15,9 +15,10 @@ import (
1515 "github.com/Azure/azure-container-networking/aitelemetry"
1616 "github.com/Azure/azure-container-networking/cni"
1717 "github.com/Azure/azure-container-networking/cni/api"
18- "github.com/Azure/azure-container-networking/cni/log"
18+ zaplog "github.com/Azure/azure-container-networking/cni/log"
1919 "github.com/Azure/azure-container-networking/cni/network"
2020 "github.com/Azure/azure-container-networking/common"
21+ "github.com/Azure/azure-container-networking/log"
2122 "github.com/Azure/azure-container-networking/nns"
2223 "github.com/Azure/azure-container-networking/platform"
2324 "github.com/Azure/azure-container-networking/store"
@@ -61,11 +62,11 @@ func printVersion() {
6162
6263// send error report to hostnetagent if CNI encounters any error.
6364func reportPluginError (reportManager * telemetry.ReportManager , tb * telemetry.TelemetryBuffer , err error ) {
64- log .Logger .Error ("Report plugin error" )
65+ zaplog .Logger .Error ("Report plugin error" )
6566 reflect .ValueOf (reportManager .Report ).Elem ().FieldByName ("ErrorMessage" ).SetString (err .Error ())
6667
6768 if err := reportManager .SendReport (tb ); err != nil {
68- log .Logger .Error ("SendReport failed" , zap .Error (err ))
69+ zaplog .Logger .Error ("SendReport failed" , zap .Error (err ))
6970 }
7071}
7172
@@ -83,7 +84,7 @@ func validateConfig(jsonBytes []byte) error {
8384}
8485
8586func getCmdArgsFromEnv () (string , * skel.CmdArgs , error ) {
86- log .Logger .Info ("Going to read from stdin" )
87+ zaplog .Logger .Info ("Going to read from stdin" )
8788 stdinData , err := io .ReadAll (os .Stdin )
8889 if err != nil {
8990 return "" , nil , fmt .Errorf ("error reading from stdin: %v" , err )
@@ -109,32 +110,32 @@ func handleIfCniUpdate(update func(*skel.CmdArgs) error) (bool, error) {
109110 return false , nil
110111 }
111112
112- log .Logger .Info ("CNI UPDATE received" )
113+ zaplog .Logger .Info ("CNI UPDATE received" )
113114
114115 _ , cmdArgs , err := getCmdArgsFromEnv ()
115116 if err != nil {
116- log .Logger .Error ("Received error while retrieving cmds from environment" , zap .Error (err ))
117+ zaplog .Logger .Error ("Received error while retrieving cmds from environment" , zap .Error (err ))
117118 return isupdate , err
118119 }
119120
120- log .Logger .Info ("Retrieved command args for update" , zap .Any ("args" , cmdArgs ))
121+ zaplog .Logger .Info ("Retrieved command args for update" , zap .Any ("args" , cmdArgs ))
121122 err = validateConfig (cmdArgs .StdinData )
122123 if err != nil {
123- log .Logger .Error ("Failed to handle CNI UPDATE" , zap .Error (err ))
124+ zaplog .Logger .Error ("Failed to handle CNI UPDATE" , zap .Error (err ))
124125 return isupdate , err
125126 }
126127
127128 err = update (cmdArgs )
128129 if err != nil {
129- log .Logger .Error ("Failed to handle CNI UPDATE" , zap .Error (err ))
130+ zaplog .Logger .Error ("Failed to handle CNI UPDATE" , zap .Error (err ))
130131 return isupdate , err
131132 }
132133
133134 return isupdate , nil
134135}
135136
136137func printCNIError (msg string ) {
137- log .Logger .Error (msg )
138+ zaplog .Logger .Error (msg )
138139 cniErr := & cniTypes.Error {
139140 Code : cniTypes .ErrTryAgainLater ,
140141 Msg : msg ,
@@ -178,7 +179,7 @@ func rootExecute() error {
178179 cniCmd := os .Getenv (cni .Cmd )
179180
180181 if cniCmd != cni .CmdVersion {
181- log .Logger .Info ("Environment variable set" , zap .String ("CNI_COMMAND" , cniCmd ))
182+ zaplog .Logger .Info ("Environment variable set" , zap .String ("CNI_COMMAND" , cniCmd ))
182183
183184 cniReport .GetReport (pluginName , version , ipamQueryURL )
184185
@@ -194,7 +195,7 @@ func rootExecute() error {
194195
195196 tb = telemetry .NewTelemetryBuffer ()
196197 if tberr := tb .Connect (); tberr != nil {
197- log .Logger .Error ("Cannot connect to telemetry service" , zap .Error (tberr ))
198+ zaplog .Logger .Error ("Cannot connect to telemetry service" , zap .Error (tberr ))
198199 return errors .Wrap (err , "lock acquire error" )
199200 }
200201
@@ -209,7 +210,7 @@ func rootExecute() error {
209210 }
210211 sendErr := telemetry .SendCNIMetric (& cniMetric , tb )
211212 if sendErr != nil {
212- log .Logger .Error ("Couldn't send cnilocktimeout metric" , zap .Error (sendErr ))
213+ zaplog .Logger .Error ("Couldn't send cnilocktimeout metric" , zap .Error (sendErr ))
213214 }
214215 }
215216
@@ -219,7 +220,7 @@ func rootExecute() error {
219220
220221 defer func () {
221222 if errUninit := netPlugin .Plugin .UninitializeKeyValueStore (); errUninit != nil {
222- log .Logger .Error ("Failed to uninitialize key-value store of network plugin" , zap .Error (errUninit ))
223+ zaplog .Logger .Error ("Failed to uninitialize key-value store of network plugin" , zap .Error (errUninit ))
223224 }
224225
225226 if recover () != nil {
@@ -246,17 +247,17 @@ func rootExecute() error {
246247
247248 // used to dump state
248249 if cniCmd == cni .CmdGetEndpointsState {
249- log .Logger .Debug ("Retrieving state" )
250+ zaplog .Logger .Debug ("Retrieving state" )
250251 var simpleState * api.AzureCNIState
251252 simpleState , err = netPlugin .GetAllEndpointState ("azure" )
252253 if err != nil {
253- log .Logger .Error ("Failed to get Azure CNI state" , zap .Error (err ))
254+ zaplog .Logger .Error ("Failed to get Azure CNI state" , zap .Error (err ))
254255 return errors .Wrap (err , "Get all endpoints error" )
255256 }
256257
257258 err = simpleState .PrintResult ()
258259 if err != nil {
259- log .Logger .Error ("Failed to print state result to stdout" , zap .Error (err ))
260+ zaplog .Logger .Error ("Failed to print state result to stdout" , zap .Error (err ))
260261 }
261262
262263 return errors .Wrap (err , "Get cni state printresult error" )
@@ -265,9 +266,9 @@ func rootExecute() error {
265266
266267 handled , _ := handleIfCniUpdate (netPlugin .Update )
267268 if handled {
268- log .Logger .Info ("CNI UPDATE finished." )
269+ zaplog .Logger .Info ("CNI UPDATE finished." )
269270 } else if err = netPlugin .Execute (cni .PluginApi (netPlugin )); err != nil {
270- log .Logger .Error ("Failed to execute network plugin" , zap .Error (err ))
271+ zaplog .Logger .Error ("Failed to execute network plugin" , zap .Error (err ))
271272 }
272273
273274 if cniCmd == cni .CmdVersion {
@@ -295,14 +296,23 @@ func main() {
295296 os .Exit (0 )
296297 }
297298
298- loggerCfg := & log.Config {
299+ log .SetName (name )
300+ log .SetLevel (log .LevelInfo )
301+ if err := log .SetTargetLogDirectory (log .TargetLogfile , "" ); err != nil {
302+ fmt .Printf ("Failed to setup cni logging: %v\n " , err )
303+ return
304+ }
305+
306+ defer log .Close ()
307+
308+ loggerCfg := & zaplog.Config {
299309 Level : zapcore .DebugLevel ,
300- LogPath : log .LogPath + name + ".log" ,
310+ LogPath : zaplog .LogPath + name + ".log" ,
301311 MaxSizeInMB : maxLogFileSizeInMb ,
302312 MaxBackups : maxLogFileCount ,
303313 Name : name ,
304314 }
305- log .Initialize (ctx , loggerCfg )
315+ zaplog .Initialize (ctx , loggerCfg )
306316
307317 if rootExecute () != nil {
308318 os .Exit (1 )
0 commit comments