github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/stream/logger.go (about)

     1  /*
     2  Copyright 2017 Mirantis
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package stream
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/json"
    22  	"io"
    23  	"os"
    24  	"sync"
    25  	"time"
    26  
    27  	"github.com/golang/glog"
    28  )
    29  
    30  // NewLogWriter writes the lines from stdout channel to logFile in k8s format
    31  func NewLogWriter(stdout <-chan []byte, logFile string, wg *sync.WaitGroup) {
    32  	defer wg.Done()
    33  	glog.V(1).Info("Spawned new log writer. Log file:", logFile)
    34  	if _, err := os.Stat(logFile); os.IsNotExist(err) {
    35  		file, err := os.Create(logFile)
    36  		if err != nil {
    37  			glog.Warningln("Failed to create output file:", err)
    38  			return
    39  		}
    40  		file.Close()
    41  	}
    42  
    43  	f, err := os.OpenFile(logFile, os.O_WRONLY|os.O_APPEND, 0777)
    44  	if err != nil {
    45  		glog.Error("Failed to open output file:", err)
    46  		return
    47  	}
    48  	defer f.Close()
    49  
    50  	buffer := bytes.NewBufferString("")
    51  	for data := range stdout {
    52  		buffer.Write(data)
    53  		for {
    54  			line, err := buffer.ReadString('\n')
    55  			if err != nil {
    56  				// if EOF then write data back to buffer. It's unfinished line
    57  				if err == io.EOF {
    58  					buffer.WriteString(line)
    59  					break
    60  				} else {
    61  					glog.Error("Error when reading from buffer:", err)
    62  				}
    63  
    64  			}
    65  			err = writeLog(f, line)
    66  			if err != nil {
    67  				break
    68  			}
    69  		}
    70  	}
    71  	glog.V(1).Info("Log writter stopped. Finished logging to file:", logFile)
    72  }
    73  
    74  func writeLog(f *os.File, line string) error {
    75  	// Convert raw line into Kubernetes json.
    76  	m := map[string]interface{}{
    77  		"time":   time.Now().Format(time.RFC3339),
    78  		"stream": "stdout",
    79  		"log":    line,
    80  	}
    81  	converted, err := json.Marshal(m)
    82  	if err != nil {
    83  		glog.Warning("Error marshalling the log line:", err)
    84  		// this must be something exceptional, but let's not stop logging here
    85  		return nil
    86  	}
    87  	if _, err = f.Write(append(converted, '\n')); err != nil {
    88  		glog.V(1).Info("Error writing log line:", err)
    89  		return err
    90  	}
    91  	if err = f.Sync(); err != nil {
    92  		glog.V(1).Info("Error syncing the log file:", err)
    93  		return err
    94  	}
    95  	return nil
    96  }