github.com/wiselike/revel-cmd@v1.2.1/logger/composite_multihandler.go (about)

     1  package logger
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  
     7  	"github.com/mattn/go-colorable"
     8  	"gopkg.in/natefinch/lumberjack.v2"
     9  )
    10  
    11  type CompositeMultiHandler struct {
    12  	DebugHandler    LogHandler
    13  	InfoHandler     LogHandler
    14  	WarnHandler     LogHandler
    15  	ErrorHandler    LogHandler
    16  	CriticalHandler LogHandler
    17  }
    18  
    19  func NewCompositeMultiHandler() (*CompositeMultiHandler, LogHandler) {
    20  	cw := &CompositeMultiHandler{}
    21  	return cw, cw
    22  }
    23  
    24  func (h *CompositeMultiHandler) Log(r *Record) (err error) {
    25  	var handler LogHandler
    26  
    27  	switch r.Level {
    28  	case LvlInfo:
    29  		handler = h.InfoHandler
    30  	case LvlDebug:
    31  		handler = h.DebugHandler
    32  	case LvlWarn:
    33  		handler = h.WarnHandler
    34  	case LvlError:
    35  		handler = h.ErrorHandler
    36  	case LvlCrit:
    37  		handler = h.CriticalHandler
    38  	}
    39  
    40  	// Embed the caller function in the context
    41  	if handler != nil {
    42  		if err := handler.Log(r); err != nil {
    43  			panic(err)
    44  		}
    45  	}
    46  
    47  	return
    48  }
    49  
    50  func (h *CompositeMultiHandler) SetHandler(handler LogHandler, replace bool, level LogLevel) {
    51  	if handler == nil {
    52  		// Ignore empty handler
    53  		return
    54  	}
    55  	source := &h.DebugHandler
    56  	switch level {
    57  	case LvlDebug:
    58  		source = &h.DebugHandler
    59  	case LvlInfo:
    60  		source = &h.InfoHandler
    61  	case LvlWarn:
    62  		source = &h.WarnHandler
    63  	case LvlError:
    64  		source = &h.ErrorHandler
    65  	case LvlCrit:
    66  		source = &h.CriticalHandler
    67  	}
    68  
    69  	if !replace && *source != nil {
    70  		// If we are not replacing the source make sure that the level handler is applied first
    71  		if _, isLevel := (*source).(*LevelFilterHandler); !isLevel {
    72  			*source = LevelHandler(level, *source)
    73  		}
    74  		// If this already was a list add a new logger to it
    75  		if ll, found := (*source).(*ListLogHandler); found {
    76  			ll.Add(handler)
    77  		} else {
    78  			*source = NewListLogHandler(*source, handler)
    79  		}
    80  	} else {
    81  		*source = handler
    82  	}
    83  }
    84  
    85  // For the multi handler set the handler, using the LogOptions defined.
    86  func (h *CompositeMultiHandler) SetHandlers(handler LogHandler, options *LogOptions) {
    87  	if len(options.Levels) == 0 {
    88  		options.Levels = LvlAllList
    89  	}
    90  
    91  	// Set all levels
    92  	for _, lvl := range options.Levels {
    93  		h.SetHandler(handler, options.ReplaceExistingHandler, lvl)
    94  	}
    95  }
    96  
    97  func (h *CompositeMultiHandler) SetJSON(writer io.Writer, options *LogOptions) {
    98  	handler := CallerFileHandler(StreamHandler(writer, JSONFormatEx(
    99  		options.GetBoolDefault("pretty", false),
   100  		options.GetBoolDefault("lineSeparated", true),
   101  	)))
   102  	if options.HandlerWrap != nil {
   103  		handler = options.HandlerWrap.SetChild(handler)
   104  	}
   105  	h.SetHandlers(handler, options)
   106  }
   107  
   108  // Use built in rolling function.
   109  func (h *CompositeMultiHandler) SetJSONFile(filePath string, options *LogOptions) {
   110  	writer := &lumberjack.Logger{
   111  		Filename:   filePath,
   112  		MaxSize:    options.GetIntDefault("maxSizeMB", 1024), // megabytes
   113  		MaxAge:     options.GetIntDefault("maxAgeDays", 7),   // days
   114  		MaxBackups: options.GetIntDefault("maxBackups", 7),
   115  		Compress:   options.GetBoolDefault("compress", true),
   116  	}
   117  	h.SetJSON(writer, options)
   118  }
   119  
   120  func (h *CompositeMultiHandler) SetTerminal(writer io.Writer, options *LogOptions) {
   121  	streamHandler := StreamHandler(
   122  		writer,
   123  		TerminalFormatHandler(
   124  			options.GetBoolDefault("noColor", false),
   125  			options.GetBoolDefault("smallDate", true)))
   126  
   127  	if os.Stdout == writer {
   128  		streamHandler = StreamHandler(
   129  			colorable.NewColorableStdout(),
   130  			TerminalFormatHandler(
   131  				options.GetBoolDefault("noColor", false),
   132  				options.GetBoolDefault("smallDate", true)))
   133  	} else if os.Stderr == writer {
   134  		streamHandler = StreamHandler(
   135  			colorable.NewColorableStderr(),
   136  			TerminalFormatHandler(
   137  				options.GetBoolDefault("noColor", false),
   138  				options.GetBoolDefault("smallDate", true)))
   139  	}
   140  	handler := CallerFileHandler(streamHandler)
   141  
   142  	if options.HandlerWrap != nil {
   143  		handler = options.HandlerWrap.SetChild(handler)
   144  	}
   145  	h.SetHandlers(handler, options)
   146  }
   147  
   148  // Use built in rolling function.
   149  func (h *CompositeMultiHandler) SetTerminalFile(filePath string, options *LogOptions) {
   150  	writer := &lumberjack.Logger{
   151  		Filename:   filePath,
   152  		MaxSize:    options.GetIntDefault("maxSizeMB", 1024), // megabytes
   153  		MaxAge:     options.GetIntDefault("maxAgeDays", 7),   // days
   154  		MaxBackups: options.GetIntDefault("maxBackups", 7),
   155  		Compress:   options.GetBoolDefault("compress", true),
   156  	}
   157  	h.SetTerminal(writer, options)
   158  }
   159  
   160  func (h *CompositeMultiHandler) Disable(levels ...LogLevel) {
   161  	if len(levels) == 0 {
   162  		levels = LvlAllList
   163  	}
   164  	for _, level := range levels {
   165  		switch level {
   166  		case LvlDebug:
   167  			h.DebugHandler = nil
   168  		case LvlInfo:
   169  			h.InfoHandler = nil
   170  		case LvlWarn:
   171  			h.WarnHandler = nil
   172  		case LvlError:
   173  			h.ErrorHandler = nil
   174  		case LvlCrit:
   175  			h.CriticalHandler = nil
   176  		}
   177  	}
   178  }