github.com/sujit-baniya/log@v1.0.73/multi.go (about)

     1  package log
     2  
     3  import (
     4  	"io"
     5  )
     6  
     7  // MultiWriter is an alias for MultiLevelWriter
     8  type MultiWriter = MultiLevelWriter
     9  
    10  // MultiLevelWriter is an Writer that log to different writers by different levels
    11  type MultiLevelWriter struct {
    12  	// InfoWriter specifies all the level logs writes to
    13  	InfoWriter Writer
    14  
    15  	// WarnWriter specifies the level greater than or equal to WarnLevel writes to
    16  	WarnWriter Writer
    17  
    18  	// WarnWriter specifies the level greater than or equal to ErrorLevel writes to
    19  	ErrorWriter Writer
    20  
    21  	// ConsoleWriter specifies the console writer
    22  	ConsoleWriter Writer
    23  
    24  	// ConsoleLevel specifies the level greater than or equal to it also writes to
    25  	ConsoleLevel Level
    26  }
    27  
    28  // Close implements io.Closer, and closes the underlying LeveledWriter.
    29  func (w *MultiLevelWriter) Close() (err error) {
    30  	for _, writer := range []Writer{
    31  		w.InfoWriter,
    32  		w.WarnWriter,
    33  		w.ErrorWriter,
    34  		w.ConsoleWriter,
    35  	} {
    36  		if writer == nil {
    37  			continue
    38  		}
    39  		if closer, ok := writer.(io.Closer); ok {
    40  			if err1 := closer.Close(); err1 != nil {
    41  				err = err1
    42  			}
    43  		}
    44  	}
    45  	return
    46  }
    47  
    48  // WriteEntry implements entryWriter.
    49  func (w *MultiLevelWriter) WriteEntry(e *Entry) (n int, err error) {
    50  	var err1 error
    51  	switch e.Level {
    52  	case noLevel, PanicLevel, FatalLevel, ErrorLevel:
    53  		if w.ErrorWriter != nil {
    54  			n, err1 = w.ErrorWriter.WriteEntry(e)
    55  			if err1 != nil && err == nil {
    56  				err = err1
    57  			}
    58  		}
    59  		fallthrough
    60  	case WarnLevel:
    61  		if w.WarnWriter != nil {
    62  			n, err1 = w.WarnWriter.WriteEntry(e)
    63  			if err1 != nil && err == nil {
    64  				err = err1
    65  			}
    66  		}
    67  		fallthrough
    68  	default:
    69  		if w.InfoWriter != nil {
    70  			n, err1 = w.InfoWriter.WriteEntry(e)
    71  			if err1 != nil && err == nil {
    72  				err = err1
    73  			}
    74  		}
    75  	}
    76  
    77  	if w.ConsoleWriter != nil && e.Level >= w.ConsoleLevel {
    78  		_, _ = w.ConsoleWriter.WriteEntry(e)
    79  	}
    80  
    81  	return
    82  }
    83  
    84  var _ Writer = (*MultiLevelWriter)(nil)
    85  
    86  // MultiEntryWriter is an array Writer that log to different writers
    87  type MultiEntryWriter []Writer
    88  
    89  // Close implements io.Closer, and closes the underlying MultiEntryWriter.
    90  func (w *MultiEntryWriter) Close() (err error) {
    91  	for _, writer := range *w {
    92  		if closer, ok := writer.(io.Closer); ok {
    93  			if err1 := closer.Close(); err1 != nil {
    94  				err = err1
    95  			}
    96  		}
    97  	}
    98  	return
    99  }
   100  
   101  // WriteEntry implements entryWriter.
   102  func (w *MultiEntryWriter) WriteEntry(e *Entry) (n int, err error) {
   103  	var err1 error
   104  	for _, writer := range *w {
   105  		n, err1 = writer.WriteEntry(e)
   106  		if err1 != nil && err == nil {
   107  			err = err1
   108  		}
   109  	}
   110  	return
   111  }
   112  
   113  var _ Writer = (*MultiEntryWriter)(nil)
   114  
   115  // MultiIOWriter is an array io.Writer that log to different writers
   116  type MultiIOWriter []io.Writer
   117  
   118  // Close implements io.Closer, and closes the underlying MultiIOWriter.
   119  func (w *MultiIOWriter) Close() (err error) {
   120  	for _, writer := range *w {
   121  		if closer, ok := writer.(io.Closer); ok {
   122  			if err1 := closer.Close(); err1 != nil {
   123  				err = err1
   124  			}
   125  		}
   126  	}
   127  	return
   128  }
   129  
   130  // WriteEntry implements entryWriter.
   131  func (w *MultiIOWriter) WriteEntry(e *Entry) (n int, err error) {
   132  	for _, writer := range *w {
   133  		n, err = writer.Write(e.buf)
   134  		if err != nil {
   135  			return
   136  		}
   137  	}
   138  
   139  	return
   140  }