github.com/getgauge/gauge@v1.6.9/logger/logWriter.go (about)

     1  // Copyright 2019 ThoughtWorks, Inc.
     2  
     3  /*----------------------------------------------------------------
     4   *  Copyright (c) ThoughtWorks, Inc.
     5   *  Licensed under the Apache License, Version 2.0
     6   *  See LICENSE in the project root for license information.
     7   *----------------------------------------------------------------*/
     8  
     9  package logger
    10  
    11  import (
    12  	"bufio"
    13  	"encoding/json"
    14  	"fmt"
    15  	"io"
    16  	"os"
    17  	"strings"
    18  )
    19  
    20  // Writer represents to a custom writer.
    21  // It intercepts the log messages and redirects them to logger according the log level given in info
    22  type Writer struct {
    23  	LoggerID            string
    24  	ShouldWriteToStdout bool
    25  	stream              int
    26  	File                io.Writer
    27  	isErrorStream       bool
    28  }
    29  
    30  // LogInfo represents the log message structure for plugins
    31  type LogInfo struct {
    32  	LogLevel string `json:"logLevel"`
    33  	Message  string `json:"message"`
    34  }
    35  
    36  func (w Writer) Write(p []byte) (int, error) {
    37  	scanner := bufio.NewScanner(strings.NewReader(string(p)))
    38  	for scanner.Scan() {
    39  		_logEntry := strings.Trim(scanner.Text(), " ")
    40  		if _logEntry == "" {
    41  			continue
    42  		}
    43  		_p := []byte(_logEntry)
    44  		m := &LogInfo{}
    45  		err := json.Unmarshal(_p, m)
    46  		if err != nil {
    47  			if w.isErrorStream {
    48  				logError(loggersMap.getLogger(w.LoggerID), w.ShouldWriteToStdout, string(_p))
    49  			} else {
    50  				logInfo(loggersMap.getLogger(w.LoggerID), w.ShouldWriteToStdout, string(_p))
    51  			}
    52  		}
    53  		if w.stream > 0 {
    54  			m.Message = fmt.Sprintf("[runner: %d] %s", w.stream, m.Message)
    55  		}
    56  		switch m.LogLevel {
    57  		case "debug":
    58  			logDebug(loggersMap.getLogger(w.LoggerID), w.ShouldWriteToStdout, m.Message)
    59  		case "info":
    60  			logInfo(loggersMap.getLogger(w.LoggerID), w.ShouldWriteToStdout, m.Message)
    61  		case "error":
    62  			logError(loggersMap.getLogger(w.LoggerID), w.ShouldWriteToStdout, m.Message)
    63  		case "warning":
    64  			logWarning(loggersMap.getLogger(w.LoggerID), w.ShouldWriteToStdout, m.Message)
    65  		case "fatal":
    66  			logCritical(loggersMap.getLogger(w.LoggerID), m.Message)
    67  			addFatalError(w.LoggerID, m.Message)
    68  		}
    69  	}
    70  	return len(p), nil
    71  }
    72  
    73  // LogWriter represents the type which consists of two custom writers
    74  type LogWriter struct {
    75  	Stderr io.Writer
    76  	Stdout io.Writer
    77  }
    78  
    79  // NewLogWriter creates a new logWriter for given id
    80  func NewLogWriter(LoggerID string, stdout bool, stream int) *LogWriter {
    81  	return &LogWriter{
    82  		Stderr: Writer{ShouldWriteToStdout: stdout, stream: stream, LoggerID: LoggerID, File: os.Stderr},
    83  		Stdout: Writer{ShouldWriteToStdout: stdout, stream: stream, LoggerID: LoggerID, File: os.Stdout},
    84  	}
    85  }