@@ -36,12 +36,6 @@ type checkpoint struct {
3636
3737var checkpointData checkpoint
3838
39- func logToDebug (msg string ) {
40- if _ , err := debugFile .WriteString (fmt .Sprintf ("%s - %s\n " , time .Now ().Format ("2006-01-02 15:04:05" ), msg )); err != nil {
41- fmt .Println (err )
42- }
43- }
44-
4539func initialiseDebugFile () {
4640 if _ , err := os .Stat (filepath .Join (pkg .ConfigData .LogsPath , ".k8s.debug" )); os .IsNotExist (err ) {
4741 debugFile , err = os .Create (filepath .Join (pkg .ConfigData .LogsPath , ".k8s.debug" ))
@@ -54,6 +48,8 @@ func initialiseDebugFile() {
5448 panic (err )
5549 }
5650 }
51+ os .Stdout = debugFile
52+ os .Stderr = debugFile
5753}
5854
5955func readCheckpoint () {
@@ -112,14 +108,14 @@ func writeCheckpoint() {
112108}
113109
114110func main () {
115- logToDebug ("Starting logger..." )
111+ fmt . Println ("Starting logger..." )
116112 initialiseDebugFile ()
117113 readCheckpoint ()
118114 defer writeCheckpoint ()
119115 kubeconfig := clientcmd .NewDefaultClientConfigLoadingRules ().GetDefaultFilename ()
120116 config , err := clientcmd .BuildConfigFromFlags ("" , kubeconfig )
121117 if err != nil {
122- logToDebug (err .Error ())
118+ fmt . Println (err .Error ())
123119 }
124120 cs := kubernetes .NewForConfigOrDie (config )
125121 ctx , cancel := context .WithCancel (context .Background ())
@@ -139,7 +135,7 @@ func main() {
139135 }
140136 initialList , err := cs .CoreV1 ().Pods (namespace ).List (context .TODO (), opts )
141137 if err != nil {
142- logToDebug ("Exiting runner..." + err .Error ())
138+ fmt . Println ("Exiting runner..." + err .Error ())
143139 return
144140 }
145141 pods := mergeSort (initialList .Items )
@@ -149,10 +145,10 @@ func main() {
149145 opts .ResourceVersion = initialList .ResourceVersion
150146 watcher , err := cs .CoreV1 ().Pods (namespace ).Watch (context .TODO (), opts )
151147 if err != nil {
152- logToDebug ("Exiting runner..." + err .Error ())
148+ fmt . Println ("Exiting runner..." + err .Error ())
153149 return
154150 }
155- logToDebug ("Watching for new pods in namespace" + namespace )
151+ fmt . Println ("Watching for new pods in namespace" + namespace )
156152 go func () {
157153 for event := range watcher .ResultChan () {
158154 switch event .Type {
@@ -175,7 +171,7 @@ func main() {
175171 }
176172 }()
177173 wg .Wait ()
178- logToDebug ("Stopping logger..." )
174+ fmt . Println ("Stopping logger..." )
179175}
180176
181177// Process pod first synchronously append metadata to the metadata log because order is important.
@@ -186,10 +182,10 @@ func processPod(ctx context.Context, cs *kubernetes.Clientset, pod *v1.Pod, name
186182 // podNs := pod.Namespace
187183 // TODO: Fix this 5 second wait
188184 time .Sleep (time .Second * 5 ) // Wait for the pod to be ready
189- logToDebug ("New pod added: " + pod .Name + "on time " + creationTime .Format ("2006-01-02 15:04:05" ))
185+ fmt . Println ("New pod added: " + pod .Name + "on time " + creationTime .Format ("2006-01-02 15:04:05" ))
190186 dir := filepath .Join (pkg .ConfigData .LogsPath , namespace )
191187 if err := os .MkdirAll (dir , 0755 ); err != nil {
192- logToDebug (err .Error ())
188+ fmt . Println (err .Error ())
193189 return
194190 }
195191 owner := getLastNode (& PodNode {
@@ -201,7 +197,7 @@ func processPod(ctx context.Context, cs *kubernetes.Clientset, pod *v1.Pod, name
201197 // Use the advisory lock to write in metadata file
202198 f , err := os .OpenFile (path , os .O_CREATE | os .O_WRONLY | os .O_APPEND , 0644 )
203199 if err != nil {
204- logToDebug (err .Error ())
200+ fmt . Println (err .Error ())
205201 }
206202 if err := syscall .Flock (int (f .Fd ()), syscall .LOCK_EX ); err != nil {
207203 fmt .Println ("Error locking file:" , err )
@@ -215,11 +211,11 @@ func processPod(ctx context.Context, cs *kubernetes.Clientset, pod *v1.Pod, name
215211 checkpointData .LastResourceVersion = pod .ResourceVersion
216212 //Start watching and recording logs
217213 go func (podName string ) {
218- logToDebug ("Watching logs for pod: " + podName )
214+ fmt . Println ("Watching logs for pod: " + podName )
219215 path = filepath .Join (dir , podName + ".log" )
220216 file , err := os .OpenFile (path , os .O_CREATE | os .O_WRONLY | os .O_APPEND , 0644 )
221217 if err != nil {
222- logToDebug (err .Error ())
218+ fmt . Println (err .Error ())
223219 }
224220 defer file .Close () // Remember to close the file
225221 for {
@@ -229,14 +225,14 @@ func processPod(ctx context.Context, cs *kubernetes.Clientset, pod *v1.Pod, name
229225 req := cs .CoreV1 ().Pods (namespace ).GetLogs (podName , opts )
230226 stream , err := req .Stream (ctx )
231227 if err != nil {
232- logToDebug (err .Error ())
228+ fmt . Println (err .Error ())
233229 return
234230 }
235231 defer stream .Close ()
236232 if _ , err := io .Copy (file , stream ); err != nil {
237- logToDebug (err .Error ())
233+ fmt . Println (err .Error ())
238234 }
239- logToDebug ("Stream closed for pod: " + podName )
235+ fmt . Println ("Stream closed for pod: " + podName )
240236 break
241237 }
242238 }(podName )
@@ -322,7 +318,7 @@ func (p *PodNode) Next() Node {
322318 case "ReplicaSet" :
323319 rs , err := p .cs .AppsV1 ().ReplicaSets (namespace ).Get (context .Background (), name , metav1.GetOptions {})
324320 if err != nil {
325- logToDebug (fmt .Sprintf ("Error fetching ReplicaSet %s: %v" , name , err ))
321+ fmt . Println (fmt .Sprintf ("Error fetching ReplicaSet %s: %v" , name , err ))
326322 return nil
327323 }
328324 rsNode := & ReplicasetNode {
@@ -354,7 +350,7 @@ func (p *ReplicasetNode) Next() Node {
354350 case "Deployment" :
355351 deps , err := p .cs .AppsV1 ().Deployments (namespace ).Get (context .Background (), name , metav1.GetOptions {})
356352 if err != nil {
357- logToDebug (fmt .Sprintf ("Error fetching ReplicaSet %s: %v" , name , err ))
353+ fmt . Println (fmt .Sprintf ("Error fetching ReplicaSet %s: %v" , name , err ))
358354 return nil
359355 }
360356 depsNode := DeploymentNode {
0 commit comments